diff options
| author | Vicent Marti <vicent@github.com> | 2014-09-09 12:52:36 +0200 |
|---|---|---|
| committer | Vicent Marti <vicent@github.com> | 2014-09-09 12:52:36 +0200 |
| commit | 31e752b6546537bbeee89b6d2f3027cf0eff9a53 (patch) | |
| tree | 70a3645184c7e0c561704a3164b446edaa2846e8 /src | |
| parent | 1e71354e34f360b19285ca108cd07903122a0a62 (diff) | |
| parent | 15c30b72e16528bdf71c0343e4d238600c0df7a1 (diff) | |
| download | libgit2-31e752b6546537bbeee89b6d2f3027cf0eff9a53.tar.gz | |
Merge pull request #2511 from libgit2/cmn/remote-default-restrict
Restrict which refs can be the default branch
Diffstat (limited to 'src')
| -rw-r--r-- | src/clone.c | 49 | ||||
| -rw-r--r-- | src/remote.c | 6 |
2 files changed, 30 insertions, 25 deletions
diff --git a/src/clone.c b/src/clone.c index 7835be1b1..43b839003 100644 --- a/src/clone.c +++ b/src/clone.c @@ -144,9 +144,9 @@ static int update_head_to_remote( const git_signature *signature, const char *reflog_message) { - int error = 0, found_branch = 0; + int error = 0; size_t refs_len; - git_refspec dummy_spec, *refspec; + git_refspec *refspec; const git_remote_head *remote_head, **refs; const git_oid *remote_head_id; git_buf remote_master_name = GIT_BUF_INIT; @@ -155,28 +155,30 @@ static int update_head_to_remote( if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0) return error; - /* Did we just clone an empty repository? */ - if (refs_len == 0) + /* We cloned an empty repository or one with an unborn HEAD */ + if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE)) return setup_tracking_config( repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE); - error = git_remote_default_branch(&branch, remote); - if (error == GIT_ENOTFOUND) { - git_buf_puts(&branch, GIT_REFS_HEADS_MASTER_FILE); - } else { - found_branch = 1; - } - - /* Get the remote's HEAD. This is always the first ref in the list. */ + /* We know we have HEAD, let's see where it points */ remote_head = refs[0]; assert(remote_head); remote_head_id = &remote_head->oid; + + error = git_remote_default_branch(&branch, remote); + if (error == GIT_ENOTFOUND) { + error = git_repository_set_head_detached( + repo, remote_head_id, signature, reflog_message); + goto cleanup; + } + refspec = git_remote__matching_refspec(remote, git_buf_cstr(&branch)); if (refspec == NULL) { - memset(&dummy_spec, 0, sizeof(git_refspec)); - refspec = &dummy_spec; + giterr_set(GITERR_NET, "the remote's default branch does not fit the refspec configuration"); + error = GIT_EINVALIDSPEC; + goto cleanup; } /* Determine the remote tracking reference name from the local master */ @@ -184,21 +186,18 @@ static int update_head_to_remote( &remote_master_name, refspec, git_buf_cstr(&branch))) < 0) - return error; + goto cleanup; - if (found_branch) { - error = update_head_to_new_branch( - repo, - remote_head_id, - git_buf_cstr(&branch), - signature, reflog_message); - } else { - error = git_repository_set_head_detached( - repo, remote_head_id, signature, reflog_message); - } + error = update_head_to_new_branch( + repo, + remote_head_id, + git_buf_cstr(&branch), + signature, reflog_message); +cleanup: git_buf_free(&remote_master_name); git_buf_free(&branch); + return error; } diff --git a/src/remote.c b/src/remote.c index 433015f60..fa5ec8b4c 100644 --- a/src/remote.c +++ b/src/remote.c @@ -1955,6 +1955,9 @@ int git_remote_default_branch(git_buf *out, git_remote *remote) if (heads_len == 0) return GIT_ENOTFOUND; + if (strcmp(heads[0]->name, GIT_HEAD_FILE)) + 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) @@ -1971,6 +1974,9 @@ int git_remote_default_branch(git_buf *out, git_remote *remote) if (git_oid_cmp(head_id, &heads[i]->oid)) continue; + if (git__prefixcmp(heads[i]->name, GIT_REFS_HEADS_DIR)) + continue; + if (!guess) { guess = heads[i]; continue; |
