Code

tag.h: Remove unused signature field
[git.git] / rerere.c
index a0d477aaca73ff7ec8b12dc6c8255b69dbd677a5..29f95f657d04300e375d007e9d01832b89311602 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -12,15 +12,15 @@ static int rerere_autoupdate;
 
 static char *merge_rr_path;
 
-static const char *rr_path(const char *name, const char *file)
+const char *rerere_path(const char *hex, const char *file)
 {
-       return git_path("rr-cache/%s/%s", name, file);
+       return git_path("rr-cache/%s/%s", hex, file);
 }
 
-static int has_resolution(const char *name)
+int has_rerere_resolution(const char *hex)
 {
        struct stat st;
-       return !stat(rr_path(name, "postimage"), &st);
+       return !stat(rerere_path(hex, "postimage"), &st);
 }
 
 static void read_rr(struct string_list *rr)
@@ -61,7 +61,7 @@ static int write_rr(struct string_list *rr, int out_fd)
                path = rr->items[i].string;
                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_str_in_full(out_fd, "\t") != 1 ||
                    write_in_full(out_fd, path, length) != length)
                        die("unable to write rerere record");
        }
@@ -70,15 +70,32 @@ static int write_rr(struct string_list *rr, int out_fd)
        return 0;
 }
 
+static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
+{
+       if (!count || *err)
+               return;
+       if (fwrite(p, count, 1, fp) != 1)
+               *err = errno;
+}
+
+static inline void ferr_puts(const char *s, FILE *fp, int *err)
+{
+       ferr_write(s, strlen(s), fp, err);
+}
+
 static int handle_file(const char *path,
         unsigned char *sha1, const char *output)
 {
-       SHA_CTX ctx;
+       git_SHA_CTX ctx;
        char buf[1024];
-       int hunk = 0, hunk_no = 0;
-       struct strbuf one, two;
+       int hunk_no = 0;
+       enum {
+               RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
+       } hunk = RR_CONTEXT;
+       struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
        FILE *f = fopen(path, "r");
        FILE *out = NULL;
+       int wrerror = 0;
 
        if (!f)
                return error("Could not open %s", path);
@@ -92,47 +109,51 @@ static int handle_file(const char *path,
        }
 
        if (sha1)
-               SHA1_Init(&ctx);
+               git_SHA1_Init(&ctx);
 
-       strbuf_init(&one, 0);
-       strbuf_init(&two,  0);
        while (fgets(buf, sizeof(buf), f)) {
                if (!prefixcmp(buf, "<<<<<<< ")) {
-                       if (hunk)
+                       if (hunk != RR_CONTEXT)
+                               goto bad;
+                       hunk = RR_SIDE_1;
+               } else if (!prefixcmp(buf, "|||||||") && isspace(buf[7])) {
+                       if (hunk != RR_SIDE_1)
                                goto bad;
-                       hunk = 1;
+                       hunk = RR_ORIGINAL;
                } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
-                       if (hunk != 1)
+                       if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
                                goto bad;
-                       hunk = 2;
+                       hunk = RR_SIDE_2;
                } else if (!prefixcmp(buf, ">>>>>>> ")) {
-                       if (hunk != 2)
+                       if (hunk != RR_SIDE_2)
                                goto bad;
                        if (strbuf_cmp(&one, &two) > 0)
                                strbuf_swap(&one, &two);
                        hunk_no++;
-                       hunk = 0;
+                       hunk = RR_CONTEXT;
                        if (out) {
-                               fputs("<<<<<<<\n", out);
-                               fwrite(one.buf, one.len, 1, out);
-                               fputs("=======\n", out);
-                               fwrite(two.buf, two.len, 1, out);
-                               fputs(">>>>>>>\n", out);
+                               ferr_puts("<<<<<<<\n", out, &wrerror);
+                               ferr_write(one.buf, one.len, out, &wrerror);
+                               ferr_puts("=======\n", out, &wrerror);
+                               ferr_write(two.buf, two.len, out, &wrerror);
+                               ferr_puts(">>>>>>>\n", out, &wrerror);
                        }
                        if (sha1) {
-                               SHA1_Update(&ctx, one.buf ? one.buf : "",
+                               git_SHA1_Update(&ctx, one.buf ? one.buf : "",
                                            one.len + 1);
-                               SHA1_Update(&ctx, two.buf ? two.buf : "",
+                               git_SHA1_Update(&ctx, two.buf ? two.buf : "",
                                            two.len + 1);
                        }
                        strbuf_reset(&one);
                        strbuf_reset(&two);
-               } else if (hunk == 1)
+               } else if (hunk == RR_SIDE_1)
                        strbuf_addstr(&one, buf);
-               else if (hunk == 2)
+               else if (hunk == RR_ORIGINAL)
+                       ; /* discard */
+               else if (hunk == RR_SIDE_2)
                        strbuf_addstr(&two, buf);
                else if (out)
-                       fputs(buf, out);
+                       ferr_puts(buf, out, &wrerror);
                continue;
        bad:
                hunk = 99; /* force error exit */
@@ -142,15 +163,21 @@ static int handle_file(const char *path,
        strbuf_release(&two);
 
        fclose(f);
-       if (out)
-               fclose(out);
+       if (wrerror)
+               error("There were errors while writing %s (%s)",
+                     path, strerror(wrerror));
+       if (out && fclose(out))
+               wrerror = error("Failed to flush %s: %s",
+                               path, strerror(errno));
        if (sha1)
-               SHA1_Final(sha1, &ctx);
-       if (hunk) {
+               git_SHA1_Final(sha1, &ctx);
+       if (hunk != RR_CONTEXT) {
                if (output)
-                       unlink(output);
+                       unlink_or_warn(output);
                return error("Could not parse conflict hunks in %s", path);
        }
+       if (wrerror)
+               return -1;
        return hunk_no;
 }
 
@@ -181,21 +208,25 @@ static int merge(const char *name, const char *path)
        mmbuffer_t result = {NULL, 0};
        xpparam_t xpp = {XDF_NEED_MINIMAL};
 
-       if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
+       if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
                return 1;
 
-       if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
-                       read_mmfile(&base, rr_path(name, "preimage")) ||
-                       read_mmfile(&other, rr_path(name, "postimage")))
+       if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
+                       read_mmfile(&base, rerere_path(name, "preimage")) ||
+                       read_mmfile(&other, rerere_path(name, "postimage")))
                return 1;
        ret = xdl_merge(&base, &cur, "", &other, "",
                        &xpp, XDL_MERGE_ZEALOUS, &result);
        if (!ret) {
                FILE *f = fopen(path, "w");
                if (!f)
-                       return error("Could not write to %s", path);
-               fwrite(result.ptr, result.size, 1, f);
-               fclose(f);
+                       return error("Could not open %s: %s", path,
+                                    strerror(errno));
+               if (fwrite(result.ptr, result.size, 1, f) != 1)
+                       error("Could not write %s: %s", path, strerror(errno));
+               if (fclose(f))
+                       return error("Writing %s failed: %s", path,
+                                    strerror(errno));
        }
 
        free(cur.ptr);
@@ -259,8 +290,8 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                        hex = xstrdup(sha1_to_hex(sha1));
                        string_list_insert(path, rr)->util = hex;
                        if (mkdir(git_path("rr-cache/%s", hex), 0755))
-                               continue;;
-                       handle_file(path, NULL, rr_path(hex, "preimage"));
+                               continue;
+                       handle_file(path, NULL, rerere_path(hex, "preimage"));
                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
                }
        }
@@ -276,7 +307,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                const char *path = rr->items[i].string;
                const char *name = (const char *)rr->items[i].util;
 
-               if (has_resolution(name)) {
+               if (has_rerere_resolution(name)) {
                        if (!merge(name, path)) {
                                if (rerere_autoupdate)
                                        string_list_insert(path, &update);
@@ -295,7 +326,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                        continue;
 
                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
-               copy_file(rr_path(name, "postimage"), path, 0666);
+               copy_file(rerere_path(name, "postimage"), path, 0666);
        mark_resolved:
                rr->items[i].util = NULL;
        }
@@ -319,7 +350,6 @@ static int git_rerere_config(const char *var, const char *value, void *cb)
 
 static int is_rerere_enabled(void)
 {
-       struct stat st;
        const char *rr_cache;
        int rr_cache_exists;
 
@@ -327,7 +357,7 @@ static int is_rerere_enabled(void)
                return 0;
 
        rr_cache = git_path("rr-cache");
-       rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
+       rr_cache_exists = is_directory(rr_cache);
        if (rerere_enabled < 0)
                return rr_cache_exists;