apply: rename new_sha1_prefix and old_sha1_prefix
[gitweb.git] / rerere.c
index 895ad80c0cf910f37787a7e3988a158cc6a6dad4..7aa149e849775f110231a54cda7b71f60e75cb4f 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -9,6 +9,7 @@
 #include "ll-merge.h"
 #include "attr.h"
 #include "pathspec.h"
+#include "object-store.h"
 #include "sha1-lookup.h"
 
 #define RESOLVED 0
@@ -200,7 +201,7 @@ static struct rerere_id *new_rerere_id(unsigned char *sha1)
 static void read_rr(struct string_list *rr)
 {
        struct strbuf buf = STRBUF_INIT;
-       FILE *in = fopen_or_warn(git_path_merge_rr(), "r");
+       FILE *in = fopen_or_warn(git_path_merge_rr(the_repository), "r");
 
        if (!in)
                return;
@@ -302,38 +303,6 @@ static void rerere_io_putstr(const char *str, struct rerere_io *io)
                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];
-
-       while (size) {
-               if (size <= sizeof(buf) - 2) {
-                       memset(buf, ch, size);
-                       buf[size] = '\n';
-                       buf[size + 1] = '\0';
-                       size = 0;
-               } else {
-                       int sz = sizeof(buf) - 1;
-
-                       /*
-                        * Make sure we will not write everything out
-                        * in this round by leaving at least 1 byte
-                        * for the next round, giving the next round
-                        * a chance to add the terminating LF.  Yuck.
-                        */
-                       if (size <= sz)
-                               sz -= (sz - size) + 1;
-                       memset(buf, ch, sz);
-                       buf[sz] = '\0';
-                       size -= sz;
-               }
-               rerere_io_putstr(buf, io);
-       }
-}
-
 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
 {
        if (io->output)
@@ -384,85 +353,109 @@ static int is_cmarker(char *buf, int marker_char, int marker_size)
        return isspace(*buf);
 }
 
-/*
- * Read contents a file with conflicts, normalize the conflicts
- * by (1) discarding the common ancestor version in diff3-style,
- * (2) reordering our side and their side so that whichever sorts
- * alphabetically earlier comes before the other one, while
- * computing the "conflict ID", which is just an SHA-1 hash of
- * one side of the conflict, NUL, the other side of the conflict,
- * and NUL concatenated together.
- *
- * Return the number of conflict hunks found.
- */
-static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
+static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
+{
+       strbuf_addchars(buf, ch, size);
+       strbuf_addch(buf, '\n');
+}
+
+static int handle_conflict(struct strbuf *out, struct rerere_io *io,
+                          int marker_size, git_SHA_CTX *ctx)
 {
-       git_SHA_CTX ctx;
-       int hunk_no = 0;
        enum {
-               RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
-       } hunk = RR_CONTEXT;
+               RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
+       } hunk = RR_SIDE_1;
        struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
-       struct strbuf buf = STRBUF_INIT;
-
-       if (sha1)
-               git_SHA1_Init(&ctx);
+       struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
+       int has_conflicts = -1;
 
        while (!io->getline(&buf, io)) {
                if (is_cmarker(buf.buf, '<', marker_size)) {
-                       if (hunk != RR_CONTEXT)
-                               goto bad;
-                       hunk = RR_SIDE_1;
+                       if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
+                               break;
+                       if (hunk == RR_SIDE_1)
+                               strbuf_addbuf(&one, &conflict);
+                       else
+                               strbuf_addbuf(&two, &conflict);
+                       strbuf_release(&conflict);
                } else if (is_cmarker(buf.buf, '|', marker_size)) {
                        if (hunk != RR_SIDE_1)
-                               goto bad;
+                               break;
                        hunk = RR_ORIGINAL;
                } else if (is_cmarker(buf.buf, '=', marker_size)) {
                        if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
-                               goto bad;
+                               break;
                        hunk = RR_SIDE_2;
                } else if (is_cmarker(buf.buf, '>', marker_size)) {
                        if (hunk != RR_SIDE_2)
-                               goto bad;
+                               break;
                        if (strbuf_cmp(&one, &two) > 0)
                                strbuf_swap(&one, &two);
-                       hunk_no++;
-                       hunk = RR_CONTEXT;
-                       rerere_io_putconflict('<', marker_size, io);
-                       rerere_io_putmem(one.buf, one.len, io);
-                       rerere_io_putconflict('=', marker_size, io);
-                       rerere_io_putmem(two.buf, two.len, io);
-                       rerere_io_putconflict('>', marker_size, io);
-                       if (sha1) {
-                               git_SHA1_Update(&ctx, one.buf ? one.buf : "",
+                       has_conflicts = 1;
+                       rerere_strbuf_putconflict(out, '<', marker_size);
+                       strbuf_addbuf(out, &one);
+                       rerere_strbuf_putconflict(out, '=', marker_size);
+                       strbuf_addbuf(out, &two);
+                       rerere_strbuf_putconflict(out, '>', marker_size);
+                       if (ctx) {
+                               git_SHA1_Update(ctx, one.buf ? one.buf : "",
                                            one.len + 1);
-                               git_SHA1_Update(&ctx, two.buf ? two.buf : "",
+                               git_SHA1_Update(ctx, two.buf ? two.buf : "",
                                            two.len + 1);
                        }
-                       strbuf_reset(&one);
-                       strbuf_reset(&two);
+                       break;
                } else if (hunk == RR_SIDE_1)
                        strbuf_addbuf(&one, &buf);
                else if (hunk == RR_ORIGINAL)
                        ; /* discard */
                else if (hunk == RR_SIDE_2)
                        strbuf_addbuf(&two, &buf);
-               else
-                       rerere_io_putstr(buf.buf, io);
-               continue;
-       bad:
-               hunk = 99; /* force error exit */
-               break;
        }
        strbuf_release(&one);
        strbuf_release(&two);
        strbuf_release(&buf);
 
+       return has_conflicts;
+}
+
+/*
+ * Read contents a file with conflicts, normalize the conflicts
+ * by (1) discarding the common ancestor version in diff3-style,
+ * (2) reordering our side and their side so that whichever sorts
+ * alphabetically earlier comes before the other one, while
+ * computing the "conflict ID", which is just an SHA-1 hash of
+ * one side of the conflict, NUL, the other side of the conflict,
+ * and NUL concatenated together.
+ *
+ * Return 1 if conflict hunks are found, 0 if there are no conflict
+ * hunks and -1 if an error occured.
+ */
+static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
+{
+       git_SHA_CTX ctx;
+       struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
+       int has_conflicts = 0;
+       if (sha1)
+               git_SHA1_Init(&ctx);
+
+       while (!io->getline(&buf, io)) {
+               if (is_cmarker(buf.buf, '<', marker_size)) {
+                       has_conflicts = handle_conflict(&out, io, marker_size,
+                                                       sha1 ? &ctx : NULL);
+                       if (has_conflicts < 0)
+                               break;
+                       rerere_io_putmem(out.buf, out.len, io);
+                       strbuf_reset(&out);
+               } else
+                       rerere_io_putstr(buf.buf, io);
+       }
+       strbuf_release(&buf);
+       strbuf_release(&out);
+
        if (sha1)
                git_SHA1_Final(sha1, &ctx);
-       if (hunk != RR_CONTEXT)
-               return -1;
-       return hunk_no;
+
+       return has_conflicts;
 }
 
 /*
@@ -471,7 +464,7 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
  */
 static int handle_file(const char *path, unsigned char *sha1, const char *output)
 {
-       int hunk_no = 0;
+       int has_conflicts = 0;
        struct rerere_io_file io;
        int marker_size = ll_merge_marker_size(path);
 
@@ -491,7 +484,7 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
                }
        }
 
-       hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
+       has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
 
        fclose(io.input);
        if (io.io.wrerror)
@@ -500,14 +493,14 @@ static int handle_file(const char *path, unsigned char *sha1, const char *output
        if (io.io.output && fclose(io.io.output))
                io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
 
-       if (hunk_no < 0) {
+       if (has_conflicts < 0) {
                if (output)
                        unlink_or_warn(output);
                return error(_("could not parse conflict hunks in '%s'"), path);
        }
        if (io.io.wrerror)
                return -1;
-       return hunk_no;
+       return has_conflicts;
 }
 
 /*
@@ -528,7 +521,7 @@ static int check_one_conflict(int i, int *type)
        }
 
        *type = PUNTED;
-       while (ce_stage(active_cache[i]) == 1)
+       while (i < active_nr && ce_stage(active_cache[i]) == 1)
                i++;
 
        /* Only handle regular files with both stages #2 and #3 */
@@ -823,7 +816,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                struct rerere_id *id;
                unsigned char sha1[20];
                const char *path = conflict.items[i].string;
-               int ret, has_string;
+               int ret;
 
                /*
                 * Ask handle_file() to scan and assign a
@@ -831,12 +824,11 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                 * yet.
                 */
                ret = handle_file(path, sha1, NULL);
-               has_string = string_list_has_string(rr, path);
-               if (ret < 0 && has_string) {
+               if (ret != 0 && string_list_has_string(rr, path)) {
                        remove_variant(string_list_lookup(rr, path)->util);
                        string_list_remove(rr, path, 1);
                }
-               if (ret < 1 || has_string)
+               if (ret < 1)
                        continue;
 
                id = new_rerere_id(sha1);
@@ -893,7 +885,8 @@ int setup_rerere(struct string_list *merge_rr, int flags)
        if (flags & RERERE_READONLY)
                fd = 0;
        else
-               fd = hold_lock_file_for_update(&write_lock, git_path_merge_rr(),
+               fd = hold_lock_file_for_update(&write_lock,
+                                              git_path_merge_rr(the_repository),
                                               LOCK_DIE_ON_ERROR);
        read_rr(merge_rr);
        return fd;
@@ -954,7 +947,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
        mmfile_t mmfile[3] = {{NULL}};
        mmbuffer_t result = {NULL, 0};
        const struct cache_entry *ce;
-       int pos, len, i, hunk_no;
+       int pos, len, i, has_conflicts;
        struct rerere_io_mem io;
        int marker_size = ll_merge_marker_size(path);
 
@@ -1008,11 +1001,11 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
         * Grab the conflict ID and optionally write the original
         * contents with conflict markers out.
         */
-       hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
+       has_conflicts = handle_path(sha1, (struct rerere_io *)&io, marker_size);
        strbuf_release(&io.input);
        if (io.io.output)
                fclose(io.io.output);
-       return hunk_no;
+       return has_conflicts;
 }
 
 static int rerere_forget_one_path(const char *path, struct string_list *rr)
@@ -1116,7 +1109,7 @@ int rerere_forget(struct pathspec *pathspec)
        find_conflict(&conflict);
        for (i = 0; i < conflict.nr; i++) {
                struct string_list_item *it = &conflict.items[i];
-               if (!match_pathspec(pathspec, it->string,
+               if (!match_pathspec(&the_index, pathspec, it->string,
                                    strlen(it->string), 0, NULL, 0))
                        continue;
                rerere_forget_one_path(it->string, &merge_rr);
@@ -1243,6 +1236,6 @@ void rerere_clear(struct string_list *merge_rr)
                        rmdir(rerere_path(id, NULL));
                }
        }
-       unlink_or_warn(git_path_merge_rr());
+       unlink_or_warn(git_path_merge_rr(the_repository));
        rollback_lock_file(&write_lock);
 }