Skip to content

Commit cd74e47

Browse files
committed
sha1_name.c: introduce get_sha1_committish()
Many callers know that the user meant to name a committish by syntactical positions where the object name appears. Calling this function allows the machinery to disambiguate shorter-than-unique abbreviated object names between committish and others. Note that this does NOT error out when the named object is not a committish. It is merely to give a hint to the disambiguation machinery. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33bd598 commit cd74e47

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ struct object_context {
817817
#define GET_SHA1_ONLY_TO_DIE 04000
818818

819819
extern int get_sha1(const char *str, unsigned char *sha1);
820+
extern int get_sha1_committish(const char *str, unsigned char *sha1);
820821
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
821822
extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc);
822823

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ struct commit *lookup_commit_reference_by_name(const char *name)
6767
unsigned char sha1[20];
6868
struct commit *commit;
6969

70-
if (get_sha1(name, sha1))
70+
if (get_sha1_committish(name, sha1))
7171
return NULL;
7272
commit = lookup_commit_reference(sha1);
7373
if (!commit || parse_commit(commit))

revision.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
979979
flags ^= UNINTERESTING;
980980
arg++;
981981
}
982-
if (get_sha1(arg, sha1))
982+
if (get_sha1_committish(arg, sha1))
983983
return 0;
984984
while (1) {
985985
it = get_reference(revs, arg, sha1, 0);
@@ -1120,8 +1120,8 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs,
11201120
next = "HEAD";
11211121
if (dotdot == arg)
11221122
this = "HEAD";
1123-
if (!get_sha1(this, from_sha1) &&
1124-
!get_sha1(next, sha1)) {
1123+
if (!get_sha1_committish(this, from_sha1) &&
1124+
!get_sha1_committish(next, sha1)) {
11251125
struct commit *a, *b;
11261126
struct commit_list *exclude;
11271127

sha1_name.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
872872
struct strbuf sb;
873873
strbuf_init(&sb, dots - name);
874874
strbuf_add(&sb, name, dots - name);
875-
st = get_sha1(sb.buf, sha1_tmp);
875+
st = get_sha1_committish(sb.buf, sha1_tmp);
876876
strbuf_release(&sb);
877877
}
878878
if (st)
@@ -881,7 +881,7 @@ int get_sha1_mb(const char *name, unsigned char *sha1)
881881
if (!one)
882882
return -1;
883883

884-
if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
884+
if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
885885
return -1;
886886
two = lookup_commit_reference_gently(sha1_tmp, 0);
887887
if (!two)
@@ -999,6 +999,23 @@ int get_sha1(const char *name, unsigned char *sha1)
999999
return get_sha1_with_context(name, 0, sha1, &unused);
10001000
}
10011001

1002+
/*
1003+
* Many callers know that the user meant to name a committish by
1004+
* syntactical positions where the object name appears. Calling this
1005+
* function allows the machinery to disambiguate shorter-than-unique
1006+
* abbreviated object names between committish and others.
1007+
*
1008+
* Note that this does NOT error out when the named object is not a
1009+
* committish. It is merely to give a hint to the disambiguation
1010+
* machinery.
1011+
*/
1012+
int get_sha1_committish(const char *name, unsigned char *sha1)
1013+
{
1014+
struct object_context unused;
1015+
return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1016+
sha1, &unused);
1017+
}
1018+
10021019
/* Must be called only when object_name:filename doesn't exist. */
10031020
static void diagnose_invalid_sha1_path(const char *prefix,
10041021
const char *filename,

t/t1512-rev-parse-disambiguation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test_expect_failure 'disambiguate commit' '
102102
test $(git rev-parse $commit^) = $(git rev-parse 0000000000e4f)
103103
'
104104

105-
test_expect_failure 'log name1..name2 takes only commit-ishes on both ends' '
105+
test_expect_success 'log name1..name2 takes only commit-ishes on both ends' '
106106
git log 000000000..000000000 &&
107107
git log ..000000000 &&
108108
git log 000000000.. &&

0 commit comments

Comments
 (0)