Commit d7d1b496 authored by Jeff Hostetler's avatar Jeff Hostetler Committed by Junio C Hamano

stat_tracking_info: return +1 when branches not equal

Extend stat_tracking_info() to return +1 when branches are not equal and to
take a new "enum ahead_behind_flags" argument to allow skipping the (possibly
expensive) ahead/behind computation.

This will be used in the next commit to allow "git status" to avoid full
ahead/behind calculations for performance reasons.
Signed-off-by: default avatarJeff Hostetler <jeffhost@microsoft.com>
Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
parent 1eaabe34
...@@ -1249,8 +1249,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname, ...@@ -1249,8 +1249,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
if (atom->u.remote_ref.option == RR_REF) if (atom->u.remote_ref.option == RR_REF)
*s = show_ref(&atom->u.remote_ref.refname, refname); *s = show_ref(&atom->u.remote_ref.refname, refname);
else if (atom->u.remote_ref.option == RR_TRACK) { else if (atom->u.remote_ref.option == RR_TRACK) {
if (stat_tracking_info(branch, &num_ours, if (stat_tracking_info(branch, &num_ours, &num_theirs,
&num_theirs, NULL)) { NULL, AHEAD_BEHIND_FULL) < 0) {
*s = xstrdup(msgs.gone); *s = xstrdup(msgs.gone);
} else if (!num_ours && !num_theirs) } else if (!num_ours && !num_theirs)
*s = ""; *s = "";
...@@ -1267,8 +1267,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname, ...@@ -1267,8 +1267,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
free((void *)to_free); free((void *)to_free);
} }
} else if (atom->u.remote_ref.option == RR_TRACKSHORT) { } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
if (stat_tracking_info(branch, &num_ours, if (stat_tracking_info(branch, &num_ours, &num_theirs,
&num_theirs, NULL)) NULL, AHEAD_BEHIND_FULL) < 0)
return; return;
if (!num_ours && !num_theirs) if (!num_ours && !num_theirs)
......
...@@ -2007,16 +2007,23 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) ...@@ -2007,16 +2007,23 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid)
} }
/* /*
* Compare a branch with its upstream, and save their differences (number * Lookup the upstream branch for the given branch and if present, optionally
* of commits) in *num_ours and *num_theirs. The name of the upstream branch * compute the commit ahead/behind values for the pair.
* (or NULL if no upstream is defined) is returned via *upstream_name, if it *
* is not itself NULL. * If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
* counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
* the (potentially expensive) a/b computation (*num_ours and *num_theirs are
* set to zero).
*
* The name of the upstream branch (or NULL if no upstream is defined) is
* returned via *upstream_name, if it is not itself NULL.
* *
* Returns -1 if num_ours and num_theirs could not be filled in (e.g., no * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
* upstream defined, or ref does not exist), 0 otherwise. * upstream defined, or ref does not exist). Returns 0 if the commits are
* identical. Returns 1 if commits are different.
*/ */
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
const char **upstream_name) const char **upstream_name, enum ahead_behind_flags abf)
{ {
struct object_id oid; struct object_id oid;
struct commit *ours, *theirs; struct commit *ours, *theirs;
...@@ -2044,11 +2051,13 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, ...@@ -2044,11 +2051,13 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
if (!ours) if (!ours)
return -1; return -1;
*num_theirs = *num_ours = 0;
/* are we the same? */ /* are we the same? */
if (theirs == ours) { if (theirs == ours)
*num_theirs = *num_ours = 0;
return 0; return 0;
} if (abf == AHEAD_BEHIND_QUICK)
return 1;
/* Run "rev-list --left-right ours...theirs" internally... */ /* Run "rev-list --left-right ours...theirs" internally... */
argv_array_push(&argv, ""); /* ignored */ argv_array_push(&argv, ""); /* ignored */
...@@ -2064,8 +2073,6 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, ...@@ -2064,8 +2073,6 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
die("revision walk setup failed"); die("revision walk setup failed");
/* ... and count the commits on each side. */ /* ... and count the commits on each side. */
*num_ours = 0;
*num_theirs = 0;
while (1) { while (1) {
struct commit *c = get_revision(&revs); struct commit *c = get_revision(&revs);
if (!c) if (!c)
...@@ -2081,7 +2088,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, ...@@ -2081,7 +2088,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
clear_commit_marks(theirs, ALL_REV_FLAGS); clear_commit_marks(theirs, ALL_REV_FLAGS);
argv_array_clear(&argv); argv_array_clear(&argv);
return 0; return 1;
} }
/* /*
...@@ -2094,7 +2101,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) ...@@ -2094,7 +2101,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
char *base; char *base;
int upstream_is_gone = 0; int upstream_is_gone = 0;
if (stat_tracking_info(branch, &ours, &theirs, &full_base) < 0) { if (stat_tracking_info(branch, &ours, &theirs, &full_base,
AHEAD_BEHIND_FULL) < 0) {
if (!full_base) if (!full_base)
return 0; return 0;
upstream_is_gone = 1; upstream_is_gone = 1;
......
...@@ -257,9 +257,15 @@ enum match_refs_flags { ...@@ -257,9 +257,15 @@ enum match_refs_flags {
MATCH_REFS_FOLLOW_TAGS = (1 << 3) MATCH_REFS_FOLLOW_TAGS = (1 << 3)
}; };
/* Flags for --ahead-behind option. */
enum ahead_behind_flags {
AHEAD_BEHIND_QUICK = 0, /* just eq/neq reporting */
AHEAD_BEHIND_FULL = 1, /* traditional a/b reporting */
};
/* Reporting of tracking info */ /* Reporting of tracking info */
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs, int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
const char **upstream_name); const char **upstream_name, enum ahead_behind_flags abf);
int format_tracking_info(struct branch *branch, struct strbuf *sb); int format_tracking_info(struct branch *branch, struct strbuf *sb);
struct ref *get_local_heads(void); struct ref *get_local_heads(void);
......
...@@ -1796,7 +1796,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s) ...@@ -1796,7 +1796,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
color_fprintf(s->fp, branch_color_local, "%s", branch_name); color_fprintf(s->fp, branch_color_local, "%s", branch_name);
if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) { if (stat_tracking_info(branch, &num_ours, &num_theirs, &base,
AHEAD_BEHIND_FULL) < 0) {
if (!base) if (!base)
goto conclude; goto conclude;
...@@ -1933,7 +1934,8 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s) ...@@ -1933,7 +1934,8 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
/* Lookup stats on the upstream tracking branch, if set. */ /* Lookup stats on the upstream tracking branch, if set. */
branch = branch_get(branch_name); branch = branch_get(branch_name);
base = NULL; base = NULL;
ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0); ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind,
&base, AHEAD_BEHIND_FULL) >= 0);
if (base) { if (base) {
base = shorten_unambiguous_ref(base, 0); base = shorten_unambiguous_ref(base, 0);
fprintf(s->fp, "# branch.upstream %s%c", base, eol); fprintf(s->fp, "# branch.upstream %s%c", base, eol);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment