2007-07-15 03:14:45 +04:00
|
|
|
#include "builtin.h"
|
2006-12-20 19:39:41 +03:00
|
|
|
#include "cache.h"
|
2008-07-21 22:03:49 +04:00
|
|
|
#include "string-list.h"
|
2008-07-09 16:58:57 +04:00
|
|
|
#include "rerere.h"
|
2006-12-20 19:39:41 +03:00
|
|
|
#include "xdiff/xdiff.h"
|
|
|
|
#include "xdiff-interface.h"
|
|
|
|
|
|
|
|
static const char git_rerere_usage[] =
|
2008-07-13 17:36:15 +04:00
|
|
|
"git rerere [clear | status | diff | gc]";
|
2006-12-20 19:39:41 +03:00
|
|
|
|
|
|
|
/* these values are days */
|
|
|
|
static int cutoff_noresolve = 15;
|
|
|
|
static int cutoff_resolve = 60;
|
|
|
|
|
|
|
|
static const char *rr_path(const char *name, const char *file)
|
|
|
|
{
|
|
|
|
return git_path("rr-cache/%s/%s", name, file);
|
|
|
|
}
|
|
|
|
|
2008-06-22 10:28:58 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-12-20 19:39:41 +03:00
|
|
|
static void unlink_rr_item(const char *name)
|
|
|
|
{
|
|
|
|
unlink(rr_path(name, "thisimage"));
|
|
|
|
unlink(rr_path(name, "preimage"));
|
|
|
|
unlink(rr_path(name, "postimage"));
|
|
|
|
rmdir(git_path("rr-cache/%s", name));
|
|
|
|
}
|
|
|
|
|
2008-07-09 16:58:57 +04:00
|
|
|
static int git_rerere_gc_config(const char *var, const char *value, void *cb)
|
|
|
|
{
|
|
|
|
if (!strcmp(var, "gc.rerereresolved"))
|
|
|
|
cutoff_resolve = git_config_int(var, value);
|
|
|
|
else if (!strcmp(var, "gc.rerereunresolved"))
|
|
|
|
cutoff_noresolve = git_config_int(var, value);
|
|
|
|
else
|
|
|
|
return git_default_config(var, value, cb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-21 22:03:49 +04:00
|
|
|
static void garbage_collect(struct string_list *rr)
|
2006-12-20 19:39:41 +03:00
|
|
|
{
|
2008-07-21 22:03:49 +04:00
|
|
|
struct string_list to_remove = { NULL, 0, 0, 1 };
|
2006-12-20 19:39:41 +03:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *e;
|
2008-06-22 10:28:58 +04:00
|
|
|
int i, cutoff;
|
2006-12-20 19:39:41 +03:00
|
|
|
time_t now = time(NULL), then;
|
|
|
|
|
2008-07-09 16:58:57 +04:00
|
|
|
git_config(git_rerere_gc_config, NULL);
|
2008-06-22 10:28:58 +04:00
|
|
|
dir = opendir(git_path("rr-cache"));
|
2006-12-20 19:39:41 +03:00
|
|
|
while ((e = readdir(dir))) {
|
|
|
|
const char *name = e->d_name;
|
2008-06-22 10:28:58 +04:00
|
|
|
if (name[0] == '.' &&
|
|
|
|
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
|
2006-12-20 19:39:41 +03:00
|
|
|
continue;
|
2008-06-22 10:28:58 +04:00
|
|
|
then = rerere_created_at(name);
|
|
|
|
if (!then)
|
2006-12-20 19:39:41 +03:00
|
|
|
continue;
|
2008-06-22 10:28:58 +04:00
|
|
|
cutoff = (has_resolution(name)
|
|
|
|
? cutoff_resolve : cutoff_noresolve);
|
|
|
|
if (then < now - cutoff * 86400)
|
2008-07-21 22:03:49 +04:00
|
|
|
string_list_append(name, &to_remove);
|
2006-12-20 19:39:41 +03:00
|
|
|
}
|
|
|
|
for (i = 0; i < to_remove.nr; i++)
|
2008-07-21 22:03:49 +04:00
|
|
|
unlink_rr_item(to_remove.items[i].string);
|
|
|
|
string_list_clear(&to_remove, 0);
|
2006-12-20 19:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nbuf; i++)
|
2007-01-08 18:58:23 +03:00
|
|
|
if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
|
|
|
|
return -1;
|
2006-12-20 19:39:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int diff_two(const char *file1, const char *label1,
|
|
|
|
const char *file2, const char *label2)
|
|
|
|
{
|
|
|
|
xpparam_t xpp;
|
|
|
|
xdemitconf_t xecfg;
|
|
|
|
xdemitcb_t ecb;
|
|
|
|
mmfile_t minus, plus;
|
|
|
|
|
|
|
|
if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
printf("--- a/%s\n+++ b/%s\n", label1, label2);
|
|
|
|
fflush(stdout);
|
|
|
|
xpp.flags = XDF_NEED_MINIMAL;
|
2007-07-04 22:05:46 +04:00
|
|
|
memset(&xecfg, 0, sizeof(xecfg));
|
2006-12-20 19:39:41 +03:00
|
|
|
xecfg.ctxlen = 3;
|
|
|
|
ecb.outf = outf;
|
2007-12-14 00:25:07 +03:00
|
|
|
xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
|
2006-12-20 19:39:41 +03:00
|
|
|
|
|
|
|
free(minus.ptr);
|
|
|
|
free(plus.ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-18 04:06:47 +04:00
|
|
|
int cmd_rerere(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2008-07-21 22:03:49 +04:00
|
|
|
struct string_list merge_rr = { NULL, 0, 0, 1 };
|
2007-09-18 04:06:47 +04:00
|
|
|
int i, fd;
|
|
|
|
|
2008-07-09 16:58:57 +04:00
|
|
|
if (argc < 2)
|
|
|
|
return rerere();
|
|
|
|
|
2007-09-18 04:06:47 +04:00
|
|
|
fd = setup_rerere(&merge_rr);
|
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
2006-12-20 19:39:41 +03:00
|
|
|
|
2008-07-09 16:58:57 +04:00
|
|
|
if (!strcmp(argv[1], "clear")) {
|
2006-12-20 19:39:41 +03:00
|
|
|
for (i = 0; i < merge_rr.nr; i++) {
|
|
|
|
const char *name = (const char *)merge_rr.items[i].util;
|
2008-06-22 10:28:58 +04:00
|
|
|
if (!has_resolution(name))
|
2006-12-20 19:39:41 +03:00
|
|
|
unlink_rr_item(name);
|
|
|
|
}
|
2008-07-09 16:58:57 +04:00
|
|
|
unlink(git_path("rr-cache/MERGE_RR"));
|
2006-12-20 19:39:41 +03:00
|
|
|
} else if (!strcmp(argv[1], "gc"))
|
|
|
|
garbage_collect(&merge_rr);
|
|
|
|
else if (!strcmp(argv[1], "status"))
|
|
|
|
for (i = 0; i < merge_rr.nr; i++)
|
2008-07-21 22:03:49 +04:00
|
|
|
printf("%s\n", merge_rr.items[i].string);
|
2006-12-20 19:39:41 +03:00
|
|
|
else if (!strcmp(argv[1], "diff"))
|
|
|
|
for (i = 0; i < merge_rr.nr; i++) {
|
2008-07-21 22:03:49 +04:00
|
|
|
const char *path = merge_rr.items[i].string;
|
2006-12-20 19:39:41 +03:00
|
|
|
const char *name = (const char *)merge_rr.items[i].util;
|
|
|
|
diff_two(rr_path(name, "preimage"), path, path, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
usage(git_rerere_usage);
|
|
|
|
|
2008-07-21 22:03:49 +04:00
|
|
|
string_list_clear(&merge_rr, 1);
|
2006-12-20 19:39:41 +03:00
|
|
|
return 0;
|
|
|
|
}
|