Skip to content

Commit 33bd598

Browse files
committed
sha1_name.c: teach lookup context to get_sha1_with_context()
The function takes user input string and returns the object name (binary SHA-1) with mode bits and path when the object was looked up in a tree. Additionally give hints to help disambiguation of abbreviated object names when the caller knows what it is looking for. Signed-off-by: Junio C Hamano <[email protected]>
1 parent e264361 commit 33bd598

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

builtin/cat-file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
9090
unsigned long size;
9191
struct object_context obj_context;
9292

93-
if (get_sha1_with_context(obj_name, sha1, &obj_context))
93+
if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
9494
die("Not a valid object name %s", obj_name);
9595

9696
buf = NULL;

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,11 @@ struct object_context {
814814
#define GET_SHA1_QUIETLY 01
815815
#define GET_SHA1_COMMIT 02
816816
#define GET_SHA1_COMMITTISH 04
817+
#define GET_SHA1_ONLY_TO_DIE 04000
817818

818819
extern int get_sha1(const char *str, unsigned char *sha1);
819820
extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix);
820-
extern int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc);
821+
extern int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc);
821822

822823
/*
823824
* Try to read a SHA1 in hexadecimal format from the 40 characters

revision.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs,
11801180
local_flags = UNINTERESTING;
11811181
arg++;
11821182
}
1183-
if (get_sha1_with_context(arg, sha1, &oc))
1183+
if (get_sha1_with_context(arg, 0, sha1, &oc))
11841184
return revs->ignore_missing ? 0 : -1;
11851185
if (!cant_be_filename)
11861186
verify_non_filename(revs->prefix, arg);
@@ -1795,7 +1795,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
17951795
unsigned char sha1[20];
17961796
struct object *object;
17971797
struct object_context oc;
1798-
if (get_sha1_with_context(revs->def, sha1, &oc))
1798+
if (get_sha1_with_context(revs->def, 0, sha1, &oc))
17991799
die("bad default revision '%s'", revs->def);
18001800
object = get_reference(revs, revs->def, sha1, 0);
18011801
add_pending_object_with_mode(revs, object, revs->def, oc.mode);

sha1_name.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
996996
int get_sha1(const char *name, unsigned char *sha1)
997997
{
998998
struct object_context unused;
999-
return get_sha1_with_context(name, sha1, &unused);
999+
return get_sha1_with_context(name, 0, sha1, &unused);
10001000
}
10011001

10021002
/* Must be called only when object_name:filename doesn't exist. */
@@ -1112,20 +1112,24 @@ static char *resolve_relative_path(const char *rel)
11121112
rel);
11131113
}
11141114

1115-
static int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1116-
struct object_context *oc,
1117-
int only_to_die, const char *prefix)
1115+
static int get_sha1_with_context_1(const char *name,
1116+
unsigned flags,
1117+
const char *prefix,
1118+
unsigned char *sha1,
1119+
struct object_context *oc)
11181120
{
11191121
int ret, bracket_depth;
11201122
int namelen = strlen(name);
11211123
const char *cp;
1124+
int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
11221125

11231126
memset(oc, 0, sizeof(*oc));
11241127
oc->mode = S_IFINVALID;
1125-
ret = get_sha1_1(name, namelen, sha1, 0);
1128+
ret = get_sha1_1(name, namelen, sha1, flags);
11261129
if (!ret)
11271130
return ret;
1128-
/* sha1:path --> object name of path in ent sha1
1131+
/*
1132+
* sha1:path --> object name of path in ent sha1
11291133
* :path -> object name of absolute path in index
11301134
* :./path -> object name of path relative to cwd in index
11311135
* :[0-3]:path -> object name of path in index at stage
@@ -1239,10 +1243,10 @@ void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
12391243
{
12401244
struct object_context oc;
12411245
unsigned char sha1[20];
1242-
get_sha1_with_context_1(name, sha1, &oc, 1, prefix);
1246+
get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
12431247
}
12441248

1245-
int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
1249+
int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc)
12461250
{
1247-
return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
1251+
return get_sha1_with_context_1(str, flags, NULL, sha1, orc);
12481252
}

0 commit comments

Comments
 (0)