aboutsummaryrefslogtreecommitdiff
path: root/remote.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-03-12 14:09:06 -0700
committerJunio C Hamano <gitster@pobox.com>2026-03-12 14:09:06 -0700
commitd0413b31ddcce6ae6ffaff0a30a67ffbd1a7c648 (patch)
tree1caab056d17c7d62d2727398c34259fe3cc315f5 /remote.c
parent03161747b412fe739d8a7ef631769b3d8f60d56f (diff)
parent68791d7506aa39da7c98de4143cac09cc93fc404 (diff)
downloadgit-d0413b31ddcce6ae6ffaff0a30a67ffbd1a7c648.tar.xz
Merge branch 'hn/status-compare-with-push'
"git status" learned to show comparison between the current branch and various other branches listed on status.compareBranches configuration. * hn/status-compare-with-push: status: clarify how status.compareBranches deduplicates status: add status.compareBranches config for multiple branch comparisons refactor format_branch_comparison in preparation
Diffstat (limited to 'remote.c')
-rw-r--r--remote.c178
1 files changed, 141 insertions, 37 deletions
diff --git a/remote.c b/remote.c
index f6980dc656..7ca2a6501b 100644
--- a/remote.c
+++ b/remote.c
@@ -29,6 +29,12 @@
enum map_direction { FROM_SRC, FROM_DST };
+enum {
+ ENABLE_ADVICE_PULL = (1 << 0),
+ ENABLE_ADVICE_PUSH = (1 << 1),
+ ENABLE_ADVICE_DIVERGENCE = (1 << 2),
+};
+
struct counted_string {
size_t len;
const char *s;
@@ -2234,43 +2240,49 @@ 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 char *resolve_compare_branch(struct branch *branch, const char *name)
{
- int ours, theirs, sti;
- const char *full_base;
- char *base;
- int upstream_is_gone = 0;
+ const char *resolved = NULL;
- sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
- if (sti < 0) {
- if (!full_base)
- return 0;
- upstream_is_gone = 1;
+ if (!branch || !name)
+ return NULL;
+
+ if (!strcasecmp(name, "@{upstream}")) {
+ resolved = branch_get_upstream(branch, NULL);
+ } else if (!strcasecmp(name, "@{push}")) {
+ resolved = branch_get_push(branch, NULL);
+ } else {
+ warning(_("ignoring value '%s' for status.compareBranches, "
+ "only @{upstream} and @{push} are supported"),
+ name);
+ return NULL;
}
- 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 (resolved)
+ return xstrdup(resolved);
+ return NULL;
+}
+
+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,
+ unsigned flags)
+{
+ bool use_push_advice = (flags & ENABLE_ADVICE_PUSH);
+ bool use_pull_advice = (flags & ENABLE_ADVICE_PULL);
+ bool use_divergence_advice = (flags & ENABLE_ADVICE_DIVERGENCE);
+
+ 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);
- if (advice_enabled(ADVICE_STATUS_HINTS))
+ branch_name);
+ if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
"git status --ahead-behind");
} else if (!theirs) {
@@ -2278,8 +2290,8 @@ 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);
- if (advice_enabled(ADVICE_STATUS_HINTS))
+ branch_name, ours);
+ if (use_push_advice && advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git push\" to publish your local commits)\n"));
} else if (!ours) {
@@ -2289,8 +2301,8 @@ 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);
- if (advice_enabled(ADVICE_STATUS_HINTS))
+ branch_name, theirs);
+ if (use_pull_advice && advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git pull\" to update your local branch)\n"));
} else {
@@ -2302,14 +2314,106 @@ 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);
- if (show_divergence_advice &&
- advice_enabled(ADVICE_STATUS_HINTS))
+ branch_name, ours, theirs);
+ if (use_divergence_advice && advice_enabled(ADVICE_STATUS_HINTS))
strbuf_addstr(sb,
_(" (use \"git pull\" if you want to integrate the remote branch with yours)\n"));
}
- free(base);
- return 1;
+}
+
+/*
+ * 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)
+{
+ char *compare_branches = NULL;
+ struct string_list branches = STRING_LIST_INIT_DUP;
+ struct strset processed_refs = STRSET_INIT;
+ int reported = 0;
+ size_t i;
+ const char *upstream_ref;
+ const char *push_ref;
+
+ repo_config_get_string(the_repository, "status.comparebranches",
+ &compare_branches);
+
+ if (compare_branches) {
+ string_list_split(&branches, compare_branches, " ", -1);
+ string_list_remove_empty_items(&branches, 0);
+ } else {
+ string_list_append(&branches, "@{upstream}");
+ }
+
+ upstream_ref = branch_get_upstream(branch, NULL);
+ push_ref = branch_get_push(branch, NULL);
+
+ for (i = 0; i < branches.nr; i++) {
+ char *full_ref;
+ char *short_ref;
+ int ours, theirs, cmp;
+ int is_upstream, is_push;
+ unsigned flags = 0;
+
+ full_ref = resolve_compare_branch(branch,
+ branches.items[i].string);
+ if (!full_ref)
+ continue;
+
+ if (!strset_add(&processed_refs, full_ref)) {
+ free(full_ref);
+ continue;
+ }
+
+ short_ref = refs_shorten_unambiguous_ref(
+ get_main_ref_store(the_repository), full_ref, 0);
+
+ is_upstream = upstream_ref && !strcmp(full_ref, upstream_ref);
+ is_push = push_ref && !strcmp(full_ref, push_ref);
+
+ if (is_upstream && (!push_ref || !strcmp(upstream_ref, push_ref)))
+ is_push = 1;
+
+ cmp = stat_branch_pair(branch->refname, full_ref,
+ &ours, &theirs, abf);
+
+ if (cmp < 0) {
+ if (is_upstream) {
+ strbuf_addf(sb,
+ _("Your branch is based on '%s', but the upstream is gone.\n"),
+ short_ref);
+ if (advice_enabled(ADVICE_STATUS_HINTS))
+ strbuf_addstr(sb,
+ _(" (use \"git branch --unset-upstream\" to fixup)\n"));
+ reported = 1;
+ }
+ free(full_ref);
+ free(short_ref);
+ continue;
+ }
+
+ if (reported)
+ strbuf_addstr(sb, "\n");
+
+ if (is_upstream)
+ flags |= ENABLE_ADVICE_PULL;
+ if (is_push)
+ flags |= ENABLE_ADVICE_PUSH;
+ if (show_divergence_advice && is_upstream)
+ flags |= ENABLE_ADVICE_DIVERGENCE;
+ format_branch_comparison(sb, !cmp, ours, theirs, short_ref,
+ abf, flags);
+ reported = 1;
+
+ free(full_ref);
+ free(short_ref);
+ }
+
+ string_list_clear(&branches, 0);
+ strset_clear(&processed_refs);
+ free(compare_branches);
+ return reported;
}
static int one_local_ref(const struct reference *ref, void *cb_data)