aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarald Nordgren <haraldnordgren@gmail.com>2026-02-26 10:33:41 +0000
committerJunio C Hamano <gitster@pobox.com>2026-02-26 07:25:48 -0800
commit04f47265c1cfb8dce2287b5fb36ddfaa86a2ac61 (patch)
tree6987aec25a0937ef2cfc31c46ae496b58b0e459b
parent2aa9b75b4319f94c3bf23c52ca704e010fc86e48 (diff)
downloadgit-04f47265c1cfb8dce2287b5fb36ddfaa86a2ac61.tar.xz
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>
-rw-r--r--remote.c80
1 files changed, 47 insertions, 33 deletions
diff --git a/remote.c b/remote.c
index 468d1bbfab..9b56191dc7 100644
--- a/remote.c
+++ b/remote.c
@@ -2241,42 +2241,21 @@ 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);
}
-/*
- * Return true when there is anything to report, otherwise false.
- */
-int format_tracking_info(struct branch *branch, struct strbuf *sb,
- enum ahead_behind_flags abf,
- int show_divergence_advice)
+static void format_branch_comparison(struct strbuf *sb,
+ bool up_to_date,
+ int ours, int theirs,
+ const char *branch_name,
+ enum ahead_behind_flags abf,
+ bool show_divergence_advice)
{
- int ours, theirs, sti;
- const char *full_base;
- char *base;
- int upstream_is_gone = 0;
-
- sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
- if (sti < 0) {
- if (!full_base)
- return 0;
- upstream_is_gone = 1;
- }
-
- base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
- full_base, 0);
- if (upstream_is_gone) {
- strbuf_addf(sb,
- _("Your branch is based on '%s', but the upstream is gone.\n"),
- base);
- if (advice_enabled(ADVICE_STATUS_HINTS))
- strbuf_addstr(sb,
- _(" (use \"git branch --unset-upstream\" to fixup)\n"));
- } else if (!sti) {
+ if (up_to_date) {
strbuf_addf(sb,
_("Your branch is up to date with '%s'.\n"),
- base);
+ branch_name);
} else if (abf == AHEAD_BEHIND_QUICK) {
strbuf_addf(sb,
_("Your branch and '%s' refer to different commits.\n"),
- base);
+ branch_name);
if (advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
"git status --ahead-behind");
@@ -2285,7 +2264,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
Q_("Your branch is ahead of '%s' by %d commit.\n",
"Your branch is ahead of '%s' by %d commits.\n",
ours),
- base, ours);
+ branch_name, ours);
if (advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git push\" to publish your local commits)\n"));
@@ -2296,7 +2275,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
"Your branch is behind '%s' by %d commits, "
"and can be fast-forwarded.\n",
theirs),
- base, theirs);
+ branch_name, theirs);
if (advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git pull\" to update your local branch)\n"));
@@ -2309,12 +2288,47 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
"and have %d and %d different commits each, "
"respectively.\n",
ours + theirs),
- base, ours, theirs);
+ branch_name, ours, theirs);
if (show_divergence_advice &&
advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
}
+}
+
+/*
+ * Return true when there is anything to report, otherwise false.
+ */
+int format_tracking_info(struct branch *branch, struct strbuf *sb,
+ enum ahead_behind_flags abf,
+ int show_divergence_advice)
+{
+ int ours, theirs, cmp_fetch;
+ const char *full_base;
+ char *base;
+ int upstream_is_gone = 0;
+
+ cmp_fetch = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
+ if (cmp_fetch < 0) {
+ if (!full_base)
+ return 0;
+ upstream_is_gone = 1;
+ }
+
+ base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
+ full_base, 0);
+
+ if (upstream_is_gone) {
+ strbuf_addf(sb,
+ _("Your branch is based on '%s', but the upstream is gone.\n"),
+ base);
+ if (advice_enabled(ADVICE_STATUS_HINTS))
+ strbuf_addstr(sb,
+ _(" (use \"git branch --unset-upstream\" to fixup)\n"));
+ } else {
+ format_branch_comparison(sb, !cmp_fetch, ours, theirs, base, abf, show_divergence_advice);
+ }
+
free(base);
return 1;
}