rerere: rerere_created_at() and has_resolution() abstraction
There were too many places in the code how an entry in the rerere database looks like, and the garbage_collect() function that iterates over subdirectories of the rr-cache directory was the worse offender. Introduce two helper functions, rerere_created_at() and has_resolution(), to abstract out the logic a bit better. Incidentally this fixes a small memory leak in garbage_collect() function. The path list to collect the entries to be pruned were defined to strdup the paths but the caller was feeding a path after doing an extra copy. Because the list does not have to be sorted by conflict signature hash, we use path_list_append() instead of path_list_insert(). While we are at it, make a conflicted hunk comparision in handle_file() a bit easier to read. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
73f03627f4
commit
9022a495a3
@ -23,6 +23,18 @@ static const char *rr_path(const char *name, const char *file)
|
|||||||
return git_path("rr-cache/%s/%s", name, 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)
|
static void read_rr(struct path_list *rr)
|
||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
@ -98,13 +110,10 @@ static int handle_file(const char *path,
|
|||||||
else if (!prefixcmp(buf, "======="))
|
else if (!prefixcmp(buf, "======="))
|
||||||
hunk = 2;
|
hunk = 2;
|
||||||
else if (!prefixcmp(buf, ">>>>>>> ")) {
|
else if (!prefixcmp(buf, ">>>>>>> ")) {
|
||||||
int cmp = strbuf_cmp(&one, &two);
|
if (strbuf_cmp(&one, &two) > 0)
|
||||||
|
strbuf_swap(&one, &two);
|
||||||
hunk_no++;
|
hunk_no++;
|
||||||
hunk = 0;
|
hunk = 0;
|
||||||
if (cmp > 0) {
|
|
||||||
strbuf_swap(&one, &two);
|
|
||||||
}
|
|
||||||
if (out) {
|
if (out) {
|
||||||
fputs("<<<<<<<\n", out);
|
fputs("<<<<<<<\n", out);
|
||||||
fwrite(one.buf, one.len, 1, out);
|
fwrite(one.buf, one.len, 1, out);
|
||||||
@ -201,33 +210,24 @@ static void unlink_rr_item(const char *name)
|
|||||||
static void garbage_collect(struct path_list *rr)
|
static void garbage_collect(struct path_list *rr)
|
||||||
{
|
{
|
||||||
struct path_list to_remove = { NULL, 0, 0, 1 };
|
struct path_list to_remove = { NULL, 0, 0, 1 };
|
||||||
char buf[1024];
|
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *e;
|
struct dirent *e;
|
||||||
int len, i, cutoff;
|
int i, cutoff;
|
||||||
time_t now = time(NULL), then;
|
time_t now = time(NULL), then;
|
||||||
|
|
||||||
strlcpy(buf, git_path("rr-cache"), sizeof(buf));
|
dir = opendir(git_path("rr-cache"));
|
||||||
len = strlen(buf);
|
|
||||||
dir = opendir(buf);
|
|
||||||
strcpy(buf + len++, "/");
|
|
||||||
while ((e = readdir(dir))) {
|
while ((e = readdir(dir))) {
|
||||||
const char *name = e->d_name;
|
const char *name = e->d_name;
|
||||||
struct stat st;
|
if (name[0] == '.' &&
|
||||||
if (name[0] == '.' && (name[1] == '\0' ||
|
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
|
||||||
(name[1] == '.' && name[2] == '\0')))
|
|
||||||
continue;
|
continue;
|
||||||
i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
|
then = rerere_created_at(name);
|
||||||
strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
|
if (!then)
|
||||||
if (stat(buf, &st))
|
|
||||||
continue;
|
continue;
|
||||||
then = st.st_mtime;
|
cutoff = (has_resolution(name)
|
||||||
strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
|
? cutoff_resolve : cutoff_noresolve);
|
||||||
cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
|
if (then < now - cutoff * 86400)
|
||||||
if (then < now - cutoff * 86400) {
|
path_list_append(name, &to_remove);
|
||||||
buf[len + i] = '\0';
|
|
||||||
path_list_insert(xstrdup(name), &to_remove);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (i = 0; i < to_remove.nr; i++)
|
for (i = 0; i < to_remove.nr; i++)
|
||||||
unlink_rr_item(to_remove.items[i].path);
|
unlink_rr_item(to_remove.items[i].path);
|
||||||
@ -306,13 +306,11 @@ static int do_plain_rerere(struct path_list *rr, int fd)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
for (i = 0; i < rr->nr; i++) {
|
for (i = 0; i < rr->nr; i++) {
|
||||||
struct stat st;
|
|
||||||
int ret;
|
int ret;
|
||||||
const char *path = rr->items[i].path;
|
const char *path = rr->items[i].path;
|
||||||
const char *name = (const char *)rr->items[i].util;
|
const char *name = (const char *)rr->items[i].util;
|
||||||
|
|
||||||
if (!stat(rr_path(name, "preimage"), &st) &&
|
if (has_resolution(name)) {
|
||||||
!stat(rr_path(name, "postimage"), &st)) {
|
|
||||||
if (!merge(name, path)) {
|
if (!merge(name, path)) {
|
||||||
fprintf(stderr, "Resolved '%s' using "
|
fprintf(stderr, "Resolved '%s' using "
|
||||||
"previous resolution.\n", path);
|
"previous resolution.\n", path);
|
||||||
@ -410,11 +408,8 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)
|
|||||||
return do_plain_rerere(&merge_rr, fd);
|
return do_plain_rerere(&merge_rr, fd);
|
||||||
else if (!strcmp(argv[1], "clear")) {
|
else if (!strcmp(argv[1], "clear")) {
|
||||||
for (i = 0; i < merge_rr.nr; i++) {
|
for (i = 0; i < merge_rr.nr; i++) {
|
||||||
struct stat st;
|
|
||||||
const char *name = (const char *)merge_rr.items[i].util;
|
const char *name = (const char *)merge_rr.items[i].util;
|
||||||
if (!stat(git_path("rr-cache/%s", name), &st) &&
|
if (!has_resolution(name))
|
||||||
S_ISDIR(st.st_mode) &&
|
|
||||||
stat(rr_path(name, "postimage"), &st))
|
|
||||||
unlink_rr_item(name);
|
unlink_rr_item(name);
|
||||||
}
|
}
|
||||||
unlink(merge_rr_path);
|
unlink(merge_rr_path);
|
||||||
|
Loading…
Reference in New Issue
Block a user