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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Harald Nordgren committed Feb 26, 2026 at 10:33 UTC 04f47265c1cfb8dce2287b5fb36ddfaa86a2ac61
1 file changed +48 -34
remote.c
+48 -34
@@ -2241,42 +2241,21 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
2241 return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
2242 }
2243
2244 -/*
2245 - * Return true when there is anything to report, otherwise false.
2246 - */
2247 -int format_tracking_info(struct branch *branch, struct strbuf *sb,
2248 - enum ahead_behind_flags abf,
2249 - int show_divergence_advice)
2250 -{
2251 - int ours, theirs, sti;
2252 - const char *full_base;
2253 - char *base;
2254 - int upstream_is_gone = 0;
2255 -
2256 - sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
2257 - if (sti < 0) {
2258 - if (!full_base)
2259 - return 0;
2260 - upstream_is_gone = 1;
2261 - }
2262 -
2263 - base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2264 - full_base, 0);
2265 - if (upstream_is_gone) {
2266 - strbuf_addf(sb,
2267 - _("Your branch is based on '%s', but the upstream is gone.\n"),
2268 - base);
2269 - if (advice_enabled(ADVICE_STATUS_HINTS))
2270 - strbuf_addstr(sb,
2271 - _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2272 - } else if (!sti) {
2244 +static void format_branch_comparison(struct strbuf *sb,
2245 + bool up_to_date,
2246 + int ours, int theirs,
2247 + const char *branch_name,
2248 + enum ahead_behind_flags abf,
2249 + bool show_divergence_advice)
2250 +{
2251 + if (up_to_date) {
2252 strbuf_addf(sb,
2253 _("Your branch is up to date with '%s'.\n"),
2275 - base);
2254 + branch_name);
2255 } else if (abf == AHEAD_BEHIND_QUICK) {
2256 strbuf_addf(sb,
2257 _("Your branch and '%s' refer to different commits.\n"),
2279 - base);
2258 + branch_name);
2259 if (advice_enabled(ADVICE_STATUS_HINTS))
2260 strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
2261 "git status --ahead-behind");
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
2264 Q_("Your branch is ahead of '%s' by %d commit.\n",
2265 "Your branch is ahead of '%s' by %d commits.\n",
2266 ours),
2288 - base, ours);
2267 + branch_name, ours);
2268 if (advice_enabled(ADVICE_STATUS_HINTS))
2269 strbuf_addstr(sb,
2270 _(" (use \"git push\" to publish your local commits)\n"));
@@ -2296,7 +2275,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
2275 "Your branch is behind '%s' by %d commits, "
2276 "and can be fast-forwarded.\n",
2277 theirs),
2299 - base, theirs);
2278 + branch_name, theirs);
2279 if (advice_enabled(ADVICE_STATUS_HINTS))
2280 strbuf_addstr(sb,
2281 _(" (use \"git pull\" to update your local branch)\n"));
@@ -2309,12 +2288,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
2288 "and have %d and %d different commits each, "
2289 "respectively.\n",
2290 ours + theirs),
2312 - base, ours, theirs);
2291 + branch_name, ours, theirs);
2292 if (show_divergence_advice &&
2293 advice_enabled(ADVICE_STATUS_HINTS))
2294 strbuf_addstr(sb,
2295 _(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
2296 }
2297 +}
2298 +
2299 +/*
2300 + * Return true when there is anything to report, otherwise false.
2301 + */
2302 +int format_tracking_info(struct branch *branch, struct strbuf *sb,
2303 + enum ahead_behind_flags abf,
2304 + int show_divergence_advice)
2305 +{
2306 + int ours, theirs, cmp_fetch;
2307 + const char *full_base;
2308 + char *base;
2309 + int upstream_is_gone = 0;
2310 +
2311 + cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
2312 + if (cmp_fetch < 0) {
2313 + if (!full_base)
2314 + return 0;
2315 + upstream_is_gone = 1;
2316 + }
2317 +
2318 + base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2319 + full_base, 0);
2320 +
2321 + if (upstream_is_gone) {
2322 + strbuf_addf(sb,
2323 + _("Your branch is based on '%s', but the upstream is gone.\n"),
2324 + base);
2325 + if (advice_enabled(ADVICE_STATUS_HINTS))
2326 + strbuf_addstr(sb,
2327 + _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2328 + } else {
2329 + format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
2330 + }
2331 +
2332 free(base);
2333 return 1;
2334 }