Skip to content

Commit a9f9f8c

Browse files
peffgitster
authored andcommitted
remote.c: introduce branch_get_upstream helper
All of the information needed to find the @{upstream} of a branch is included in the branch struct, but callers have to navigate a series of possible-NULL values to get there. Let's wrap that logic up in an easy-to-read helper. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8770e6f commit a9f9f8c

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

builtin/branch.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ static int branch_merged(int kind, const char *name,
123123

124124
if (kind == REF_LOCAL_BRANCH) {
125125
struct branch *branch = branch_get(name);
126+
const char *upstream = branch_get_upstream(branch);
126127
unsigned char sha1[20];
127128

128-
if (branch &&
129-
branch->merge &&
130-
branch->merge[0] &&
131-
branch->merge[0]->dst &&
129+
if (upstream &&
132130
(reference_name = reference_name_to_free =
133-
resolve_refdup(branch->merge[0]->dst, RESOLVE_REF_READING,
131+
resolve_refdup(upstream, RESOLVE_REF_READING,
134132
sha1, NULL)) != NULL)
135133
reference_rev = lookup_commit_reference(sha1);
136134
}

builtin/for-each-ref.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,9 @@ static void populate_value(struct refinfo *ref)
664664
continue;
665665
branch = branch_get(ref->refname + 11);
666666

667-
if (!branch || !branch->merge || !branch->merge[0] ||
668-
!branch->merge[0]->dst)
667+
refname = branch_get_upstream(branch);
668+
if (!refname)
669669
continue;
670-
refname = branch->merge[0]->dst;
671670
} else if (starts_with(name, "color:")) {
672671
char color[COLOR_MAXLEN] = "";
673672

builtin/log.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,16 +1632,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
16321632
break;
16331633
default:
16341634
current_branch = branch_get(NULL);
1635-
if (!current_branch || !current_branch->merge
1636-
|| !current_branch->merge[0]
1637-
|| !current_branch->merge[0]->dst) {
1635+
upstream = branch_get_upstream(current_branch);
1636+
if (!upstream) {
16381637
fprintf(stderr, _("Could not find a tracked"
16391638
" remote branch, please"
16401639
" specify <upstream> manually.\n"));
16411640
usage_with_options(cherry_usage, options);
16421641
}
1643-
1644-
upstream = current_branch->merge[0]->dst;
16451642
}
16461643

16471644
init_revisions(&revs, prefix);

remote.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,13 @@ int branch_merge_matches(struct branch *branch,
17051705
return refname_match(branch->merge[i]->src, refname);
17061706
}
17071707

1708+
const char *branch_get_upstream(struct branch *branch)
1709+
{
1710+
if (!branch || !branch->merge || !branch->merge[0])
1711+
return NULL;
1712+
return branch->merge[0]->dst;
1713+
}
1714+
17081715
static int ignore_symref_update(const char *refname)
17091716
{
17101717
unsigned char sha1[20];
@@ -1914,12 +1921,11 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
19141921
int rev_argc;
19151922

19161923
/* Cannot stat unless we are marked to build on top of somebody else. */
1917-
if (!branch ||
1918-
!branch->merge || !branch->merge[0] || !branch->merge[0]->dst)
1924+
base = branch_get_upstream(branch);
1925+
if (!base)
19191926
return 0;
19201927

19211928
/* Cannot stat if what we used to build on no longer exists */
1922-
base = branch->merge[0]->dst;
19231929
if (read_ref(base, sha1))
19241930
return -1;
19251931
theirs = lookup_commit_reference(sha1);

remote.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit);
218218
int branch_has_merge_config(struct branch *branch);
219219
int branch_merge_matches(struct branch *, int n, const char *);
220220

221+
/**
222+
* Return the fully-qualified refname of the tracking branch for `branch`.
223+
* I.e., what "branch@{upstream}" would give you. Returns NULL if no
224+
* upstream is defined.
225+
*/
226+
const char *branch_get_upstream(struct branch *branch);
227+
221228
/* Flags to match_refs. */
222229
enum match_refs_flags {
223230
MATCH_REFS_NONE = 0,

0 commit comments

Comments
 (0)