1938
1939
/*
1940
* Compare a branch with its upstream, and save their differences (number
1941
- * of commits) in *num_ours and *num_theirs.
1941
+ * of commits) in *num_ours and *num_theirs. The name of the upstream branch
1942
+ * (or NULL if no upstream is defined) is returned via *upstream_name, if it
1943
+ * is not itself NULL.
1944
*
1943
- * Return 0 if branch has no upstream (no base), -1 if upstream is missing
1944
- * (with "gone" base), otherwise 1 (with base).
1945
+ * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
1946
+ * upstream defined, or ref does not exist), 0 otherwise.
1947
*/
1946
-int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
1948
+int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
1949
+ const char **upstream_name)
1950
{
1951
unsigned char sha1[20];
1952
struct commit *ours, *theirs;
1957
1958
/* Cannot stat unless we are marked to build on top of somebody else. */
1959
base = branch_get_upstream(branch, NULL);
1960
+ if (upstream_name)
1961
+ *upstream_name = base;
1962
if (!base)
1958
- return 0;
1963
+ return -1;
1964
1965
/* Cannot stat if what we used to build on no longer exists */
1966
if (read_ref(base, sha1))
1978
/* are we the same? */
1979
if (theirs == ours) {
1980
*num_theirs = *num_ours = 0;
1976
- return 1;
1981
+ return 0;
1982
}
1983
1984
/* Run "rev-list --left-right ours...theirs" internally... */
2014
/* clear object flags smudged by the above traversal */
2015
clear_commit_marks(ours, ALL_REV_FLAGS);
2016
clear_commit_marks(theirs, ALL_REV_FLAGS);
2012
- return 1;
2017
+ return 0;
2018
}
2019
2020
/*
2023
int format_tracking_info(struct branch *branch, struct strbuf *sb)
2024
{
2025
int ours, theirs;
2026
+ const char *full_base;
2027
char *base;
2028
int upstream_is_gone = 0;
2029
2024
- switch (stat_tracking_info(branch, &ours, &theirs)) {
2025
- case 0:
2026
- /* no base */
2027
- return 0;
2028
- case -1:
2029
- /* with "gone" base */
2030
+ if (stat_tracking_info(branch, &ours, &theirs, &full_base) < 0) {
2031
+ if (!full_base)
2032
+ return 0;
2033
upstream_is_gone = 1;
2031
- break;
2032
- default:
2033
- /* with base */
2034
- break;
2034
}
2035
2037
- base = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
2036
+ base = shorten_unambiguous_ref(full_base, 0);
2037
if (upstream_is_gone) {
2038
strbuf_addf(sb,
2039
_("Your branch is based on '%s', but the upstream is gone.\n"),