for-each-ref: add %(upstream:gone) to mark missing refs
PATCH 1/1 for-each-ref: add %(upstream:gone) to mark missing refs
git branch -vv will show "gone" next to a remote tracking branch if it does not exist. for-each-ref is suitable for parsing but had no way of showing this information.
This introduces "%(upstream:gone)" to display "gone" in the formatted output if the ref does not exist or an empty string otherwise, analogous to git branch -vv.
--- Documentation/git-for-each-ref.txt | 5 +++-- ref-filter.c | 10 +++++++++- t/t6300-for-each-ref.sh | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-)
Documentation/git-for-each-ref.txt +3 -2
@@ -114,8 +114,9 @@ upstream:: `refname` above. Additionally respects `:track` to show "[ahead N, behind M]" and `:trackshort` to show the terse version: ">" (ahead), "<" (behind), "<>" (ahead and behind), - or "=" (in sync). Has no effect if the ref does not have - tracking information associated with it. + or "=" (in sync) and `:gone` to show "gone" if the remote ref + does not exist, or an empty string if it does. Has no effect if + the ref does not have tracking information associated with it. push:: The name of a local ref which represents the `@{push}` location
ref-filter.c +9 -1
@@ -37,7 +37,7 @@ static struct used_atom {union {char color[COLOR_MAXLEN ];struct align align; -enum {RR_NORMAL ,RR_SHORTEN ,RR_TRACK ,RR_TRACKSHORT } +enum {RR_NORMAL ,RR_SHORTEN ,RR_TRACK ,RR_TRACKSHORT ,RR_GONE } remote_ref;struct {enum {C_BARE ,C_BODY ,C_BODY_DEP ,C_LINES ,C_SIG ,C_SUB } option;
@@ -67,6 +67,8 @@ static void remote_ref_atom_parser(struct used_atom *atom, const char *arg) atom->u.remote_ref =RR_TRACK ; else if (!strcmp(arg, "trackshort")) atom->u.remote_ref =RR_TRACKSHORT ; + else if (!strcmp(arg, "gone")) + atom->u.remote_ref =RR_GONE ; else die(_("unrecognized format: %%(%s)"), atom->name); }
@@ -923,6 +925,12 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname, *s =">" ; else *s ="<>" ; + }else if (atom->u.remote_ref ==RR_GONE ) { +const char *upstream; +if (stat_tracking_info (branch, &num_ours, &num_theirs, &upstream) <0 ) + *s ="gone" ; +else + *s ="" ; }else /* RR_NORMAL */ *s = refname; }
t/t6300-for-each-ref.sh +12 -0
@@ -383,6 +383,18 @@ test_expect_success 'Check that :track[short] works when upstream is invalid' ' test_cmp expected actual ' +test_expect_success 'Check that :gone produces expected results' ' + cat >expected <<-\EOF && +gone + EOF + test_when_finished "git config branch.master.merge refs/heads/master" && + git config branch.master.merge refs/heads/does-not-exist && + git for-each-ref \ + --format="%(upstream:gone)" \ + refs/heads >actual && + test_cmp expected actual +' + test_expect_success 'Check for invalid refname format' ' test_must_fail git for-each-ref --format="%(refname:INVALID)" '
Signature · 1 line
2.9.2
Quoted 7 lines · Øystein Walle
git branch -vv will show "gone" next to a remote tracking branch if it does not exist. for-each-ref is suitable for parsing but had no way of showing this information.
This introduces "%(upstream:gone)" to display "gone" in the formatted output if the ref does not exist or an empty string otherwise, analogous to git branch -vv.
Hmm. So this sounds like the minimum thing to implement "branch -vv" output, but it feels sort of weirdly non-orthogonal. I guess you'd say:
%(upstream:track)%(upstream:gone)
and assume that only one of them will show any data.
I almost wonder if %(upstream:track) should just output some specific token for the "gone" case, which matches what "branch -vv" does.
(I'm also not sure we can match "branch -vv" exactly here anyway, as it looks like "[%(upstream): %(upstream:track)"], but our "%(upstream:track)" uses its own brackets.)
I suppose that is a backwards-incompatible change for "%(upstream:track)", but I'm not sure how much we have promised about its format. The error cases seem totally undocumented.
Quoted 5 lines
--- Documentation/git-for-each-ref.txt | 5 +++-- ref-filter.c | 10 +++++++++- t/t6300-for-each-ref.sh | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-)
The code itself looks OK to me (assuming we want upstream:gone as the interface), with a few minor nits in the test:
t/t6300-for-each-ref.sh test_expect_success 'Check that :track[short] works when upstream is invalid' ' · line 389
Quoted 4 lines
t/t6300-for-each-ref.sh +4
-0+test_expect_success 'Check that :gone produces expected results' ' + cat >expected <<-\EOF && +gone + EOF
The "<<-" will strip leading tabs, meaning you can indent "gone" here to match the rest of the test.
t/t6300-for-each-ref.sh test_expect_success 'Check that :track[short] works when upstream is invalid' ' · line 391
Quoted 2 lines
t/t6300-for-each-ref.sh +2
-0+ test_when_finished "git config branch.master.merge refs/heads/master" && + git config branch.master.merge refs/heads/does-not-exist &&
I thought this could be test_config, but it is not clever enough to restore an old value, only to remove a value we just added. So this is probably the best solution.
t/t6300-for-each-ref.sh test_expect_success 'Check that :track[short] works when upstream is invalid' ' · line 396
Quoted 5 lines
t/t6300-for-each-ref.sh +5
-0+ git for-each-ref \ + --format="%(upstream:gone)" \ + refs/heads >actual && + test_cmp expected actual +'
This tests only that we show "gone"; do you want to add a second branch and make sure that it shows nothing? Maybe you could even use a new branch name, which makes test_config applicable again:
cat >expected <<-\EOF && master no-upstream gone EOF git branch refs/heads/no-upstream && test_config branch.no-upstream.remote . && test_config branch.no-upstream.merge refs/heads/does-not-exist && git for-each-ref --format="%(refname:short) %(upstream:gone)" >actual && test_cmp expected actual
-Peff