rev-parse: fix meaning of rev~ vs rev~0.
[gitweb.git] / sha1_name.c
index 858f08c34a2de917b630d02974a839d626d23e7f..6c9c1c75adefe2d5e38212b75302368293c9137b 100644 (file)
@@ -239,23 +239,13 @@ static int ambiguous_path(const char *path, int len)
        return slash;
 }
 
-static const char *ref_fmt[] = {
-       "%.*s",
-       "refs/%.*s",
-       "refs/tags/%.*s",
-       "refs/heads/%.*s",
-       "refs/remotes/%.*s",
-       "refs/remotes/%.*s/HEAD",
-       NULL
-};
-
 int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
 {
        const char **p, *r;
        int refs_found = 0;
 
        *ref = NULL;
-       for (p = ref_fmt; *p; p++) {
+       for (p = ref_rev_parse_rules; *p; p++) {
                unsigned char sha1_from_ref[20];
                unsigned char *this_result;
 
@@ -277,7 +267,7 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
        int logs_found = 0;
 
        *log = NULL;
-       for (p = ref_fmt; *p; p++) {
+       for (p = ref_rev_parse_rules; *p; p++) {
                struct stat st;
                unsigned char hash[20];
                char path[PATH_MAX];
@@ -370,7 +360,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
                                fprintf(stderr,
                                        "warning: Log for '%.*s' only goes "
                                        "back to %s.\n", len, str,
-                                       show_rfc2822_date(co_time, co_tz));
+                                       show_date(co_time, co_tz, DATE_RFC2822));
                        else
                                fprintf(stderr,
                                        "warning: Log for '%.*s' only has "
@@ -418,18 +408,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;
 }
 
@@ -536,9 +530,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--) {
@@ -556,11 +549,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);
        }
@@ -610,24 +602,35 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
 {
        struct commit_list *list = NULL, *backup = NULL, *l;
        int retval = -1;
+       char *temp_commit_buffer = NULL;
 
        if (prefix[0] == '!') {
                if (prefix[1] != '!')
                        die ("Invalid search pattern: %s", prefix);
                prefix++;
        }
-       if (!save_commit_buffer)
-               return error("Could not expand oneline-name.");
        for_each_ref(handle_one_ref, &list);
        for (l = list; l; l = l->next)
                commit_list_insert(l->item, &backup);
        while (list) {
                char *p;
                struct commit *commit;
+               enum object_type type;
+               unsigned long size;
 
                commit = pop_most_recent_commit(&list, ONELINE_SEEN);
                parse_object(commit->object.sha1);
-               if (!commit->buffer || !(p = strstr(commit->buffer, "\n\n")))
+               if (temp_commit_buffer)
+                       free(temp_commit_buffer);
+               if (commit->buffer)
+                       p = commit->buffer;
+               else {
+                       p = read_sha1_file(commit->object.sha1, &type, &size);
+                       if (!p)
+                               continue;
+                       temp_commit_buffer = p;
+               }
+               if (!(p = strstr(p, "\n\n")))
                        continue;
                if (!prefixcmp(p + 2, prefix)) {
                        hashcpy(sha1, commit->object.sha1);
@@ -635,6 +638,8 @@ static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
                        break;
                }
        }
+       if (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);