diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2014-05-21 09:32:35 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2014-05-21 12:12:32 +0200 |
commit | d22db24fb75134f30c3a72af0bc47fc7f0a07f33 (patch) | |
tree | 5f9b1cdeb1432a0da1c88fad05abd4cfb2cc9011 /src/remote.c | |
parent | 04865aa05e9d16ad56920103678ee4c34578da78 (diff) | |
download | libgit2-d22db24fb75134f30c3a72af0bc47fc7f0a07f33.tar.gz |
remote: add api to guess the remote's default branch
If the remote supports the symref protocol extension, then we return
that, otherwise we guess with git's rules.
Diffstat (limited to 'src/remote.c')
-rw-r--r-- | src/remote.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/remote.c b/src/remote.c index bdc4791a9..f2e2e2f7a 100644 --- a/src/remote.c +++ b/src/remote.c @@ -1885,3 +1885,50 @@ int git_remote_delete(git_remote *remote) return 0; } + +int git_remote_default_branch(git_buf *out, git_remote *remote) +{ + const git_remote_head **heads; + const git_remote_head *guess = NULL; + const git_oid *head_id; + size_t heads_len, i; + int error; + + if ((error = git_remote_ls(&heads, &heads_len, remote)) < 0) + return error; + + if (heads_len == 0) + return GIT_ENOTFOUND; + + git_buf_sanitize(out); + /* the first one must be HEAD so if that has the symref info, we're done */ + if (heads[0]->symref_target) + return git_buf_puts(out, heads[0]->symref_target); + + /* + * If there's no symref information, we have to look over them + * and guess. We return the first match unless the master + * branch is a candidate. Then we return the master branch. + */ + head_id = &heads[0]->oid; + + for (i = 1; i < heads_len; i++) { + if (git_oid_cmp(head_id, &heads[i]->oid)) + continue; + + if (!guess) { + guess = heads[i]; + continue; + } + + if (!git__strcmp(GIT_REFS_HEADS_MASTER_FILE, heads[i]->name)) { + guess = heads[i]; + break; + } + } + + if (!guess) + return GIT_ENOTFOUND; + + return git_buf_puts(out, guess->name); +} |