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 <gitster@pobox.com>

Junio C Hamano committed Jul 2, 2012 at 10:32 UTC 33bd598c3902c40c5a10a68aeaa3004484239258
4 files changed +18 -13
builtin/cat-file.c
+1 -1
@@ -90,7 +90,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
90 unsigned long size;
91 struct object_context obj_context;
92
93 - if (get_sha1_with_context(obj_name, sha1, &obj_context))
93 + if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
94 die("Not a valid object name %s", obj_name);
95
96 buf = NULL;
cache.h
+2 -1
@@ -814,10 +814,11 @@ struct object_context {
814 #define GET_SHA1_QUIETLY 01
815 #define GET_SHA1_COMMIT 02
816 #define GET_SHA1_COMMITTISH 04
817 +#define GET_SHA1_ONLY_TO_DIE 04000
818
819 extern int get_sha1(const char *str, unsigned char *sha1);
820 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);
822
823 /*
824 * Try to read a SHA1 in hexadecimal format from the 40 characters
revision.c
+2 -2
@@ -1180,7 +1180,7 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs,
1180 local_flags = UNINTERESTING;
1181 arg++;
1182 }
1183 - if (get_sha1_with_context(arg, sha1, &oc))
1183 + if (get_sha1_with_context(arg, 0, sha1, &oc))
1184 return revs->ignore_missing ? 0 : -1;
1185 if (!cant_be_filename)
1186 verify_non_filename(revs->prefix, arg);
@@ -1795,7 +1795,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
1795 unsigned char sha1[20];
1796 struct object *object;
1797 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))
1799 die("bad default revision '%s'", revs->def);
1800 object = get_reference(revs, revs->def, sha1, 0);
1801 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
sha1_name.c
+13 -9
@@ -996,7 +996,7 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
996 int get_sha1(const char *name, unsigned char *sha1)
997 {
998 struct object_context unused;
999 - return get_sha1_with_context(name, sha1, &unused);
999 + return get_sha1_with_context(name, 0, sha1, &unused);
1000 }
1001
1002 /* Must be called only when object_name:filename doesn't exist. */
@@ -1112,20 +1112,24 @@ static char *resolve_relative_path(const char *rel)
1112 rel);
1113 }
1114
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)
1120 {
1121 int ret, bracket_depth;
1122 int namelen = strlen(name);
1123 const char *cp;
1124 + int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
1125
1126 memset(oc, 0, sizeof(*oc));
1127 oc->mode = S_IFINVALID;
1125 - ret = get_sha1_1(name, namelen, sha1, 0);
1128 + ret = get_sha1_1(name, namelen, sha1, flags);
1129 if (!ret)
1130 return ret;
1128 - /* sha1:path --> object name of path in ent sha1
1131 + /*
1132 + * sha1:path --> object name of path in ent sha1
1133 * :path -> object name of absolute path in index
1134 * :./path -> object name of path relative to cwd in index
1135 * :[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)
1243 {
1244 struct object_context oc;
1245 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);
1247 }
1248
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)
1250 {
1247 - return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
1251 + return get_sha1_with_context_1(str, flags, NULL, sha1, orc);
1252 }