Skip to content

Closes #1464: Add support for libgit2's custom_headers#1465

Merged
jdavid merged 4 commits into
libgit2:masterfrom
beamerblvd:add-custom-header-support
Jun 25, 2026
Merged

Closes #1464: Add support for libgit2's custom_headers#1465
jdavid merged 4 commits into
libgit2:masterfrom
beamerblvd:add-custom-header-support

Conversation

@beamerblvd

Copy link
Copy Markdown
Contributor

Feedback welcome of course, and also this may need more work, because I do not know how to write an automated test for this. An automated test rather depends on having a remote that requires a specific header and fails without it (my particular use case), which isn't really amenable to an automated test. I would love thoughts on approaches I could take. Otherwise, the existing pytest suite, mypy, ruff, and subtests pass.

Manual test:

In [1]: import pathlib, pygit2

In [2]: from typing import override

In [3]: class BitbucketCallbacks(pygit2.RemoteCallbacks):
   ...:     @override
   ...:     def custom_headers(self) -> list[str] | None:
   ...:         return ["Authorization: Bearer BBDC-abc123def456ghi789+"]
   ...: 

In [4]: clone_dir = pathlib.Path("./bs_clone").resolve()

In [5]: clone_dir
Out[5]: PosixPath('/foo/bar/baz/bs_clone')

In [6]: repo = pygit2.clone_repository(url="https://www.example.com/bitbucket/scm/project/repo.git", path=clone_dir, callbacks=BitbucketCallbacks())

In [7]: repo
Out[7]: pygit2.Repository('/foo/bar/baz/bs_clone/.git/')

In [9]: list(repo.branches)
Out[9]: 
['main-3.4.0',
 'origin/1',
 'origin/1.0.0',
 ...]

In [10]: branch = repo.branches.local.create("nicktest-3.4.0", repo[repo.branches["main-3.4.0"].target])

In [11]: branch.is_checked_out()
Out[11]: False

In [14]: repo.set_head(branch.name)

In [15]: branch.is_checked_out()
Out[15]: True

In [16]: (clone_dir / "nicktest.txt").write_text("testing 1 2 3\n")
Out[16]: 13

In [17]: repo.status()
Out[17]: {'nicktest.txt': <FileStatus.WT_NEW: 128>}

In [19]: repo.index.add("nicktest.txt")

In [20]: repo.index.write()

In [21]: repo.status()
Out[21]: {'nicktest.txt': <FileStatus.INDEX_NEW: 1>}

In [22]: tree_id = repo.index.write_tree()

In [23]: author = pygit2.Signature('Nick Williams', 'nick.williams@example.com')

In [24]: parents = [repo.head.target]

In [25]: commit_id = repo.create_commit("HEAD", author, author, "#1234: This is just a test", tree_id, parents)

In [30]: repo.remotes["origin"].push([f"{repo.head.name}:{repo.head.name}"], callbacks=BitbucketCallbacks())

In [31]: branch.upstream = repo.branches.remote["origin/nicktest-3.4.0"]

In [32]: branch.upstream_name
Out[32]: 'refs/remotes/origin/nicktest-3.4.0'

In [37]: repo.status()
Out[37]: {}

In [38]: repo.remotes["origin"].fetch(callbacks=BitbucketCallbacks())
Out[38]: <pygit2.remotes.TransferProgress at 0x11127d5d0>

In [39]: heads = repo.remotes["origin"].list_heads(callbacks=BitbucketCallbacks())

In [40]: [head.name for head in heads]
Out[40]: 
['HEAD',
 'refs/heads/1',
 'refs/heads/1.0.0',
 ...,
 'refs/heads/main-3.4.0',
 'refs/heads/nicktest-3.4.0',
 ...]

@jdavid jdavid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A full test will be difficult, maybe we can do something simple, AI suggested this...

Test that the headers are correctly copied into the C structs, similar to test_push_options in test/test_remote.py. Example:

def test_custom_headers(origin, clone, remote):
    class MyCallbacks(pygit2.RemoteCallbacks):
        def custom_headers(self):
            return ['Authorization: Bearer token', 'X-Other: foo']

    callbacks = MyCallbacks()
    remote.push(['refs/heads/master'], callbacks)
    assert callbacks.push_options.custom_headers.count == 2 

A corresponding fetch/connect test would also be valuable.

Comment thread .gitignore Outdated
Comment thread pygit2/callbacks.py
@beamerblvd beamerblvd force-pushed the add-custom-header-support branch from f2f722f to 1f24b96 Compare June 25, 2026 15:40
@jdavid jdavid merged commit 666278b into libgit2:master Jun 25, 2026
15 checks passed
@beamerblvd beamerblvd deleted the add-custom-header-support branch June 25, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants