Code

xdl_merge(): fix a segmentation fault when refining conflicts
[git.git] / commit.c
index 6ac3bf7554ec92cec2222c1f3a8f7d3565977688..59ea77c577886a3ba5b4ef385099d7a564af173c 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -1,21 +1,22 @@
 #include "cache.h"
 #include "tag.h"
 #include "commit.h"
+#include "pkt-line.h"
 
 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 +57,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 +87,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 +124,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)
@@ -221,6 +222,8 @@ static void prepare_commit_graft(void)
                return;
        graft_file = get_graft_file();
        read_graft_file(graft_file);
+       /* make sure shallows are read */
+       is_repository_shallow();
        commit_graft_prepared = 1;
 }
 
@@ -234,6 +237,37 @@ static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
        return commit_graft[pos];
 }
 
+int write_shallow_commits(int fd, int use_pack_protocol)
+{
+       int i, count = 0;
+       for (i = 0; i < commit_graft_nr; i++)
+               if (commit_graft[i]->nr_parent < 0) {
+                       const char *hex =
+                               sha1_to_hex(commit_graft[i]->sha1);
+                       count++;
+                       if (use_pack_protocol)
+                               packet_write(fd, "shallow %s", hex);
+                       else {
+                               write(fd, hex,  40);
+                               write(fd, "\n", 1);
+                       }
+               }
+       return count;
+}
+
+int unregister_shallow(const unsigned char *sha1)
+{
+       int pos = commit_graft_pos(sha1);
+       if (pos < 0)
+               return -1;
+       if (pos + 1 < commit_graft_nr)
+               memcpy(commit_graft + pos, commit_graft + pos + 1,
+                               sizeof(struct commit_graft *)
+                               * (commit_graft_nr - pos - 1));
+       commit_graft_nr--;
+       return 0;
+}
+
 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
 {
        char *tail = buffer;
@@ -467,7 +501,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;
@@ -507,14 +542,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' */
@@ -545,10 +582,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);
@@ -557,7 +597,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;
@@ -646,12 +689,14 @@ 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;
                }
 
@@ -727,10 +772,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;
 }
 
@@ -857,11 +902,11 @@ void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
 
 /* 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)
+/* bits #0..15 in revision.h */
+#define PARENT1                (1u<<16)
+#define PARENT2                (1u<<17)
+#define STALE          (1u<<18)
+#define RESULT         (1u<<19)
 
 static struct commit *interesting(struct commit_list *list)
 {
@@ -998,3 +1043,20 @@ struct commit_list *get_merge_bases(struct commit *one,
        free(rslt);
        return result;
 }
+
+int in_merge_bases(struct commit *rev1, struct commit *rev2)
+{
+       struct commit_list *bases, *b;
+       int ret = 0;
+
+       bases = get_merge_bases(rev1, rev2, 1);
+       for (b = bases; b; b = b->next) {
+               if (!hashcmp(rev1->object.sha1, b->item->object.sha1)) {
+                       ret = 1;
+                       break;
+               }
+       }
+
+       free_commit_list(bases);
+       return ret;
+}