Skip to content

Add support for git_remote_ls() #935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ Authors::
Yu Jianjian
chengyuhang
earl
Dan Sully
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.28.3 (UNRELEASED)
-------------------------

- New support for ``Remote.ls_remotes(..)``

- Support for ``/`` operator to traverse trees
`#903 <https://github.com/libgit2/pygit2/pull/903>`_

Expand Down
9 changes: 9 additions & 0 deletions pygit2/decl/oid.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
typedef struct git_oid {
unsigned char id[20];
} git_oid;

// This should go to net.h but due to h_order in _run.py, ffi won't compile properly.
typedef struct git_remote_head {
int local;
git_oid oid;
git_oid loid;
char *name;
char *symref_target;
} git_remote_head;
8 changes: 8 additions & 0 deletions pygit2/decl/remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ const git_refspec * git_remote_get_refspec(const git_remote *remote, size_t n);
int git_remote_get_fetch_refspecs(git_strarray *array, const git_remote *remote);
int git_remote_get_push_refspecs(git_strarray *array, const git_remote *remote);
void git_remote_free(git_remote *remote);

int git_remote_connect(
git_remote *remote,
int direction,
const git_remote_callbacks *callbacks,
const git_proxy_options *proxy_opts,
const git_strarray *custom_headers);
int git_remote_ls(const git_remote_head ***out, size_t *size, git_remote *remote);
53 changes: 53 additions & 0 deletions pygit2/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def _fill_prune_callbacks(self, prune_callbacks):
self._self_handle = ffi.new_handle(self)
prune_callbacks.payload = self._self_handle

def _fill_connect_callbacks(self, connect_callbacks):
connect_callbacks.credentials = self._credentials_cb
# We need to make sure that this handle stays alive
self._self_handle = ffi.new_handle(self)
connect_callbacks.payload = self._self_handle

# These functions exist to be called by the git_remote as
# callbacks. They proxy the call to whatever the user set

Expand Down Expand Up @@ -372,6 +378,19 @@ def push_url(self):

return maybe_string(C.git_remote_pushurl(self._remote))

def connect(self, callbacks=None, direction=C.GIT_DIRECTION_FETCH):
"""Connect to the remote."""

remote_callbacks = ffi.new('git_remote_callbacks *')
C.git_remote_init_callbacks(remote_callbacks, C.GIT_REMOTE_CALLBACKS_VERSION)

if callbacks is None:
callbacks = RemoteCallbacks()

callbacks._fill_connect_callbacks(remote_callbacks)
err = C.git_remote_connect(self._remote, direction, remote_callbacks, ffi.NULL, ffi.NULL);
check_error(err)

def save(self):
"""Save a remote to its repository's configuration."""

Expand Down Expand Up @@ -412,6 +431,40 @@ def fetch(self, refspecs=None, message=None, callbacks=None, prune=C.GIT_FETCH_P

return TransferProgress(C.git_remote_stats(self._remote))

def ls_remotes(self, callbacks=None):
"""Return a list of dicts that maps to `git_remote_head` from a `ls_remotes` call."""

self.connect(callbacks=callbacks)

refs = ffi.new('git_remote_head ***')
refs_len = ffi.new('size_t *')

err = C.git_remote_ls(refs, refs_len, self._remote)
check_error(err)

results = []

for i in range(int(refs_len[0])):

local = bool(refs[0][i].local)

if local:
loid = Oid(raw=bytes(ffi.buffer(refs[0][i].loid.id)[:]))
else:
loid = None

remote = {
"local": local,
"loid": loid,
"name": maybe_string(refs[0][i].name),
"symref_target": maybe_string(refs[0][i].symref_target),
"oid": Oid(raw=bytes(ffi.buffer(refs[0][i].oid.id)[:])),
}

results.append(remote)

return results

def prune(self, callbacks=None):
"""Perform a prune against this remote."""

Expand Down
14 changes: 13 additions & 1 deletion test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ def test_remote_list(self):
remote = self.repo.create_remote(name, url)
assert remote.name in [x.name for x in self.repo.remotes]

@unittest.skipIf(utils.no_network(), "Requires network")
def test_ls_remotes(self):
assert 1 == len(self.repo.remotes)
remote = self.repo.remotes[0]

refs = remote.ls_remotes()

assert refs

# Check that a known ref is returned.
assert next(iter(r for r in refs if r['name'] == 'refs/tags/v0.28.2'))

def test_remote_collection(self):
remote = self.repo.remotes['origin']
assert REMOTE_NAME == remote.name
Expand Down Expand Up @@ -249,7 +261,7 @@ def setUp(self):
def tearDown(self):
self.clone_repo = None
super(PruneTestCase, self).tearDown()

def test_fetch_remote_default(self):
self.clone_repo.remotes[0].fetch()
assert 'origin/i18n' in self.clone_repo.branches
Expand Down