Code

Run "git repack -a -d" once more at end, if there's 1MB or more of not-packed data.
[git.git] / commit.c
index 17f51c245525172695e6ff6d36990464349558e0..a6d543eee7831cd6a479d4cf8b2ba81a1438b298 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -7,15 +7,15 @@ int save_commit_buffer = 1;
 struct sort_node
 {
        /*
-         * the number of children of the associated commit
-         * that also occur in the list being sorted.
-         */
+        * the number of children of the associated commit
+        * that also occur in the list being sorted.
+        */
        unsigned int indegree;
 
        /*
-         * reference to original list item that we will re-use
-         * on output.
-         */
+        * reference to original list item that we will re-use
+        * on output.
+        */
        struct commit_list * list_item;
 
 };
@@ -56,7 +56,7 @@ static struct commit *check_commit(struct object *obj,
                                   const unsigned char *sha1,
                                   int quiet)
 {
-       if (obj->type != TYPE_COMMIT) {
+       if (obj->type != OBJ_COMMIT) {
                if (!quiet)
                        error("Object %s is a %s, not a commit",
                              sha1_to_hex(sha1), typename(obj->type));
@@ -86,11 +86,11 @@ struct commit *lookup_commit(const unsigned char *sha1)
        if (!obj) {
                struct commit *ret = alloc_commit_node();
                created_object(sha1, &ret->object);
-               ret->object.type = TYPE_COMMIT;
+               ret->object.type = OBJ_COMMIT;
                return ret;
        }
        if (!obj->type)
-               obj->type = TYPE_COMMIT;
+               obj->type = OBJ_COMMIT;
        return check_commit(obj, sha1, 0);
 }
 
@@ -123,7 +123,7 @@ static int commit_graft_pos(const unsigned char *sha1)
        while (lo < hi) {
                int mi = (lo + hi) / 2;
                struct commit_graft *graft = commit_graft[mi];
-               int cmp = memcmp(sha1, graft->sha1, 20);
+               int cmp = hashcmp(sha1, graft->sha1);
                if (!cmp)
                        return mi;
                if (cmp < 0)
@@ -163,14 +163,6 @@ int register_commit_graft(struct commit_graft *graft, int ignore_dups)
        return 0;
 }
 
-void free_commit_grafts(void)
-{
-       int pos = commit_graft_nr;
-       while (pos >= 0)
-               free(commit_graft[pos--]);
-       commit_graft_nr = 0;
-}
-
 struct commit_graft *read_graft_line(char *buf, int len)
 {
        /* The format is just "Commit Parent1 Parent2 ...\n" */
@@ -223,18 +215,11 @@ int read_graft_file(const char *graft_file)
 static void prepare_commit_graft(void)
 {
        static int commit_graft_prepared;
-       static char *last_graft_file;
-       char *graft_file = get_graft_file();
-
-       if (last_graft_file) {
-               if (!strcmp(graft_file, last_graft_file))
-                       return;
-               free_commit_grafts();
-       }
-       if (last_graft_file)
-               free(last_graft_file);
-       last_graft_file = strdup(graft_file);
+       char *graft_file;
 
+       if (commit_graft_prepared)
+               return;
+       graft_file = get_graft_file();
        read_graft_file(graft_file);
        commit_graft_prepared = 1;
 }
@@ -412,12 +397,13 @@ void clear_commit_marks(struct commit *commit, unsigned int mark)
 {
        struct commit_list *parents;
 
-       parents = commit->parents;
        commit->object.flags &= ~mark;
+       parents = commit->parents;
        while (parents) {
                struct commit *parent = parents->item;
-               if (parent && parent->object.parsed &&
-                   (parent->object.flags & mark))
+
+               /* Have we already cleared this? */
+               if (mark & parent->object.flags)
                        clear_commit_marks(parent, mark);
                parents = parents->next;
        }
@@ -481,7 +467,8 @@ static int add_rfc2047(char *buf, const char *line, int len)
        return bp - buf;
 }
 
-static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
+static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
+                        const char *line, int relative_date)
 {
        char *date;
        int namelen;
@@ -521,14 +508,16 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
        }
        switch (fmt) {
        case CMIT_FMT_MEDIUM:
-               ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz));
+               ret += sprintf(buf + ret, "Date:   %s\n",
+                              show_date(time, tz, relative_date));
                break;
        case CMIT_FMT_EMAIL:
                ret += sprintf(buf + ret, "Date: %s\n",
                               show_rfc2822_date(time, tz));
                break;
        case CMIT_FMT_FULLER:
-               ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
+               ret += sprintf(buf + ret, "%sDate: %s\n", what,
+                              show_date(time, tz, relative_date));
                break;
        default:
                /* notin' */
@@ -559,10 +548,13 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
 
        while (parent) {
                struct commit *p = parent->item;
-               const char *hex = abbrev
-                       ? find_unique_abbrev(p->object.sha1, abbrev)
-                       : sha1_to_hex(p->object.sha1);
-               const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
+               const char *hex = NULL;
+               const char *dots;
+               if (abbrev)
+                       hex = find_unique_abbrev(p->object.sha1, abbrev);
+               if (!hex)
+                       hex = sha1_to_hex(p->object.sha1);
+               dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
                parent = parent->next;
 
                offset += sprintf(buf + offset, " %s%s", hex, dots);
@@ -571,7 +563,10 @@ static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *com
        return offset;
 }
 
-unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject)
+unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
+                                 unsigned long len, char *buf, unsigned long space,
+                                 int abbrev, const char *subject,
+                                 const char *after_subject, int relative_date)
 {
        int hdr = 1, body = 0;
        unsigned long offset = 0;
@@ -660,15 +655,20 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
                        if (!memcmp(line, "author ", 7))
                                offset += add_user_info("Author", fmt,
                                                        buf + offset,
-                                                       line + 7);
+                                                       line + 7,
+                                                       relative_date);
                        if (!memcmp(line, "committer ", 10) &&
                            (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
                                offset += add_user_info("Commit", fmt,
                                                        buf + offset,
-                                                       line + 10);
+                                                       line + 10,
+                                                       relative_date);
                        continue;
                }
 
+               if (!subject)
+                       body = 1;
+
                if (is_empty_line(line, &linelen)) {
                        if (!body)
                                continue;
@@ -676,8 +676,6 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
                                continue;
                        if (fmt == CMIT_FMT_SHORT)
                                break;
-               } else {
-                       body = 1;
                }
 
                if (subject) {
@@ -716,6 +714,12 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit
        /* Make sure there is an EOLN for the non-oneline case */
        if (fmt != CMIT_FMT_ONELINE)
                buf[offset++] = '\n';
+       /*
+        * make sure there is another EOLN to separate the headers from whatever
+        * body the caller appends if we haven't already written a body
+        */
+       if (fmt == CMIT_FMT_EMAIL && !body)
+               buf[offset++] = '\n';
        buf[offset] = '\0';
        return offset;
 }
@@ -734,10 +738,10 @@ struct commit *pop_commit(struct commit_list **stack)
 
 int count_parents(struct commit * commit)
 {
-        int count = 0;
+        int count;
         struct commit_list * parents = commit->parents;
-        for (count=0;parents; parents=parents->next,count++)
-          ;
+        for (count = 0; parents; parents = parents->next,count++)
+               ;
         return count;
 }
 
@@ -861,3 +865,147 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
        }
        free(nodes);
 }
+
+/* merge-rebase stuff */
+
+/* bits #0..7 in revision.h */
+#define PARENT1                (1u<< 8)
+#define PARENT2                (1u<< 9)
+#define STALE          (1u<<10)
+#define RESULT         (1u<<11)
+
+static struct commit *interesting(struct commit_list *list)
+{
+       while (list) {
+               struct commit *commit = list->item;
+               list = list->next;
+               if (commit->object.flags & STALE)
+                       continue;
+               return commit;
+       }
+       return NULL;
+}
+
+static struct commit_list *merge_bases(struct commit *one, struct commit *two)
+{
+       struct commit_list *list = NULL;
+       struct commit_list *result = NULL;
+
+       if (one == two)
+               /* We do not mark this even with RESULT so we do not
+                * have to clean it up.
+                */
+               return commit_list_insert(one, &result);
+
+       parse_commit(one);
+       parse_commit(two);
+
+       one->object.flags |= PARENT1;
+       two->object.flags |= PARENT2;
+       insert_by_date(one, &list);
+       insert_by_date(two, &list);
+
+       while (interesting(list)) {
+               struct commit *commit;
+               struct commit_list *parents;
+               struct commit_list *n;
+               int flags;
+
+               commit = list->item;
+               n = list->next;
+               free(list);
+               list = n;
+
+               flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
+               if (flags == (PARENT1 | PARENT2)) {
+                       if (!(commit->object.flags & RESULT)) {
+                               commit->object.flags |= RESULT;
+                               insert_by_date(commit, &result);
+                       }
+                       /* Mark parents of a found merge stale */
+                       flags |= STALE;
+               }
+               parents = commit->parents;
+               while (parents) {
+                       struct commit *p = parents->item;
+                       parents = parents->next;
+                       if ((p->object.flags & flags) == flags)
+                               continue;
+                       parse_commit(p);
+                       p->object.flags |= flags;
+                       insert_by_date(p, &list);
+               }
+       }
+
+       /* Clean up the result to remove stale ones */
+       list = result; result = NULL;
+       while (list) {
+               struct commit_list *n = list->next;
+               if (!(list->item->object.flags & STALE))
+                       insert_by_date(list->item, &result);
+               free(list);
+               list = n;
+       }
+       return result;
+}
+
+struct commit_list *get_merge_bases(struct commit *one,
+                                   struct commit *two,
+                                    int cleanup)
+{
+       const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
+       struct commit_list *list;
+       struct commit **rslt;
+       struct commit_list *result;
+       int cnt, i, j;
+
+       result = merge_bases(one, two);
+       if (one == two)
+               return result;
+       if (!result || !result->next) {
+               if (cleanup) {
+                       clear_commit_marks(one, all_flags);
+                       clear_commit_marks(two, all_flags);
+               }
+               return result;
+       }
+
+       /* There are more than one */
+       cnt = 0;
+       list = result;
+       while (list) {
+               list = list->next;
+               cnt++;
+       }
+       rslt = xcalloc(cnt, sizeof(*rslt));
+       for (list = result, i = 0; list; list = list->next)
+               rslt[i++] = list->item;
+       free_commit_list(result);
+
+       clear_commit_marks(one, all_flags);
+       clear_commit_marks(two, all_flags);
+       for (i = 0; i < cnt - 1; i++) {
+               for (j = i+1; j < cnt; j++) {
+                       if (!rslt[i] || !rslt[j])
+                               continue;
+                       result = merge_bases(rslt[i], rslt[j]);
+                       clear_commit_marks(rslt[i], all_flags);
+                       clear_commit_marks(rslt[j], all_flags);
+                       for (list = result; list; list = list->next) {
+                               if (rslt[i] == list->item)
+                                       rslt[i] = NULL;
+                               if (rslt[j] == list->item)
+                                       rslt[j] = NULL;
+                       }
+               }
+       }
+
+       /* Surviving ones in rslt[] are the independent results */
+       result = NULL;
+       for (i = 0; i < cnt; i++) {
+               if (rslt[i])
+                       insert_by_date(rslt[i], &result);
+       }
+       free(rslt);
+       return result;
+}