diff: factor out match_filter()
[gitweb.git] / rerere.c
index dee2cb1514d1e97c47a4999ed66f8996035d71fe..98e3e294d0dc295290b7b891a834186c0791e43a 100644 (file)
--- a/rerere.c
+++ b/rerere.c
@@ -25,7 +25,7 @@ const char *rerere_path(const char *hex, const char *file)
        return git_path("rr-cache/%s/%s", hex, file);
 }
 
-int has_rerere_resolution(const char *hex)
+static int has_rerere_resolution(const char *hex)
 {
        struct stat st;
        return !stat(rerere_path(hex, "postimage"), &st);
@@ -47,8 +47,14 @@ static void read_rr(struct string_list *rr)
                name = xstrdup(buf);
                if (fgetc(in) != '\t')
                        die("corrupt MERGE_RR");
-               for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
-                       ; /* do nothing */
+               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;
@@ -278,8 +284,10 @@ static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
        strbuf_release(sb);
        if (!io->input.len)
                return -1;
-       ep = strchrnul(io->input.buf, '\n');
-       if (*ep == '\n')
+       ep = memchr(io->input.buf, '\n', io->input.len);
+       if (!ep)
+               ep = io->input.buf + io->input.len;
+       else if (*ep == '\n')
                ep++;
        len = ep - io->input.buf;
        strbuf_add(sb, io->input.buf, len);
@@ -289,7 +297,7 @@ static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
 
 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
 {
-       mmfile_t mmfile[3];
+       mmfile_t mmfile[3] = {{NULL}};
        mmbuffer_t result = {NULL, 0};
        struct cache_entry *ce;
        int pos, len, i, hunk_no;
@@ -308,17 +316,16 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu
        for (i = 0; i < 3; i++) {
                enum object_type type;
                unsigned long size;
+               int j;
 
-               mmfile[i].size = 0;
-               mmfile[i].ptr = NULL;
                if (active_nr <= pos)
                        break;
                ce = active_cache[pos++];
-               if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
-                   || ce_stage(ce) != i + 1)
-                       break;
-               mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
-               mmfile[i].size = size;
+               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;
        }
        for (i = 0; i < 3; i++) {
                if (!mmfile[i].ptr && !mmfile[i].size)
@@ -518,7 +525,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
                                continue;
                        hex = xstrdup(sha1_to_hex(sha1));
                        string_list_insert(rr, path)->util = hex;
-                       if (mkdir(git_path("rr-cache/%s", hex), 0755))
+                       if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
                                continue;
                        handle_file(path, NULL, rerere_path(hex, "preimage"));
                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
@@ -538,13 +545,13 @@ static int do_plain_rerere(struct string_list *rr, int fd)
 
                if (has_rerere_resolution(name)) {
                        if (!merge(name, path)) {
-                               if (rerere_autoupdate)
+                               const char *msg;
+                               if (rerere_autoupdate) {
                                        string_list_insert(&update, path);
-                               fprintf(stderr,
-                                       "%s '%s' using previous resolution.\n",
-                                       rerere_autoupdate
-                                       ? "Staged" : "Resolved",
-                                       path);
+                                       msg = "Staged '%s' using previous resolution.\n";
+                               } else
+                                       msg = "Resolved '%s' using previous resolution.\n";
+                               fprintf(stderr, msg, path);
                                goto mark_resolved;
                        }
                }
@@ -739,6 +746,7 @@ void rerere_gc(struct string_list *rr)
                if (then < now - cutoff * 86400)
                        string_list_append(&to_remove, e->d_name);
        }
+       closedir(dir);
        for (i = 0; i < to_remove.nr; i++)
                unlink_rr_item(to_remove.items[i].string);
        string_list_clear(&to_remove, 0);