Skip to content

Commit 42d0eb0

Browse files
KarthikNayakgitster
authored andcommitted
ref-filter: modify "%(objectname:short)" to take length
Add support for %(objectname:short=<length>) which would print the abbreviated unique objectname of given length. When no length is specified, the length is 'DEFAULT_ABBREV'. The minimum length is 'MINIMUM_ABBREV'. The length may be exceeded to ensure that the provided object name is unique. Add tests and documentation for the same. Mentored-by: Christian Couder <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Helped-by: Jacob Keller <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f3e3b3 commit 42d0eb0

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ objectsize::
110110
objectname::
111111
The object name (aka SHA-1).
112112
For a non-ambiguous abbreviation of the object name append `:short`.
113+
For an abbreviation of the object name with desired length append
114+
`:short=<length>`, where the minimum length is MINIMUM_ABBREV. The
115+
length may be exceeded to ensure unique object names.
113116

114117
upstream::
115118
The name of a local ref which can be considered ``upstream''

ref-filter.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ static struct used_atom {
5757
cmp_status cmp_status;
5858
const char *str;
5959
} if_then_else;
60-
enum { O_FULL, O_SHORT } objectname;
60+
struct {
61+
enum { O_FULL, O_LENGTH, O_SHORT } option;
62+
unsigned int length;
63+
} objectname;
6164
} u;
6265
} *used_atom;
6366
static int used_atom_cnt, need_tagged, need_symref;
@@ -129,10 +132,17 @@ static void contents_atom_parser(struct used_atom *atom, const char *arg)
129132
static void objectname_atom_parser(struct used_atom *atom, const char *arg)
130133
{
131134
if (!arg)
132-
atom->u.objectname = O_FULL;
135+
atom->u.objectname.option = O_FULL;
133136
else if (!strcmp(arg, "short"))
134-
atom->u.objectname = O_SHORT;
135-
else
137+
atom->u.objectname.option = O_SHORT;
138+
else if (skip_prefix(arg, "short=", &arg)) {
139+
atom->u.objectname.option = O_LENGTH;
140+
if (strtoul_ui(arg, 10, &atom->u.objectname.length) ||
141+
atom->u.objectname.length == 0)
142+
die(_("positive value expected objectname:short=%s"), arg);
143+
if (atom->u.objectname.length < MINIMUM_ABBREV)
144+
atom->u.objectname.length = MINIMUM_ABBREV;
145+
} else
136146
die(_("unrecognized %%(objectname) argument: %s"), arg);
137147
}
138148

@@ -606,12 +616,15 @@ static int grab_objectname(const char *name, const unsigned char *sha1,
606616
struct atom_value *v, struct used_atom *atom)
607617
{
608618
if (starts_with(name, "objectname")) {
609-
if (atom->u.objectname == O_SHORT) {
619+
if (atom->u.objectname.option == O_SHORT) {
610620
v->s = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
611621
return 1;
612-
} else if (atom->u.objectname == O_FULL) {
622+
} else if (atom->u.objectname.option == O_FULL) {
613623
v->s = xstrdup(sha1_to_hex(sha1));
614624
return 1;
625+
} else if (atom->u.objectname.option == O_LENGTH) {
626+
v->s = xstrdup(find_unique_abbrev(sha1, atom->u.objectname.length));
627+
return 1;
615628
} else
616629
die("BUG: unknown %%(objectname) option");
617630
}

t/t6300-for-each-ref.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ test_atom head objecttype commit
6060
test_atom head objectsize 171
6161
test_atom head objectname $(git rev-parse refs/heads/master)
6262
test_atom head objectname:short $(git rev-parse --short refs/heads/master)
63+
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
64+
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
6365
test_atom head tree $(git rev-parse refs/heads/master^{tree})
6466
test_atom head parent ''
6567
test_atom head numparent 0
@@ -99,6 +101,8 @@ test_atom tag objecttype tag
99101
test_atom tag objectsize 154
100102
test_atom tag objectname $(git rev-parse refs/tags/testtag)
101103
test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
104+
test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/master)
105+
test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/master)
102106
test_atom tag tree ''
103107
test_atom tag parent ''
104108
test_atom tag numparent ''
@@ -164,6 +168,12 @@ test_expect_success 'Check invalid format specifiers are errors' '
164168
test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
165169
'
166170

171+
test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
172+
test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
173+
test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
174+
test_must_fail git for-each-ref --format="%(objectname:short=foo)"
175+
'
176+
167177
test_date () {
168178
f=$1 &&
169179
committer_date=$2 &&

0 commit comments

Comments
 (0)