Silence cpio's "N blocks" output when cloning locally
[gitweb.git] / builtin-rerere.c
index 58288f61a3861df757f8299e168e847943915e6b..c607aade637423f0129167163fa4055b08546248 100644 (file)
@@ -61,9 +61,9 @@ static int write_rr(struct path_list *rr, int out_fd)
                    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);
+       return 0;
 }
 
 static int handle_file(const char *path,
@@ -72,13 +72,9 @@ static int handle_file(const char *path,
        SHA_CTX ctx;
        char buf[1024];
        int hunk = 0, hunk_no = 0;
-       struct strbuf minus, plus;
-       struct strbuf *one = &minus, *two = +
+       struct strbuf one, two;
        FILE *f = fopen(path, "r");
-       FILE *out;
-
-       strbuf_init(&minus, 0);
-       strbuf_init(&plus,  0);
+       FILE *out = NULL;
 
        if (!f)
                return error("Could not open %s", path);
@@ -89,51 +85,50 @@ 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->len > two->len);
-                       int common_len = one_is_longer ? two->len : one->len;
-                       int cmp = memcmp(one->buf, two->buf, common_len);
+                       int cmp = strbuf_cmp(&one, &two);
 
                        hunk_no++;
                        hunk = 0;
-                       if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
-                               struct strbuf *swap = one;
-                               one = two;
-                               two = swap;
+                       if (cmp > 0) {
+                               strbuf_swap(&one, &two);
                        }
                        if (out) {
                                fputs("<<<<<<<\n", out);
-                               fwrite(one->buf, one->len, 1, out);
+                               fwrite(one.buf, one.len, 1, out);
                                fputs("=======\n", out);
-                               fwrite(two->buf, two->len, 1, out);
+                               fwrite(two.buf, two.len, 1, out);
                                fputs(">>>>>>>\n", out);
                        }
                        if (sha1) {
-                               SHA1_Update(&ctx, one->buf, one->len);
-                               SHA1_Update(&ctx, "\0", 1);
-                               SHA1_Update(&ctx, two->buf, two->len);
-                               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);
                        }
-                       strbuf_release(one);
-                       strbuf_release(two);
+                       strbuf_reset(&one);
+                       strbuf_reset(&two);
                } else if (hunk == 1)
-                       strbuf_addstr(one, buf);
+                       strbuf_addstr(&one, buf);
                else if (hunk == 2)
-                       strbuf_addstr(two, buf);
+                       strbuf_addstr(&two, buf);
                else if (out)
                        fputs(buf, out);
        }
+       strbuf_release(&one);
+       strbuf_release(&two);
 
        fclose(f);
        if (out)
@@ -154,8 +149,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 */
                }
@@ -265,30 +260,13 @@ 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)
-{
-       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;
-}
-
 static int do_plain_rerere(struct path_list *rr, int fd)
 {
        struct path_list conflict = { NULL, 0, 0, 1 };
@@ -348,7 +326,7 @@ 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"));
+               copy_file(rr_path(name, "postimage"), path, 0666);
 tail_optimization:
                if (i < rr->nr - 1)
                        memmove(rr->items + i,
@@ -394,18 +372,39 @@ 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);
        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);