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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
May 21, 2015 at 00:45 UTC
a9f9f8cc1f59104257eb1a11a2d048f54dd92ee6
5 files changed
+23
-16
builtin/branch.c
+3
-5
@@ -123,14 +123,12 @@ static int branch_merged(int kind, const char *name,
123
124
if (kind == REF_LOCAL_BRANCH) {
125
struct branch *branch = branch_get(name);
126
+ const char *upstream = branch_get_upstream(branch);
127
unsigned char sha1[20];
128
128
- if (branch &&
129
- branch->merge &&
130
- branch->merge[0] &&
131
- branch->merge[0]->dst &&
129
+ if (upstream &&
130
(reference_name = reference_name_to_free =
133
- resolve_refdup(branch->merge[0]->dst, RESOLVE_REF_READING,
131
+ resolve_refdup(upstream, RESOLVE_REF_READING,
132
sha1, NULL)) != NULL)
133
reference_rev = lookup_commit_reference(sha1);
134
}
builtin/for-each-ref.c
+2
-3
@@ -664,10 +664,9 @@ static void populate_value(struct refinfo *ref)
664
continue;
665
branch = branch_get(ref->refname + 11);
666
667
- if (!branch || !branch->merge || !branch->merge[0] ||
668
- !branch->merge[0]->dst)
667
+ refname = branch_get_upstream(branch);
668
+ if (!refname)
669
continue;
670
- refname = branch->merge[0]->dst;
670
} else if (starts_with(name, "color:")) {
671
char color[COLOR_MAXLEN] = "";
672
builtin/log.c
+2
-5
@@ -1632,16 +1632,13 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)
1632
break;
1633
default:
1634
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) {
1637
fprintf(stderr, _("Could not find a tracked"
1638
" remote branch, please"
1639
" specify <upstream> manually.\n"));
1640
usage_with_options(cherry_usage, options);
1641
}
1643
-
1644
- upstream = current_branch->merge[0]->dst;
1642
}
1643
1644
init_revisions(&revs, prefix);
remote.c
+9
-3
@@ -1705,6 +1705,13 @@ int branch_merge_matches(struct branch *branch,
1705
return refname_match(branch->merge[i]->src, refname);
1706
}
1707
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
+
1715
static int ignore_symref_update(const char *refname)
1716
{
1717
unsigned char sha1[20];
@@ -1914,12 +1921,11 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1921
int rev_argc;
1922
1923
/* 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)
1926
return 0;
1927
1928
/* Cannot stat if what we used to build on no longer exists */
1922
- base = branch->merge[0]->dst;
1929
if (read_ref(base, sha1))
1930
return -1;
1931
theirs = lookup_commit_reference(sha1);
remote.h
+7
@@ -218,6 +218,13 @@ const char *pushremote_for_branch(struct branch *branch, int *explicit);
218
int branch_has_merge_config(struct branch *branch);
219
int branch_merge_matches(struct branch *, int n, const char *);
220
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
+
228
/* Flags to match_refs. */
229
enum match_refs_flags {
230
MATCH_REFS_NONE = 0,