Re: [PATCH v3 13/15] ref-filter: introduce remote_ref_atom_parser()
- From
Karthik Nayak <karthik.188@gmail.com>
- Date
- Jan 26, 2016, 10:02 UTC
- Message-ID
- <CAOLa=ZQNqia7j8bESD47xAQVW3_7dX6Qw8swwLL3WtesN5BEBg@mail.gmail.com>
- In-Reply-To
- <CAPig+cTLxhrh3d0ouGLGq+ke8mzFKqCz0ABAECSA4vS+=epv9Q@mail.gmail.com>
On Tue, Jan 26, 2016 at 5:58 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 62 quoted lines
> On Tue, Jan 5, 2016 at 3:03 AM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> 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 <ramsay@ramsayjones.plus.com>
>> Helped-by: Eric Sunshine <sunshine@sunshineco.com>
>> Signed-off-by: Karthik Nayak <Karthik.188@gmail.com>
>> ---
>> 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;
>The latter seems to make sense, will implement.
Show 17 quoted lines
>> +} >> @@ -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.)
No, I didn't consider that, and I think it's a good change to implement. Thanks. ( I don't see any problems with it)
Show 12 quoted lines
>
>> } 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.Noted.
-- Regards, Karthik Nayak