Skip to content

Commit 39f78b0

Browse files
committed
branch: clarify documentation around branches
1 parent 6bd37c3 commit 39f78b0

File tree

2 files changed

+99
-84
lines changed

2 files changed

+99
-84
lines changed

include/git2/branch.h

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ GIT_EXTERN(int) git_branch_create_from_annotated(
7575
/**
7676
* Delete an existing branch reference.
7777
*
78-
* If the branch is successfully deleted, the passed reference
79-
* object will be invalidated. The reference must be freed manually
80-
* by the user.
78+
* Note that if the deletion succeeds, the reference object will not
79+
* be valid anymore, and should be freed immediately by the user using
80+
* `git_reference_free()`.
8181
*
8282
* @param branch A valid reference representing a branch
8383
* @return 0 on success, or an error code.
@@ -145,17 +145,14 @@ GIT_EXTERN(int) git_branch_move(
145145
* Lookup a branch by its name in a repository.
146146
*
147147
* The generated reference must be freed by the user.
148-
*
149148
* The branch name will be checked for validity.
150-
* See `git_tag_create()` for rules about valid names.
151149
*
152-
* @param out pointer to the looked-up branch reference
150+
* @see git_tag_create for rules about valid names.
153151
*
152+
* @param out pointer to the looked-up branch reference
154153
* @param repo the repository to look up the branch
155-
*
156154
* @param branch_name Name of the branch to be looked-up;
157155
* this name is validated for consistency.
158-
*
159156
* @param branch_type Type of the considered branch. This should
160157
* be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
161158
*
@@ -169,116 +166,130 @@ GIT_EXTERN(int) git_branch_lookup(
169166
git_branch_t branch_type);
170167

171168
/**
172-
* Return the name of the given local or remote branch.
169+
* Get the branch name
170+
*
171+
* Given a reference object, this will check that it really is a branch (ie.
172+
* it lives under "refs/heads/" or "refs/remotes/"), and return the branch part
173+
* of it.
173174
*
174-
* The name of the branch matches the definition of the name
175-
* for git_branch_lookup. That is, if the returned name is given
176-
* to git_branch_lookup() then the reference is returned that
177-
* was given to this function.
175+
* @param out Pointer to the abbreviated reference name.
176+
* Owned by ref, do not free.
178177
*
179-
* @param out where the pointer of branch name is stored;
180-
* this is valid as long as the ref is not freed.
181-
* @param ref the reference ideally pointing to a branch
178+
* @param ref A reference object, ideally pointing to a branch
182179
*
183-
* @return 0 on success; otherwise an error code (e.g., if the
184-
* ref is no local or remote branch).
180+
* @return 0 on success; GIT_EINVALID if the reference isn't either a local or
181+
* remote branch, otherwise an error code.
185182
*/
186183
GIT_EXTERN(int) git_branch_name(
187184
const char **out,
188185
const git_reference *ref);
189186

190187
/**
191-
* Return the reference supporting the remote tracking branch,
192-
* given a local branch reference.
188+
* Get the upstream of a branch
189+
*
190+
* Given a reference, this will return a new reference object corresponding
191+
* to its remote tracking branch. The reference must be a local branch.
193192
*
194-
* @param out Pointer where to store the retrieved
195-
* reference.
193+
* @see git_branch_upstream_name for details on the resolution.
196194
*
195+
* @param out Pointer where to store the retrieved reference.
197196
* @param branch Current underlying reference of the branch.
198197
*
199198
* @return 0 on success; GIT_ENOTFOUND when no remote tracking
200-
* reference exists, otherwise an error code.
199+
* reference exists, otherwise an error code.
201200
*/
202201
GIT_EXTERN(int) git_branch_upstream(
203202
git_reference **out,
204203
const git_reference *branch);
205204

206205
/**
207-
* Set the upstream configuration for a given local branch
206+
* Set a branch's upstream branch
208207
*
209-
* @param branch the branch to configure
208+
* This will update the configuration to set the branch named `name` as the upstream of `branch`.
209+
* Pass a NULL name to unset the upstream information.
210210
*
211-
* @param upstream_name remote-tracking or local branch to set as
212-
* upstream. Pass NULL to unset.
211+
* @note the actual tracking reference must have been already created for the
212+
* operation to succeed.
213213
*
214-
* @return 0 or an error code
214+
* @param branch the branch to configure
215+
* @param branch_name remote-tracking or local branch to set as upstream.
216+
*
217+
* @return 0 on success; GIT_ENOTFOUND if there's no branch named `branch_name`
218+
* or an error code
215219
*/
216-
GIT_EXTERN(int) git_branch_set_upstream(git_reference *branch, const char *upstream_name);
220+
GIT_EXTERN(int) git_branch_set_upstream(
221+
git_reference *branch,
222+
const char *branch_name);
217223

218224
/**
219-
* Return the name of the reference supporting the remote tracking branch,
220-
* given the name of a local branch reference.
225+
* Get the upstream name of a branch
221226
*
222-
* @param out Pointer to the user-allocated git_buf which will be
223-
* filled with the name of the reference.
224-
*
225-
* @param repo the repository where the branches live
227+
* Given a local branch, this will return its remote-tracking branch information,
228+
* as a full reference name, ie. "feature/nice" would become
229+
* "refs/remote/origin/feature/nice", depending on that branch's configuration.
226230
*
231+
* @param out the buffer into which the name will be written.
232+
* @param repo the repository where the branches live.
227233
* @param refname reference name of the local branch.
228234
*
229-
* @return 0, GIT_ENOTFOUND when no remote tracking reference exists,
230-
* otherwise an error code.
235+
* @return 0 on success, GIT_ENOTFOUND when no remote tracking reference exists,
236+
* or an error code.
231237
*/
232238
GIT_EXTERN(int) git_branch_upstream_name(
233239
git_buf *out,
234240
git_repository *repo,
235241
const char *refname);
236242

237243
/**
238-
* Determine if the current local branch is pointed at by HEAD.
244+
* Determine if HEAD points to the given branch
239245
*
240-
* @param branch Current underlying reference of the branch.
246+
* @param branch A reference to a local branch.
241247
*
242-
* @return 1 if HEAD points at the branch, 0 if it isn't,
243-
* error code otherwise.
248+
* @return 1 if HEAD points at the branch, 0 if it isn't, or a negative value
249+
* as an error code.
244250
*/
245251
GIT_EXTERN(int) git_branch_is_head(
246252
const git_reference *branch);
247253

248254
/**
249-
* Determine if the current branch is checked out in any linked
250-
* repository.
255+
* Determine if any HEAD points to the current branch
251256
*
252-
* @param branch Reference to the branch.
257+
* This will iterate over all known linked repositories (usually in the form of
258+
* worktrees) and report whether any HEAD is pointing at the current branch.
253259
*
254-
* @return 1 if branch is checked out, 0 if it isn't,
255-
* error code otherwise.
260+
* @param branch A reference to a local branch.
261+
*
262+
* @return 1 if branch is checked out, 0 if it isn't, an error code otherwise.
256263
*/
257264
GIT_EXTERN(int) git_branch_is_checked_out(
258265
const git_reference *branch);
259266

260267
/**
261-
* Return the name of remote that the remote tracking branch belongs to.
268+
* Find the remote name of a remote-tracking branch
262269
*
263-
* @param out Pointer to the user-allocated git_buf which will be filled with the name of the remote.
270+
* This will return the name of the remote whose fetch refspec is matching
271+
* the given branch. E.g. given a branch "refs/remotes/test/master", it will
272+
* extract the "test" part. If refspecs from multiple remotes match,
273+
* the function will return GIT_EAMBIGUOUS.
264274
*
275+
* @param out The buffer into which the name will be written.
265276
* @param repo The repository where the branch lives.
277+
* @param refname complete name of the remote tracking branch.
266278
*
267-
* @param canonical_branch_name name of the remote tracking branch.
268-
*
269-
* @return 0, GIT_ENOTFOUND
270-
* when no remote matching remote was found,
271-
* GIT_EAMBIGUOUS when the branch maps to several remotes,
272-
* otherwise an error code.
279+
* @return 0 on success, GIT_ENOTFOUND when no matching remote was found,
280+
* GIT_EAMBIGUOUS when the branch maps to several remotes,
281+
* otherwise an error code.
273282
*/
274283
GIT_EXTERN(int) git_branch_remote_name(
275284
git_buf *out,
276285
git_repository *repo,
277-
const char *canonical_branch_name);
278-
286+
const char *refname);
279287

280288
/**
281-
* Retrieve the name of the upstream remote of a local branch
289+
* Retrieve the upstream remote of a local branch
290+
*
291+
* This will return the currently configured "branch.*.remote" for a given
292+
* branch. This branch must be local.
282293
*
283294
* @param buf the buffer into which to write the name
284295
* @param repo the repository in which to look

src/branch.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -597,35 +597,36 @@ static int unset_upstream(git_config *config, const char *shortname)
597597
return -1;
598598
}
599599

600-
int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
600+
int git_branch_set_upstream(git_reference *branch, const char *branch_name)
601601
{
602-
git_buf key = GIT_BUF_INIT, value = GIT_BUF_INIT;
602+
git_buf key = GIT_BUF_INIT, remote_name = GIT_BUF_INIT, merge_refspec = GIT_BUF_INIT;
603603
git_reference *upstream;
604604
git_repository *repo;
605605
git_remote *remote = NULL;
606606
git_config *config;
607-
const char *name, *shortname;
607+
const char *refname, *shortname;
608608
int local, error;
609609
const git_refspec *fetchspec;
610610

611-
name = git_reference_name(branch);
612-
if (!git_reference__is_branch(name))
613-
return not_a_local_branch(name);
611+
refname = git_reference_name(branch);
612+
if (!git_reference__is_branch(refname))
613+
return not_a_local_branch(refname);
614614

615615
if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0)
616616
return -1;
617617

618-
shortname = name + strlen(GIT_REFS_HEADS_DIR);
618+
shortname = refname + strlen(GIT_REFS_HEADS_DIR);
619619

620-
if (upstream_name == NULL)
620+
/* We're unsetting, delegate and bail-out */
621+
if (branch_name == NULL)
621622
return unset_upstream(config, shortname);
622623

623624
repo = git_reference_owner(branch);
624625

625-
/* First we need to figure out whether it's a branch or remote-tracking */
626-
if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_LOCAL) == 0)
626+
/* First we need to resolve name to a branch */
627+
if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_LOCAL) == 0)
627628
local = 1;
628-
else if (git_branch_lookup(&upstream, repo, upstream_name, GIT_BRANCH_REMOTE) == 0)
629+
else if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_REMOTE) == 0)
629630
local = 0;
630631
else {
631632
git_error_set(GIT_ERROR_REFERENCE,
@@ -634,60 +635,63 @@ int git_branch_set_upstream(git_reference *branch, const char *upstream_name)
634635
}
635636

636637
/*
637-
* If it's local, the remote is "." and the branch name is
638-
* simply the refname. Otherwise we need to figure out what
639-
* the remote-tracking branch's name on the remote is and use
640-
* that.
638+
* If it's a local-tracking branch, its remote is "." (as "the local
639+
* repository"), and the branch name is simply the refname.
640+
* Otherwise we need to figure out what the remote-tracking branch's
641+
* name on the remote is and use that.
641642
*/
642643
if (local)
643-
error = git_buf_puts(&value, ".");
644+
error = git_buf_puts(&remote_name, ".");
644645
else
645-
error = git_branch_remote_name(&value, repo, git_reference_name(upstream));
646+
error = git_branch_remote_name(&remote_name, repo, git_reference_name(upstream));
646647

647648
if (error < 0)
648649
goto on_error;
649650

651+
/* Update the upsteam branch config with the new name */
650652
if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
651653
goto on_error;
652654

653-
if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
655+
if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&remote_name)) < 0)
654656
goto on_error;
655657

656658
if (local) {
657-
git_buf_clear(&value);
658-
if (git_buf_puts(&value, git_reference_name(upstream)) < 0)
659+
/* A local branch uses the upstream refname directly */
660+
if (git_buf_puts(&merge_refspec, git_reference_name(upstream)) < 0)
659661
goto on_error;
660662
} else {
661-
/* Get the remoe-tracking branch's refname in its repo */
662-
if (git_remote_lookup(&remote, repo, git_buf_cstr(&value)) < 0)
663+
/* We transform the upstream branch name according to the remote's refspecs */
664+
if (git_remote_lookup(&remote, repo, git_buf_cstr(&remote_name)) < 0)
663665
goto on_error;
664666

665667
fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream));
666-
git_buf_clear(&value);
667-
if (!fetchspec || git_refspec_rtransform(&value, fetchspec, git_reference_name(upstream)) < 0)
668+
if (!fetchspec || git_refspec_rtransform(&merge_refspec, fetchspec, git_reference_name(upstream)) < 0)
668669
goto on_error;
669670

670671
git_remote_free(remote);
671672
remote = NULL;
672673
}
673674

675+
/* Update the merge branch config with the refspec */
674676
git_buf_clear(&key);
675677
if (git_buf_printf(&key, "branch.%s.merge", shortname) < 0)
676678
goto on_error;
677679

678-
if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&value)) < 0)
680+
if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&merge_refspec)) < 0)
679681
goto on_error;
680682

681683
git_reference_free(upstream);
682684
git_buf_dispose(&key);
683-
git_buf_dispose(&value);
685+
git_buf_dispose(&remote_name);
686+
git_buf_dispose(&merge_refspec);
684687

685688
return 0;
686689

687690
on_error:
688691
git_reference_free(upstream);
689692
git_buf_dispose(&key);
690-
git_buf_dispose(&value);
693+
git_buf_dispose(&remote_name);
694+
git_buf_dispose(&merge_refspec);
691695
git_remote_free(remote);
692696

693697
return -1;

0 commit comments

Comments
 (0)