rerere: explain the rerere I/O abstraction
[gitweb.git] / rerere.c
index e307711f817a9e599d2a597e59d1c87be2fb7eed..7db5b54838e1c26e4fecb4341ef0a67d8b98f9ce 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -35,32 +35,27 @@ static int has_rerere_resolution(const char *hex)
 
 static void read_rr(struct string_list *rr)
 {
-       unsigned char sha1[20];
-       char buf[PATH_MAX];
+       struct strbuf buf = STRBUF_INIT;
        FILE *in = fopen(merge_rr_path, "r");
+
        if (!in)
                return;
-       while (fread(buf, 40, 1, in) == 1) {
-               int i;
-               char *name;
-               if (get_sha1_hex(buf, sha1))
+       while (!strbuf_getwholeline(&buf, in, '\0')) {
+               char *path;
+               unsigned char sha1[20];
+
+               /* There has to be the hash, tab, path and then NUL */
+               if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
                        die("corrupt MERGE_RR");
-               buf[40] = '\0';
-               name = xstrdup(buf);
-               if (fgetc(in) != '\t')
+
+               if (buf.buf[40] != '\t')
                        die("corrupt MERGE_RR");
-               for (i = 0; i < sizeof(buf); i++) {
-                       int c = fgetc(in);
-                       if (c < 0)
-                               die("corrupt MERGE_RR");
-                       buf[i] = c;
-                       if (c == 0)
-                                break;
-               }
-               if (i == sizeof(buf))
-                       die("filename too long");
-               string_list_insert(rr, buf)->util = name;
+               buf.buf[40] = '\0';
+               path = buf.buf + 41;
+
+               string_list_insert(rr, path)->util = xstrdup(buf.buf);
        }
+       strbuf_release(&buf);
        fclose(in);
 }
 
@@ -70,22 +65,39 @@ static int write_rr(struct string_list *rr, int out_fd)
 {
        int i;
        for (i = 0; i < rr->nr; i++) {
-               const char *path;
-               int length;
+               struct strbuf buf = STRBUF_INIT;
+
+               assert(rr->items[i].util != RERERE_RESOLVED);
                if (!rr->items[i].util)
                        continue;
-               path = rr->items[i].string;
-               length = strlen(path) + 1;
-               if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
-                   write_str_in_full(out_fd, "\t") != 1 ||
-                   write_in_full(out_fd, path, length) != length)
+               strbuf_addf(&buf, "%s\t%s%c",
+                           (char *)rr->items[i].util,
+                           rr->items[i].string, 0);
+               if (write_in_full(out_fd, buf.buf, buf.len) != buf.len)
                        die("unable to write rerere record");
+
+               strbuf_release(&buf);
        }
        if (commit_lock_file(&write_lock) != 0)
                die("unable to write rerere record");
        return 0;
 }
 
+/*
+ * "rerere" interacts with conflicted file contents using this I/O
+ * abstraction.  It reads a conflicted contents from one place via
+ * "getline()" method, and optionally can write it out after
+ * normalizing the conflicted hunks to the "output".  Subclasses of
+ * rerere_io embed this structure at the beginning of their own
+ * rerere_io object.
+ */
+struct rerere_io {
+       int (*getline)(struct strbuf *, struct rerere_io *);
+       FILE *output;
+       int wrerror;
+       /* some more stuff */
+};
+
 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
 {
        if (!count || *err)
@@ -99,19 +111,15 @@ static inline void ferr_puts(const char *s, FILE *fp, int *err)
        ferr_write(s, strlen(s), fp, err);
 }
 
-struct rerere_io {
-       int (*getline)(struct strbuf *, struct rerere_io *);
-       FILE *output;
-       int wrerror;
-       /* some more stuff */
-};
-
 static void rerere_io_putstr(const char *str, struct rerere_io *io)
 {
        if (io->output)
                ferr_puts(str, io->output, &io->wrerror);
 }
 
+/*
+ * Write a conflict marker to io->output (if defined).
+ */
 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
 {
        char buf[64];
@@ -140,19 +148,42 @@ static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
                ferr_write(mem, sz, io->output, &io->wrerror);
 }
 
+/*
+ * Subclass of rerere_io that reads from an on-disk file
+ */
 struct rerere_io_file {
        struct rerere_io io;
        FILE *input;
 };
 
+/*
+ * ... and its getline() method implementation
+ */
 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
 {
        struct rerere_io_file *io = (struct rerere_io_file *)io_;
        return strbuf_getwholeline(sb, io->input, '\n');
 }
 
-static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
+/*
+ * Require the exact number of conflict marker letters, no more, no
+ * less, followed by SP or any whitespace
+ * (including LF).
+ */
+static int is_cmarker(char *buf, int marker_char, int marker_size)
 {
+       int want_sp;
+
+       /*
+        * The beginning of our version and the end of their version
+        * always are labeled like "<<<<< ours" or ">>>>> theirs",
+        * hence we set want_sp for them.  Note that the version from
+        * the common ancestor in diff3-style output is not always
+        * labelled (e.g. "||||| common" is often seen but "|||||"
+        * alone is also valid), so we do not set want_sp.
+        */
+       want_sp = (marker_char == '<') || (marker_char == '>');
+
        while (marker_size--)
                if (*buf++ != marker_char)
                        return 0;
@@ -175,19 +206,19 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
                git_SHA1_Init(&ctx);
 
        while (!io->getline(&buf, io)) {
-               if (is_cmarker(buf.buf, '<', marker_size, 1)) {
+               if (is_cmarker(buf.buf, '<', marker_size)) {
                        if (hunk != RR_CONTEXT)
                                goto bad;
                        hunk = RR_SIDE_1;
-               } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
+               } else if (is_cmarker(buf.buf, '|', marker_size)) {
                        if (hunk != RR_SIDE_1)
                                goto bad;
                        hunk = RR_ORIGINAL;
-               } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
+               } else if (is_cmarker(buf.buf, '=', marker_size)) {
                        if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
                                goto bad;
                        hunk = RR_SIDE_2;
-               } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
+               } else if (is_cmarker(buf.buf, '>', marker_size)) {
                        if (hunk != RR_SIDE_2)
                                goto bad;
                        if (strbuf_cmp(&one, &two) > 0)
@@ -272,11 +303,18 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
        return hunk_no;
 }
 
+/*
+ * Subclass of rerere_io that reads from an in-core buffer that is a
+ * strbuf
+ */
 struct rerere_io_mem {
        struct rerere_io io;
        struct strbuf input;
 };
 
+/*
+ * ... and its getline() method implementation
+ */
 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
 {
        struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
@@ -315,24 +353,23 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
                return -1;
        pos = -pos - 1;
 
-       for (i = 0; i < 3; i++) {
+       while (pos < active_nr) {
                enum object_type type;
                unsigned long size;
-               int j;
 
-               if (active_nr <= pos)
-                       break;
                ce = active_cache[pos++];
                if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
-                       continue;
-               j = ce_stage(ce) - 1;
-               mmfile[j].ptr = read_sha1_file(ce->sha1, &type, &size);
-               mmfile[j].size = size;
+                       break;
+               i = ce_stage(ce) - 1;
+               if (!mmfile[i].ptr) {
+                       mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
+                       mmfile[i].size = size;
+               }
        }
-       for (i = 0; i < 3; i++) {
+       for (i = 0; i < 3; i++)
                if (!mmfile[i].ptr && !mmfile[i].size)
                        mmfile[i].ptr = xstrdup("");
-       }
+
        /*
         * NEEDSWORK: handle conflicts from merges with
         * merge.renormalize set, too
@@ -369,7 +406,7 @@ static int check_one_conflict(int i, int *type)
        }
 
        *type = PUNTED;
-       if (ce_stage(e) == 1)
+       while (ce_stage(active_cache[i]) == 1)
                i++;
 
        /* Only handle regular files with both stages #2 and #3 */
@@ -485,6 +522,8 @@ static void update_paths(struct string_list *update)
                struct string_list_item *item = &update->items[i];
                if (add_file_to_cache(item->string, 0))
                        exit(128);
+               fprintf(stderr, "Staged '%s' using previous resolution.\n",
+                       item->string);
        }
 
        if (active_cache_changed) {
@@ -539,16 +578,16 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                const char *name = (const char *)rr->items[i].util;
 
                if (has_rerere_resolution(name)) {
-                       if (!merge(name, path)) {
-                               const char *msg;
-                               if (rerere_autoupdate) {
-                                       string_list_insert(&update, path);
-                                       msg = "Staged '%s' using previous resolution.\n";
-                               } else
-                                       msg = "Resolved '%s' using previous resolution.\n";
-                               fprintf(stderr, msg, path);
-                               goto mark_resolved;
-                       }
+                       if (merge(name, path))
+                               continue;
+
+                       if (rerere_autoupdate)
+                               string_list_insert(&update, path);
+                       else
+                               fprintf(stderr,
+                                       "Resolved '%s' using previous resolution.\n",
+                                       path);
+                       goto mark_resolved;
                }
 
                /* Let's see if we have resolved it. */
@@ -559,6 +598,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
                copy_file(rerere_path(name, "postimage"), path, 0666);
        mark_resolved:
+               free(rr->items[i].util);
                rr->items[i].util = NULL;
        }
 
@@ -627,6 +667,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
        char *hex;
        unsigned char sha1[20];
        int ret;
+       struct string_list_item *item;
 
        ret = handle_cache(path, sha1, NULL);
        if (ret < 1)
@@ -641,8 +682,9 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)
        handle_cache(path, sha1, rerere_path(hex, "preimage"));
        fprintf(stderr, "Updated preimage for '%s'\n", path);
 
-
-       string_list_insert(rr, path)->util = hex;
+       item = string_list_insert(rr, path);
+       free(item->util);
+       item->util = hex;
        fprintf(stderr, "Forgot resolution for %s\n", path);
        return 0;
 }