git-bisect: use dash-less form on git bisect log
[gitweb.git] / builtin-rerere.c
index 29d057c98cc255e718df540a62177101d9789abe..839b26e8e0a10a498a81bf27d907543e7c256cb8 100644 (file)
@@ -16,6 +16,9 @@ static int cutoff_resolve = 60;
 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
 static int rerere_enabled = -1;
 
+/* automatically update cleanly resolved paths to the index */
+static int rerere_autoupdate;
+
 static char *merge_rr_path;
 
 static const char *rr_path(const char *name, const char *file)
@@ -23,6 +26,18 @@ static const char *rr_path(const char *name, const char *file)
        return git_path("rr-cache/%s/%s", name, file);
 }
 
+static time_t rerere_created_at(const char *name)
+{
+       struct stat st;
+       return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
+}
+
+static int has_resolution(const char *name)
+{
+       struct stat st;
+       return !stat(rr_path(name, "postimage"), &st);
+}
+
 static void read_rr(struct path_list *rr)
 {
        unsigned char sha1[20];
@@ -43,7 +58,7 @@ static void read_rr(struct path_list *rr)
                        ; /* do nothing */
                if (i == sizeof(buf))
                        die("filename too long");
-               path_list_insert(buf, rr)->util = xstrdup(name);
+               path_list_insert(buf, rr)->util = name;
        }
        fclose(in);
 }
@@ -54,40 +69,20 @@ static int write_rr(struct path_list *rr, int out_fd)
 {
        int i;
        for (i = 0; i < rr->nr; i++) {
-               const char *path = rr->items[i].path;
-               int length = strlen(path) + 1;
+               const char *path;
+               int length;
+               if (!rr->items[i].util)
+                       continue;
+               path = rr->items[i].path;
+               length = strlen(path) + 1;
                if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
                    write_in_full(out_fd, "\t", 1) != 1 ||
                    write_in_full(out_fd, path, length) != length)
                        die("unable to write rerere record");
        }
-       if (close(out_fd) != 0)
+       if (commit_lock_file(&write_lock) != 0)
                die("unable to write rerere record");
-       return commit_lock_file(&write_lock);
-}
-
-struct buffer {
-       char *ptr;
-       int nr, alloc;
-};
-
-static void append_line(struct buffer *buffer, const char *line)
-{
-       int len = strlen(line);
-
-       if (buffer->nr + len > buffer->alloc) {
-               buffer->alloc = alloc_nr(buffer->nr + len);
-               buffer->ptr = xrealloc(buffer->ptr, buffer->alloc);
-       }
-       memcpy(buffer->ptr + buffer->nr, line, len);
-       buffer->nr += len;
-}
-
-static void clear_buffer(struct buffer *buffer)
-{
-       free(buffer->ptr);
-       buffer->ptr = NULL;
-       buffer->nr = buffer->alloc = 0;
+       return 0;
 }
 
 static int handle_file(const char *path,
@@ -96,10 +91,9 @@ static int handle_file(const char *path,
        SHA_CTX ctx;
        char buf[1024];
        int hunk = 0, hunk_no = 0;
-       struct buffer minus = { NULL, 0, 0 }, plus = { NULL, 0, 0 };
-       struct buffer *one = &minus, *two = &plus;
+       struct strbuf one, two;
        FILE *f = fopen(path, "r");
-       FILE *out;
+       FILE *out = NULL;
 
        if (!f)
                return error("Could not open %s", path);
@@ -110,57 +104,58 @@ static int handle_file(const char *path,
                        fclose(f);
                        return error("Could not write %s", output);
                }
-       } else
-               out = NULL;
+       }
 
        if (sha1)
                SHA1_Init(&ctx);
 
+       strbuf_init(&one, 0);
+       strbuf_init(&two,  0);
        while (fgets(buf, sizeof(buf), f)) {
                if (!prefixcmp(buf, "<<<<<<< "))
                        hunk = 1;
                else if (!prefixcmp(buf, "======="))
                        hunk = 2;
                else if (!prefixcmp(buf, ">>>>>>> ")) {
-                       int one_is_longer = (one->nr > two->nr);
-                       int common_len = one_is_longer ? two->nr : one->nr;
-                       int cmp = memcmp(one->ptr, two->ptr, common_len);
-
+                       if (strbuf_cmp(&one, &two) > 0)
+                               strbuf_swap(&one, &two);
                        hunk_no++;
                        hunk = 0;
-                       if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
-                               struct buffer *swap = one;
-                               one = two;
-                               two = swap;
-                       }
                        if (out) {
                                fputs("<<<<<<<\n", out);
-                               fwrite(one->ptr, one->nr, 1, out);
+                               fwrite(one.buf, one.len, 1, out);
                                fputs("=======\n", out);
-                               fwrite(two->ptr, two->nr, 1, out);
+                               fwrite(two.buf, two.len, 1, out);
                                fputs(">>>>>>>\n", out);
                        }
                        if (sha1) {
-                               SHA1_Update(&ctx, one->ptr, one->nr);
-                               SHA1_Update(&ctx, "\0", 1);
-                               SHA1_Update(&ctx, two->ptr, two->nr);
-                               SHA1_Update(&ctx, "\0", 1);
+                               SHA1_Update(&ctx, one.buf ? one.buf : "",
+                                           one.len + 1);
+                               SHA1_Update(&ctx, two.buf ? two.buf : "",
+                                           two.len + 1);
                        }
-                       clear_buffer(one);
-                       clear_buffer(two);
+                       strbuf_reset(&one);
+                       strbuf_reset(&two);
                } else if (hunk == 1)
-                       append_line(one, buf);
+                       strbuf_addstr(&one, buf);
                else if (hunk == 2)
-                       append_line(two, buf);
+                       strbuf_addstr(&two, buf);
                else if (out)
                        fputs(buf, out);
        }
+       strbuf_release(&one);
+       strbuf_release(&two);
 
        fclose(f);
        if (out)
                fclose(out);
        if (sha1)
                SHA1_Final(sha1, &ctx);
+       if (hunk) {
+               if (output)
+                       unlink(output);
+               return error("Could not parse conflict hunks in %s", path);
+       }
        return hunk_no;
 }
 
@@ -175,8 +170,8 @@ static int find_conflict(struct path_list *conflict)
                if (ce_stage(e2) == 2 &&
                    ce_stage(e3) == 3 &&
                    ce_same_name(e2, e3) &&
-                   S_ISREG(ntohl(e2->ce_mode)) &&
-                   S_ISREG(ntohl(e3->ce_mode))) {
+                   S_ISREG(e2->ce_mode) &&
+                   S_ISREG(e3->ce_mode)) {
                        path_list_insert((const char *)e2->name, conflict);
                        i++; /* skip over both #2 and #3 */
                }
@@ -227,33 +222,24 @@ static void unlink_rr_item(const char *name)
 static void garbage_collect(struct path_list *rr)
 {
        struct path_list to_remove = { NULL, 0, 0, 1 };
-       char buf[1024];
        DIR *dir;
        struct dirent *e;
-       int len, i, cutoff;
+       int i, cutoff;
        time_t now = time(NULL), then;
 
-       strlcpy(buf, git_path("rr-cache"), sizeof(buf));
-       len = strlen(buf);
-       dir = opendir(buf);
-       strcpy(buf + len++, "/");
+       dir = opendir(git_path("rr-cache"));
        while ((e = readdir(dir))) {
                const char *name = e->d_name;
-               struct stat st;
-               if (name[0] == '.' && (name[1] == '\0' ||
-                                       (name[1] == '.' && name[2] == '\0')))
+               if (name[0] == '.' &&
+                   (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
                        continue;
-               i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
-               strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
-               if (stat(buf, &st))
+               then = rerere_created_at(name);
+               if (!then)
                        continue;
-               then = st.st_mtime;
-               strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
-               cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
-               if (then < now - cutoff * 86400) {
-                       buf[len + i] = '\0';
-                       path_list_insert(xstrdup(name), &to_remove);
-               }
+               cutoff = (has_resolution(name)
+                         ? cutoff_resolve : cutoff_noresolve);
+               if (then < now - cutoff * 86400)
+                       path_list_append(name, &to_remove);
        }
        for (i = 0; i < to_remove.nr; i++)
                unlink_rr_item(to_remove.items[i].path);
@@ -286,33 +272,43 @@ static int diff_two(const char *file1, const char *label1,
        memset(&xecfg, 0, sizeof(xecfg));
        xecfg.ctxlen = 3;
        ecb.outf = outf;
-       xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
+       xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
 
        free(minus.ptr);
        free(plus.ptr);
        return 0;
 }
 
-static int copy_file(const char *src, const char *dest)
+static struct lock_file index_lock;
+
+static int update_paths(struct path_list *update)
 {
-       FILE *in, *out;
-       char buffer[32768];
-       int count;
-
-       if (!(in = fopen(src, "r")))
-               return error("Could not open %s", src);
-       if (!(out = fopen(dest, "w")))
-               return error("Could not open %s", dest);
-       while ((count = fread(buffer, 1, sizeof(buffer), in)))
-               fwrite(buffer, 1, count, out);
-       fclose(in);
-       fclose(out);
-       return 0;
+       int i;
+       int fd = hold_locked_index(&index_lock, 0);
+       int status = 0;
+
+       if (fd < 0)
+               return -1;
+
+       for (i = 0; i < update->nr; i++) {
+               struct path_list_item *item = &update->items[i];
+               if (add_file_to_cache(item->path, ADD_CACHE_IGNORE_ERRORS))
+                       status = -1;
+       }
+
+       if (!status && active_cache_changed) {
+               if (write_cache(fd, active_cache, active_nr) ||
+                   commit_locked_index(&index_lock))
+                       die("Unable to write new index file");
+       } else if (fd >= 0)
+               rollback_lock_file(&index_lock);
+       return status;
 }
 
 static int do_plain_rerere(struct path_list *rr, int fd)
 {
        struct path_list conflict = { NULL, 0, 0, 1 };
+       struct path_list update = { NULL, 0, 0, 1 };
        int i;
 
        find_conflict(&conflict);
@@ -349,17 +345,17 @@ static int do_plain_rerere(struct path_list *rr, int fd)
         */
 
        for (i = 0; i < rr->nr; i++) {
-               struct stat st;
                int ret;
                const char *path = rr->items[i].path;
                const char *name = (const char *)rr->items[i].util;
 
-               if (!stat(rr_path(name, "preimage"), &st) &&
-                               !stat(rr_path(name, "postimage"), &st)) {
+               if (has_resolution(name)) {
                        if (!merge(name, path)) {
                                fprintf(stderr, "Resolved '%s' using "
                                                "previous resolution.\n", path);
-                               goto tail_optimization;
+                               if (rerere_autoupdate)
+                                       path_list_insert(path, &update);
+                               goto mark_resolved;
                        }
                }
 
@@ -369,20 +365,18 @@ static int do_plain_rerere(struct path_list *rr, int fd)
                        continue;
 
                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
-               copy_file(path, rr_path(name, "postimage"));
-tail_optimization:
-               if (i < rr->nr - 1)
-                       memmove(rr->items + i,
-                               rr->items + i + 1,
-                               sizeof(rr->items[0]) * (rr->nr - i - 1));
-               rr->nr--;
-               i--;
+               copy_file(rr_path(name, "postimage"), path, 0666);
+       mark_resolved:
+               rr->items[i].util = NULL;
        }
 
+       if (update.nr)
+               update_paths(&update);
+
        return write_rr(rr, fd);
 }
 
-static int git_rerere_config(const char *var, const char *value)
+static int git_rerere_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "gc.rerereresolved"))
                cutoff_resolve = git_config_int(var, value);
@@ -390,8 +384,10 @@ static int git_rerere_config(const char *var, const char *value)
                cutoff_noresolve = git_config_int(var, value);
        else if (!strcmp(var, "rerere.enabled"))
                rerere_enabled = git_config_bool(var, value);
+       else if (!strcmp(var, "rerere.autoupdate"))
+               rerere_autoupdate = git_config_bool(var, value);
        else
-               return git_default_config(var, value);
+               return git_default_config(var, value, cb);
        return 0;
 }
 
@@ -415,28 +411,46 @@ static int is_rerere_enabled(void)
        return 1;
 }
 
-int cmd_rerere(int argc, const char **argv, const char *prefix)
+static int setup_rerere(struct path_list *merge_rr)
 {
-       struct path_list merge_rr = { NULL, 0, 0, 1 };
-       int i, fd = -1;
+       int fd;
 
-       git_config(git_rerere_config);
+       git_config(git_rerere_config, NULL);
        if (!is_rerere_enabled())
-               return 0;
+               return -1;
 
        merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
        fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
-       read_rr(&merge_rr);
+       read_rr(merge_rr);
+       return fd;
+}
+
+int rerere(void)
+{
+       struct path_list merge_rr = { NULL, 0, 0, 1 };
+       int fd;
+
+       fd = setup_rerere(&merge_rr);
+       if (fd < 0)
+               return 0;
+       return do_plain_rerere(&merge_rr, fd);
+}
+
+int cmd_rerere(int argc, const char **argv, const char *prefix)
+{
+       struct path_list merge_rr = { NULL, 0, 0, 1 };
+       int i, fd;
+
+       fd = setup_rerere(&merge_rr);
+       if (fd < 0)
+               return 0;
 
        if (argc < 2)
                return do_plain_rerere(&merge_rr, fd);
        else if (!strcmp(argv[1], "clear")) {
                for (i = 0; i < merge_rr.nr; i++) {
-                       struct stat st;
                        const char *name = (const char *)merge_rr.items[i].util;
-                       if (!stat(git_path("rr-cache/%s", name), &st) &&
-                                       S_ISDIR(st.st_mode) &&
-                                       stat(rr_path(name, "postimage"), &st))
+                       if (!has_resolution(name))
                                unlink_rr_item(name);
                }
                unlink(merge_rr_path);