diff options
Diffstat (limited to 'rerere.c')
-rw-r--r-- | rerere.c | 110 |
1 files changed, 50 insertions, 60 deletions
@@ -1,4 +1,5 @@ #include "cache.h" +#include "lockfile.h" #include "string-list.h" #include "rerere.h" #include "xdiff-interface.h" @@ -6,6 +7,7 @@ #include "resolve-undo.h" #include "ll-merge.h" #include "attr.h" +#include "pathspec.h" #define RESOLVED 0 #define PUNTED 1 @@ -18,8 +20,6 @@ static int rerere_enabled = -1; /* automatically update cleanly resolved paths to the index */ static int rerere_autoupdate; -static char *merge_rr_path; - const char *rerere_path(const char *hex, const char *file) { return git_path("rr-cache/%s/%s", hex, file); @@ -35,7 +35,7 @@ static void read_rr(struct string_list *rr) { unsigned char sha1[20]; char buf[PATH_MAX]; - FILE *in = fopen(merge_rr_path, "r"); + FILE *in = fopen(git_path_merge_rr(), "r"); if (!in) return; while (fread(buf, 40, 1, in) == 1) { @@ -206,11 +206,11 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz strbuf_reset(&one); strbuf_reset(&two); } else if (hunk == RR_SIDE_1) - strbuf_addstr(&one, buf.buf); + strbuf_addbuf(&one, &buf); else if (hunk == RR_ORIGINAL) ; /* discard */ else if (hunk == RR_SIDE_2) - strbuf_addstr(&two, buf.buf); + strbuf_addbuf(&two, &buf); else rerere_io_putstr(buf.buf, io); continue; @@ -409,6 +409,8 @@ static int find_conflict(struct string_list *conflict) int rerere_remaining(struct string_list *merge_rr) { int i; + if (setup_rerere(merge_rr, RERERE_READONLY)) + return 0; if (read_cache() < 0) return error("Could not read index"); @@ -475,28 +477,23 @@ out: static struct lock_file index_lock; -static int update_paths(struct string_list *update) +static void update_paths(struct string_list *update) { int i; - int fd = hold_locked_index(&index_lock, 0); - int status = 0; - if (fd < 0) - return -1; + hold_locked_index(&index_lock, 1); for (i = 0; i < update->nr; i++) { struct string_list_item *item = &update->items[i]; - if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS)) - status = -1; + if (add_file_to_cache(item->string, 0)) + exit(128); } - if (!status && active_cache_changed) { - if (write_cache(fd, active_cache, active_nr) || - commit_locked_index(&index_lock)) + if (active_cache_changed) { + if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) die("Unable to write new index file"); - } else if (fd >= 0) + } else rollback_lock_file(&index_lock); - return status; } static int do_plain_rerere(struct string_list *rr, int fd) @@ -573,32 +570,28 @@ static int do_plain_rerere(struct string_list *rr, int fd) return write_rr(rr, fd); } -static int git_rerere_config(const char *var, const char *value, void *cb) +static void git_rerere_config(void) { - 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, cb); - return 0; + git_config_get_bool("rerere.enabled", &rerere_enabled); + git_config_get_bool("rerere.autoupdate", &rerere_autoupdate); + git_config(git_default_config, NULL); } +static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache") + static int is_rerere_enabled(void) { - const char *rr_cache; int rr_cache_exists; if (!rerere_enabled) return 0; - rr_cache = git_path("rr-cache"); - rr_cache_exists = is_directory(rr_cache); + rr_cache_exists = is_directory(git_path_rr_cache()); if (rerere_enabled < 0) return rr_cache_exists; - if (!rr_cache_exists && mkdir_in_gitdir(rr_cache)) - die("Could not create directory %s", rr_cache); + if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache())) + die("Could not create directory %s", git_path_rr_cache()); return 1; } @@ -606,15 +599,17 @@ int setup_rerere(struct string_list *merge_rr, int flags) { int fd; - git_config(git_rerere_config, NULL); + git_rerere_config(); if (!is_rerere_enabled()) return -1; if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); - merge_rr_path = git_pathdup("MERGE_RR"); - fd = hold_lock_file_for_update(&write_lock, merge_rr_path, - LOCK_DIE_ON_ERROR); + if (flags & RERERE_READONLY) + fd = 0; + else + fd = hold_lock_file_for_update(&write_lock, git_path_merge_rr(), + LOCK_DIE_ON_ERROR); read_rr(merge_rr); return fd; } @@ -656,7 +651,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr) return 0; } -int rerere_forget(const char **pathspec) +int rerere_forget(struct pathspec *pathspec) { int i, fd; struct string_list conflict = STRING_LIST_INIT_DUP; @@ -666,13 +661,15 @@ int rerere_forget(const char **pathspec) return error("Could not read index"); fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); + if (fd < 0) + return 0; unmerge_cache(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, strlen(it->string), - 0, NULL)) + if (!match_pathspec(pathspec, it->string, + strlen(it->string), 0, NULL, 0)) continue; rerere_forget_one_path(it->string, &merge_rr); } @@ -699,24 +696,6 @@ static void unlink_rr_item(const char *name) rmdir(git_path("rr-cache/%s", name)); } -struct rerere_gc_config_cb { - int cutoff_noresolve; - int cutoff_resolve; -}; - -static int git_rerere_gc_config(const char *var, const char *value, void *cb) -{ - struct rerere_gc_config_cb *cf = cb; - - if (!strcmp(var, "gc.rerereresolved")) - cf->cutoff_resolve = git_config_int(var, value); - else if (!strcmp(var, "gc.rerereunresolved")) - cf->cutoff_noresolve = git_config_int(var, value); - else - return git_default_config(var, value, cb); - return 0; -} - void rerere_gc(struct string_list *rr) { struct string_list to_remove = STRING_LIST_INIT_DUP; @@ -724,9 +703,15 @@ void rerere_gc(struct string_list *rr) struct dirent *e; int i, cutoff; time_t now = time(NULL), then; - struct rerere_gc_config_cb cf = { 15, 60 }; + int cutoff_noresolve = 15; + int cutoff_resolve = 60; + + if (setup_rerere(rr, 0) < 0) + return; - git_config(git_rerere_gc_config, &cf); + git_config_get_int("gc.rerereresolved", &cutoff_resolve); + git_config_get_int("gc.rerereunresolved", &cutoff_noresolve); + git_config(git_default_config, NULL); dir = opendir(git_path("rr-cache")); if (!dir) die_errno("unable to open rr-cache directory"); @@ -736,12 +721,12 @@ void rerere_gc(struct string_list *rr) then = rerere_last_used_at(e->d_name); if (then) { - cutoff = cf.cutoff_resolve; + cutoff = cutoff_resolve; } else { then = rerere_created_at(e->d_name); if (!then) continue; - cutoff = cf.cutoff_noresolve; + cutoff = cutoff_noresolve; } if (then < now - cutoff * 86400) string_list_append(&to_remove, e->d_name); @@ -750,16 +735,21 @@ void rerere_gc(struct string_list *rr) for (i = 0; i < to_remove.nr; i++) unlink_rr_item(to_remove.items[i].string); string_list_clear(&to_remove, 0); + rollback_lock_file(&write_lock); } void rerere_clear(struct string_list *merge_rr) { int i; + if (setup_rerere(merge_rr, 0) < 0) + return; + for (i = 0; i < merge_rr->nr; i++) { const char *name = (const char *)merge_rr->items[i].util; if (!has_rerere_resolution(name)) unlink_rr_item(name); } - unlink_or_warn(git_path("MERGE_RR")); + unlink_or_warn(git_path_merge_rr()); + rollback_lock_file(&write_lock); } |