]> asedeno.scripts.mit.edu Git - git.git/blobdiff - sha1_name.c
Merge branch 'pb/cvsserver'
[git.git] / sha1_name.c
index 739f1b2d49a6a87e3b5db3d8cadb151d27363a6a..491d2e7ebf19d5c582adbc5ece28d931b09b8a6d 100644 (file)
@@ -192,26 +192,25 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
 
 const char *find_unique_abbrev(const unsigned char *sha1, int len)
 {
-       int status, is_null;
+       int status, exists;
        static char hex[41];
 
-       is_null = is_null_sha1(sha1);
+       exists = has_sha1_file(sha1);
        memcpy(hex, sha1_to_hex(sha1), 40);
        if (len == 40 || !len)
                return hex;
        while (len < 40) {
                unsigned char sha1_ret[20];
                status = get_short_sha1(hex, len, sha1_ret, 1);
-               if (!status ||
-                   (is_null && status != SHORT_NAME_AMBIGUOUS)) {
+               if (exists
+                   ? !status
+                   : status == SHORT_NAME_NOT_FOUND) {
                        hex[len] = 0;
                        return hex;
                }
-               if (status != SHORT_NAME_AMBIGUOUS)
-                       return NULL;
                len++;
        }
-       return NULL;
+       return hex;
 }
 
 static int ambiguous_path(const char *path, int len)
@@ -408,18 +407,22 @@ static int get_nth_ancestor(const char *name, int len,
                            unsigned char *result, int generation)
 {
        unsigned char sha1[20];
-       int ret = get_sha1_1(name, len, sha1);
+       struct commit *commit;
+       int ret;
+
+       ret = get_sha1_1(name, len, sha1);
        if (ret)
                return ret;
+       commit = lookup_commit_reference(sha1);
+       if (!commit)
+               return -1;
 
        while (generation--) {
-               struct commit *commit = lookup_commit_reference(sha1);
-
-               if (!commit || parse_commit(commit) || !commit->parents)
+               if (parse_commit(commit) || !commit->parents)
                        return -1;
-               hashcpy(sha1, commit->parents->item->object.sha1);
+               commit = commit->parents->item;
        }
-       hashcpy(result, sha1);
+       hashcpy(result, commit->object.sha1);
        return 0;
 }
 
@@ -545,9 +548,8 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
        int ret, has_suffix;
        const char *cp;
 
-       /* "name~3" is "name^^^",
-        * "name~" and "name~0" are name -- not "name^0"!
-        * "name^" is not "name^0"; it is "name^1".
+       /*
+        * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
         */
        has_suffix = 0;
        for (cp = name + len - 1; name <= cp; cp--) {
@@ -565,11 +567,10 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
                cp++;
                while (cp < name + len)
                        num = num * 10 + *cp++ - '0';
-               if (has_suffix == '^') {
-                       if (!num && len1 == len - 1)
-                               num = 1;
+               if (!num && len1 == len - 1)
+                       num = 1;
+               if (has_suffix == '^')
                        return get_parent(name, len1, sha1, num);
-               }
                /* else if (has_suffix == '~') -- goes without saying */
                return get_nth_ancestor(name, len1, sha1, num);
        }
@@ -639,9 +640,9 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
                unsigned long size;
 
                commit = pop_most_recent_commit(&list, ONELINE_SEEN);
-               parse_object(commit->object.sha1);
-               if (temp_commit_buffer)
-                       free(temp_commit_buffer);
+               if (!parse_object(commit->object.sha1))
+                       continue;
+               free(temp_commit_buffer);
                if (commit->buffer)
                        p = commit->buffer;
                else {
@@ -658,8 +659,7 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
                        break;
                }
        }
-       if (temp_commit_buffer)
-               free(temp_commit_buffer);
+       free(temp_commit_buffer);
        free_commit_list(list);
        for (l = backup; l; l = l->next)
                clear_commit_marks(l->item, ONELINE_SEEN);