Skip to content

Commit 320c1ce

Browse files
refactor format_branch_comparison in preparation
Refactor format_branch_comparison function in preparation for showing comparison with push remote tracking branch. Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
1 parent 50d063e commit 320c1ce

1 file changed

Lines changed: 48 additions & 34 deletions

File tree

remote.c

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,42 +2234,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
22342234
return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
22352235
}
22362236

2237-
/*
2238-
* Return true when there is anything to report, otherwise false.
2239-
*/
2240-
int format_tracking_info(struct branch *branch, struct strbuf *sb,
2241-
enum ahead_behind_flags abf,
2242-
int show_divergence_advice)
2243-
{
2244-
int ours, theirs, sti;
2245-
const char *full_base;
2246-
char *base;
2247-
int upstream_is_gone = 0;
2248-
2249-
sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
2250-
if (sti < 0) {
2251-
if (!full_base)
2252-
return 0;
2253-
upstream_is_gone = 1;
2254-
}
2255-
2256-
base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2257-
full_base, 0);
2258-
if (upstream_is_gone) {
2259-
strbuf_addf(sb,
2260-
_("Your branch is based on '%s', but the upstream is gone.\n"),
2261-
base);
2262-
if (advice_enabled(ADVICE_STATUS_HINTS))
2263-
strbuf_addstr(sb,
2264-
_(" (use \"git branch --unset-upstream\" to fixup)\n"));
2265-
} else if (!sti) {
2237+
static void format_branch_comparison(struct strbuf *sb,
2238+
bool up_to_date,
2239+
int ours, int theirs,
2240+
const char *branch_name,
2241+
enum ahead_behind_flags abf,
2242+
bool show_divergence_advice)
2243+
{
2244+
if (up_to_date) {
22662245
strbuf_addf(sb,
22672246
_("Your branch is up to date with '%s'.\n"),
2268-
base);
2247+
branch_name);
22692248
} else if (abf == AHEAD_BEHIND_QUICK) {
22702249
strbuf_addf(sb,
22712250
_("Your branch and '%s' refer to different commits.\n"),
2272-
base);
2251+
branch_name);
22732252
if (advice_enabled(ADVICE_STATUS_HINTS))
22742253
strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
22752254
"git status --ahead-behind");
@@ -2278,7 +2257,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
22782257
Q_("Your branch is ahead of '%s' by %d commit.\n",
22792258
"Your branch is ahead of '%s' by %d commits.\n",
22802259
ours),
2281-
base, ours);
2260+
branch_name, ours);
22822261
if (advice_enabled(ADVICE_STATUS_HINTS))
22832262
strbuf_addstr(sb,
22842263
_(" (use \"git push\" to publish your local commits)\n"));
@@ -2289,7 +2268,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
22892268
"Your branch is behind '%s' by %d commits, "
22902269
"and can be fast-forwarded.\n",
22912270
theirs),
2292-
base, theirs);
2271+
branch_name, theirs);
22932272
if (advice_enabled(ADVICE_STATUS_HINTS))
22942273
strbuf_addstr(sb,
22952274
_(" (use \"git pull\" to update your local branch)\n"));
@@ -2302,12 +2281,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
23022281
"and have %d and %d different commits each, "
23032282
"respectively.\n",
23042283
ours + theirs),
2305-
base, ours, theirs);
2284+
branch_name, ours, theirs);
23062285
if (show_divergence_advice &&
23072286
advice_enabled(ADVICE_STATUS_HINTS))
23082287
strbuf_addstr(sb,
23092288
_(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
23102289
}
2290+
}
2291+
2292+
/*
2293+
* Return true when there is anything to report, otherwise false.
2294+
*/
2295+
int format_tracking_info(struct branch *branch, struct strbuf *sb,
2296+
enum ahead_behind_flags abf,
2297+
int show_divergence_advice)
2298+
{
2299+
int ours, theirs, cmp_fetch;
2300+
const char *full_base;
2301+
char *base;
2302+
int upstream_is_gone = 0;
2303+
2304+
cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
2305+
if (cmp_fetch < 0) {
2306+
if (!full_base)
2307+
return 0;
2308+
upstream_is_gone = 1;
2309+
}
2310+
2311+
base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2312+
full_base, 0);
2313+
2314+
if (upstream_is_gone) {
2315+
strbuf_addf(sb,
2316+
_("Your branch is based on '%s', but the upstream is gone.\n"),
2317+
base);
2318+
if (advice_enabled(ADVICE_STATUS_HINTS))
2319+
strbuf_addstr(sb,
2320+
_(" (use \"git branch --unset-upstream\" to fixup)\n"));
2321+
} else {
2322+
format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
2323+
}
2324+
23112325
free(base);
23122326
return 1;
23132327
}

0 commit comments

Comments
 (0)