Re: [PATCH v17 1/2] refactor format_branch_comparison in preparation
- From
Harald Nordgren <haraldnordgren@gmail.com>
- Date
- Jan 10, 2026, 11:06 UTC
- Message-ID
- <20260110110629.18501-1-haraldnordgren@gmail.com>
- In-Reply-To
- <xmqq7btqtdgy.fsf@gitster.g>
It's following my naming scheme where each push version of a base variable tacks on _push at the end. Since the push logic introduces a second version of most variables, it's hard to make it fully logical when the base versions still retain their "unqualified" names.
I can rename the sti's to 'cmp_fetch' and 'cmp_fetch_push', but does it help?
Probably best would be to create a struct for the common variables between base and push cases. But that's a large refactoring, so maybe better done as a separate follow-up to all of this instead? Here's what is looks like:
```
diff --git a/remote.c b/remote.c index eba013b6b4..581a62c266 100644 --- a/remote.c +++ b/remote.c @@ -2237,41 +2237,47 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf); } +struct format_branch { + int ours, theirs; + int cmp_fetch; + bool up_to_date; + const char *full; + char *name; +}; + static void format_branch_comparison(struct strbuf *sb, - bool up_to_date, - int ours, int theirs, - const char *branch_name, + const struct format_branch *branch, enum ahead_behind_flags abf, bool show_divergence_advice) { - if (up_to_date) { + if (branch->up_to_date) { strbuf_addf(sb, _("Your branch is up to date with '%s'.\n"), - branch_name); + branch->name); } else if (abf == AHEAD_BEHIND_QUICK) { strbuf_addf(sb, _("Your branch and '%s' refer to different commits.\n"), - branch_name); + branch->name); if (advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addf(sb, _(" (use \"%s\" for details)\n"), "git status --ahead-behind"); - } else if (!theirs) { + } else if (!branch->theirs) { strbuf_addf(sb, Q_("Your branch is ahead of '%s' by %d commit.\n", "Your branch is ahead of '%s' by %d commits.\n", - ours), - branch_name, ours); + branch->ours), + branch->name, branch->ours); if (advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, _(" (use \"git push\" to publish your local commits)\n")); - } else if (!ours) { + } else if (!branch->ours) { strbuf_addf(sb, Q_("Your branch is behind '%s' by %d commit, " "and can be fast-forwarded.\n", "Your branch is behind '%s' by %d commits, " "and can be fast-forwarded.\n", - theirs), - branch_name, theirs); + branch->theirs), + branch->name, branch->theirs); if (advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, _(" (use \"git pull\" to update your local branch)\n")); @@ -2283,8 +2289,8 @@ static void format_branch_comparison(struct strbuf *sb, "Your branch and '%s' have diverged,\n" "and have %d and %d different commits each, " "respectively.\n", - ours + theirs), - branch_name, ours, theirs); + branch->ours + branch->theirs), + branch->name, branch->ours, branch->theirs); if (show_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, @@ -2299,33 +2305,32 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb, enum ahead_behind_flags abf, int show_divergence_advice) { - int ours, theirs, sti; - const char *full_base; - char *base; int upstream_is_gone = 0; + struct format_branch base; - sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf); - if (sti < 0) { - if (!full_base) + base.cmp_fetch = stat_tracking_info(branch, &base.ours, &base.theirs, &base.full, 0, abf); + base.up_to_date = !base.cmp_fetch; + if (base.cmp_fetch < 0) { + if (!base.full) return 0; upstream_is_gone = 1; } - base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), - full_base, 0); + base.name = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + base.full, 0); if (upstream_is_gone) { strbuf_addf(sb, _("Your branch is based on '%s', but the upstream is gone.\n"), - base); + base.name); if (advice_enabled(ADVICE_STATUS_HINTS)) strbuf_addstr(sb, _(" (use \"git branch --unset-upstream\" to fixup)\n")); } else { - format_branch_comparison(sb, !sti, ours, theirs, base, abf, show_divergence_advice); + format_branch_comparison(sb, &base, abf, show_divergence_advice); } - free(base); + free(base.name); return 1; } ``` Harald