Re: [PATCH v3 13/15] ref-filter: introduce remote_ref_atom_parser()
From: Eric Sunshine <hidden>
Date: 2016-06-15 23:07:53
On Tue, Jan 5, 2016 at 3:03 AM, Karthik Nayak [off-list ref] wrote:
quoted hunk ↗ jump to hunk
Introduce remote_ref_atom_parser() which will parse the '%(upstream)' and '%(push)' atoms and store information into the 'used_atom' structure based on the modifiers used along with the corresponding atom. Helped-by: Ramsay Jones [off-list ref] Helped-by: Eric Sunshine [off-list ref] Signed-off-by: Karthik Nayak <redacted> ---diff --git a/ref-filter.c b/ref-filter.c@@ -841,6 +863,43 @@ static inline char *copy_advance(char *dst, const char *src) +static void fill_remote_ref_details(struct used_atom *atom, const char *refname, + struct branch *branch, const char **s) +{ + int num_ours, num_theirs; + if (atom->u.remote_ref == RR_SHORTEN) + *s = shorten_unambiguous_ref(refname, warn_ambiguous_refs); + else if (atom->u.remote_ref == RR_TRACK) { + if (stat_tracking_info(branch, &num_ours, + &num_theirs, NULL)) + return; + + if (!num_ours && !num_theirs) + *s = ""; + else if (!num_ours) + *s = xstrfmt("[behind %d]", num_theirs); + else if (!num_theirs) + *s = xstrfmt("[ahead %d]", num_ours); + else + *s = xstrfmt("[ahead %d, behind %d]", + num_ours, num_theirs); + } else if (atom->u.remote_ref == RR_TRACKSHORT) { + if (stat_tracking_info(branch, &num_ours, + &num_theirs, NULL)) + return; + + if (!num_ours && !num_theirs) + *s = "="; + else if (!num_ours) + *s = "<"; + else if (!num_theirs) + *s = ">"; + else + *s = "<>"; + } else if (atom->u.remote_ref == RR_NORMAL) + *s = refname;
I think I mentioned this in a previous review: If the code falls past
this final 'else if' for some reason (programmer error), then *s won't
get assigned at all, which is probably undesirable. To protect against
such a case, you might want either to add a final 'else':
else
die("BUG: ...");
or just consider RR_NORMAL the catchall case, and turn the final 'else
if' into a plain 'else':
else /* RR_NORMAL */
*s = refname;
quoted hunk ↗ jump to hunk
+}@@ -894,6 +953,8 @@ static void populate_value(struct ref_array_item *ref) refname = branch_get_upstream(branch, NULL); if (!refname) continue; + fill_remote_ref_details(atom, refname, branch, &v->s); + continue;
There are now two 'continue' statements very close together here. Have
you considered this instead?
if (refname)
fill_remote_ref_details(...);
continue;
It might make the code a bit more straightforward. (Genuine question;
I don't feel too strongly about it.)
quoted hunk ↗ jump to hunk
} else if (starts_with(name, "push")) { const char *branch_name; if (!skip_prefix(ref->refname, "refs/heads/",@@ -904,6 +965,8 @@ static void populate_value(struct ref_array_item *ref) refname = branch_get_push(branch, NULL); if (!refname) continue; + fill_remote_ref_details(atom, refname, branch, &v->s); + continue;
Ditto.
} else if (starts_with(name, "color:")) {
v->s = atom->u.color;
continue;