sha1_name.c: rename "now" to "current"
[gitweb.git] / sha1_name.c
index ff5992acc971ac5a67fa3d4def1e0c064b90519f..c5c4591f98989e48b13f2625a9f09f09e546a2f8 100644 (file)
@@ -17,6 +17,13 @@ static int find_short_object_filename(int len, const char *name, unsigned char *
        static struct alternate_object_database *fakeent;
 
        if (!fakeent) {
+               /*
+                * Create a "fake" alternate object database that
+                * points to our own object database, to make it
+                * easier to get a temporary working space in
+                * alt->name/alt->base while iterating over the
+                * object databases including our own.
+                */
                const char *objdir = get_object_directory();
                int objdir_len = strlen(objdir);
                int entlen = objdir_len + 43;
@@ -86,11 +93,11 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
                last = num;
                while (first < last) {
                        uint32_t mid = (first + last) / 2;
-                       const unsigned char *now;
+                       const unsigned char *current;
                        int cmp;
 
-                       now = nth_packed_object_sha1(p, mid);
-                       cmp = hashcmp(match, now);
+                       current = nth_packed_object_sha1(p, mid);
+                       cmp = hashcmp(match, current);
                        if (!cmp) {
                                first = mid;
                                break;
@@ -102,17 +109,17 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
                        last = mid;
                }
                if (first < num) {
-                       const unsigned char *now, *next;
-                      now = nth_packed_object_sha1(p, first);
-                       if (match_sha(len, match, now)) {
+                       const unsigned char *current, *next;
+                       current = nth_packed_object_sha1(p, first);
+                       if (match_sha(len, match, current)) {
                                next = nth_packed_object_sha1(p, first+1);
-                              if (!next|| !match_sha(len, match, next)) {
+                               if (!next|| !match_sha(len, match, next)) {
                                        /* unique within this pack */
                                        if (!found) {
-                                               found_sha1 = now;
+                                               found_sha1 = current;
                                                found++;
                                        }
-                                       else if (hashcmp(found_sha1, now)) {
+                                       else if (hashcmp(found_sha1, current)) {
                                                found = 2;
                                                break;
                                        }
@@ -241,91 +248,6 @@ static int ambiguous_path(const char *path, int len)
        return slash;
 }
 
-/*
- * *string and *len will only be substituted, and *string returned (for
- * later free()ing) if the string passed in is a magic short-hand form
- * to name a branch.
- */
-static char *substitute_branch_name(const char **string, int *len)
-{
-       struct strbuf buf = STRBUF_INIT;
-       int ret = interpret_branch_name(*string, &buf);
-
-       if (ret == *len) {
-               size_t size;
-               *string = strbuf_detach(&buf, &size);
-               *len = size;
-               return (char *)*string;
-       }
-
-       return NULL;
-}
-
-int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
-{
-       char *last_branch = substitute_branch_name(&str, &len);
-       const char **p, *r;
-       int refs_found = 0;
-
-       *ref = NULL;
-       for (p = ref_rev_parse_rules; *p; p++) {
-               char fullref[PATH_MAX];
-               unsigned char sha1_from_ref[20];
-               unsigned char *this_result;
-               int flag;
-
-               this_result = refs_found ? sha1_from_ref : sha1;
-               mksnpath(fullref, sizeof(fullref), *p, len, str);
-               r = resolve_ref(fullref, this_result, 1, &flag);
-               if (r) {
-                       if (!refs_found++)
-                               *ref = xstrdup(r);
-                       if (!warn_ambiguous_refs)
-                               break;
-               } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
-                       warning("ignoring dangling symref %s.", fullref);
-       }
-       free(last_branch);
-       return refs_found;
-}
-
-int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
-{
-       char *last_branch = substitute_branch_name(&str, &len);
-       const char **p;
-       int logs_found = 0;
-
-       *log = NULL;
-       for (p = ref_rev_parse_rules; *p; p++) {
-               struct stat st;
-               unsigned char hash[20];
-               char path[PATH_MAX];
-               const char *ref, *it;
-
-               mksnpath(path, sizeof(path), *p, len, str);
-               ref = resolve_ref(path, hash, 1, NULL);
-               if (!ref)
-                       continue;
-               if (!stat(git_path("logs/%s", path), &st) &&
-                   S_ISREG(st.st_mode))
-                       it = path;
-               else if (strcmp(ref, path) &&
-                        !stat(git_path("logs/%s", ref), &st) &&
-                        S_ISREG(st.st_mode))
-                       it = ref;
-               else
-                       continue;
-               if (!logs_found++) {
-                       *log = xstrdup(it);
-                       hashcpy(sha1, hash);
-               }
-               if (!warn_ambiguous_refs)
-                       break;
-       }
-       free(last_branch);
-       return logs_found;
-}
-
 static inline int upstream_mark(const char *string, int len)
 {
        const char *suffix[] = { "@{upstream}", "@{u}" };
@@ -501,12 +423,6 @@ struct object *peel_to_type(const char *name, int namelen,
 {
        if (name && !namelen)
                namelen = strlen(name);
-       if (!o) {
-               unsigned char sha1[20];
-               if (get_sha1_1(name, namelen, sha1))
-                       return NULL;
-               o = parse_object(sha1);
-       }
        while (1) {
                if (!o || (!o->parsed && !parse_object(o->sha1)))
                        return NULL;
@@ -972,9 +888,9 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 {
        strbuf_branchname(sb, name);
        if (name[0] == '-')
-               return CHECK_REF_FORMAT_ERROR;
+               return -1;
        strbuf_splice(sb, 0, 0, "refs/heads/", 11);
-       return check_ref_format(sb->buf);
+       return check_refname_format(sb->buf, 0);
 }
 
 /*
@@ -1083,16 +999,6 @@ static void diagnose_invalid_index_path(int stage,
 }
 
 
-int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
-                        int only_to_die, const char *prefix)
-{
-       struct object_context oc;
-       int ret;
-       ret = get_sha1_with_context_1(name, sha1, &oc, only_to_die, prefix);
-       *mode = oc.mode;
-       return ret;
-}
-
 static char *resolve_relative_path(const char *rel)
 {
        if (prefixcmp(rel, "./") && prefixcmp(rel, "../"))
@@ -1110,9 +1016,9 @@ static char *resolve_relative_path(const char *rel)
                           rel);
 }
 
-int get_sha1_with_context_1(const char *name, unsigned char *sha1,
-                           struct object_context *oc,
-                           int only_to_die, const char *prefix)
+static int get_sha1_with_context_1(const char *name, unsigned char *sha1,
+                                  struct object_context *oc,
+                                  int only_to_die, const char *prefix)
 {
        int ret, bracket_depth;
        int namelen = strlen(name);
@@ -1225,3 +1131,22 @@ int get_sha1_with_context_1(const char *name, unsigned char *sha1,
        }
        return ret;
 }
+
+/*
+ * Call this function when you know "name" given by the end user must
+ * name an object but it doesn't; the function _may_ die with a better
+ * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
+ * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
+ * you have a chance to diagnose the error further.
+ */
+void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
+{
+       struct object_context oc;
+       unsigned char sha1[20];
+       get_sha1_with_context_1(name, sha1, &oc, 1, prefix);
+}
+
+int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
+{
+       return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
+}