2007-07-15 07:14:45 +08:00
|
|
|
#include "builtin.h"
|
2006-12-21 00:39:41 +08:00
|
|
|
#include "cache.h"
|
2009-01-10 20:07:50 +08:00
|
|
|
#include "dir.h"
|
2008-07-22 02:03:49 +08:00
|
|
|
#include "string-list.h"
|
2008-07-09 20:58:57 +08:00
|
|
|
#include "rerere.h"
|
2006-12-21 00:39:41 +08:00
|
|
|
#include "xdiff/xdiff.h"
|
|
|
|
#include "xdiff-interface.h"
|
|
|
|
|
|
|
|
static const char git_rerere_usage[] =
|
2008-07-13 21:36:15 +08:00
|
|
|
"git rerere [clear | status | diff | gc]";
|
2006-12-21 00:39:41 +08:00
|
|
|
|
|
|
|
/* these values are days */
|
|
|
|
static int cutoff_noresolve = 15;
|
|
|
|
static int cutoff_resolve = 60;
|
|
|
|
|
2008-06-22 14:28:58 +08:00
|
|
|
static time_t rerere_created_at(const char *name)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2009-02-15 06:21:04 +08:00
|
|
|
return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
|
2008-06-22 14:28:58 +08:00
|
|
|
}
|
|
|
|
|
2006-12-21 00:39:41 +08:00
|
|
|
static void unlink_rr_item(const char *name)
|
|
|
|
{
|
2009-02-15 06:21:04 +08:00
|
|
|
unlink(rerere_path(name, "thisimage"));
|
|
|
|
unlink(rerere_path(name, "preimage"));
|
|
|
|
unlink(rerere_path(name, "postimage"));
|
2006-12-21 00:39:41 +08:00
|
|
|
rmdir(git_path("rr-cache/%s", name));
|
|
|
|
}
|
|
|
|
|
2008-07-09 20:58:57 +08: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-22 02:03:49 +08:00
|
|
|
static void garbage_collect(struct string_list *rr)
|
2006-12-21 00:39:41 +08:00
|
|
|
{
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list to_remove = { NULL, 0, 0, 1 };
|
2006-12-21 00:39:41 +08:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *e;
|
2008-06-22 14:28:58 +08:00
|
|
|
int i, cutoff;
|
2006-12-21 00:39:41 +08:00
|
|
|
time_t now = time(NULL), then;
|
|
|
|
|
2008-07-09 20:58:57 +08:00
|
|
|
git_config(git_rerere_gc_config, NULL);
|
2008-06-22 14:28:58 +08:00
|
|
|
dir = opendir(git_path("rr-cache"));
|
2009-12-04 18:35:57 +08:00
|
|
|
if (!dir)
|
|
|
|
die_errno("unable to open rr-cache directory");
|
2006-12-21 00:39:41 +08:00
|
|
|
while ((e = readdir(dir))) {
|
2009-01-10 20:07:50 +08:00
|
|
|
if (is_dot_or_dotdot(e->d_name))
|
2006-12-21 00:39:41 +08:00
|
|
|
continue;
|
2009-01-10 20:07:50 +08:00
|
|
|
then = rerere_created_at(e->d_name);
|
2008-06-22 14:28:58 +08:00
|
|
|
if (!then)
|
2006-12-21 00:39:41 +08:00
|
|
|
continue;
|
2009-02-15 06:21:04 +08:00
|
|
|
cutoff = (has_rerere_resolution(e->d_name)
|
2008-06-22 14:28:58 +08:00
|
|
|
? cutoff_resolve : cutoff_noresolve);
|
|
|
|
if (then < now - cutoff * 86400)
|
2009-01-10 20:07:50 +08:00
|
|
|
string_list_append(e->d_name, &to_remove);
|
2006-12-21 00:39:41 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i < to_remove.nr; i++)
|
2008-07-22 02:03:49 +08:00
|
|
|
unlink_rr_item(to_remove.items[i].string);
|
|
|
|
string_list_clear(&to_remove, 0);
|
2006-12-21 00:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < nbuf; i++)
|
2007-01-08 23:58:23 +08:00
|
|
|
if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
|
|
|
|
return -1;
|
2006-12-21 00:39:41 +08: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);
|
2008-10-25 21:30:37 +08:00
|
|
|
memset(&xpp, 0, sizeof(xpp));
|
2006-12-21 00:39:41 +08:00
|
|
|
xpp.flags = XDF_NEED_MINIMAL;
|
2007-07-05 02:05:46 +08:00
|
|
|
memset(&xecfg, 0, sizeof(xecfg));
|
2006-12-21 00:39:41 +08:00
|
|
|
xecfg.ctxlen = 3;
|
|
|
|
ecb.outf = outf;
|
2007-12-14 05:25:07 +08:00
|
|
|
xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
|
2006-12-21 00:39:41 +08:00
|
|
|
|
|
|
|
free(minus.ptr);
|
|
|
|
free(plus.ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-18 08:06:47 +08:00
|
|
|
int cmd_rerere(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list merge_rr = { NULL, 0, 0, 1 };
|
2009-12-04 16:20:48 +08:00
|
|
|
int i, fd, flags = 0;
|
|
|
|
|
|
|
|
if (2 < argc) {
|
|
|
|
if (!strcmp(argv[1], "-h"))
|
|
|
|
usage(git_rerere_usage);
|
|
|
|
if (!strcmp(argv[1], "--rerere-autoupdate"))
|
|
|
|
flags = RERERE_AUTOUPDATE;
|
|
|
|
else if (!strcmp(argv[1], "--no-rerere-autoupdate"))
|
|
|
|
flags = RERERE_NOAUTOUPDATE;
|
|
|
|
if (flags) {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
}
|
2008-07-09 20:58:57 +08:00
|
|
|
if (argc < 2)
|
2009-12-04 16:20:48 +08:00
|
|
|
return rerere(flags);
|
2009-11-09 23:05:01 +08:00
|
|
|
|
2010-01-21 06:44:31 +08:00
|
|
|
if (!strcmp(argv[1], "forget"))
|
2009-12-26 07:51:32 +08:00
|
|
|
return rerere_forget(argv + 2);
|
2009-11-09 23:05:01 +08:00
|
|
|
|
2009-12-04 16:20:48 +08:00
|
|
|
fd = setup_rerere(&merge_rr, flags);
|
2007-09-18 08:06:47 +08:00
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
2006-12-21 00:39:41 +08:00
|
|
|
|
2008-07-09 20:58:57 +08:00
|
|
|
if (!strcmp(argv[1], "clear")) {
|
2006-12-21 00:39:41 +08:00
|
|
|
for (i = 0; i < merge_rr.nr; i++) {
|
|
|
|
const char *name = (const char *)merge_rr.items[i].util;
|
2009-02-15 06:21:04 +08:00
|
|
|
if (!has_rerere_resolution(name))
|
2006-12-21 00:39:41 +08:00
|
|
|
unlink_rr_item(name);
|
|
|
|
}
|
2009-04-30 05:22:56 +08:00
|
|
|
unlink_or_warn(git_path("rr-cache/MERGE_RR"));
|
2006-12-21 00:39:41 +08: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-22 02:03:49 +08:00
|
|
|
printf("%s\n", merge_rr.items[i].string);
|
2006-12-21 00:39:41 +08:00
|
|
|
else if (!strcmp(argv[1], "diff"))
|
|
|
|
for (i = 0; i < merge_rr.nr; i++) {
|
2008-07-22 02:03:49 +08:00
|
|
|
const char *path = merge_rr.items[i].string;
|
2006-12-21 00:39:41 +08:00
|
|
|
const char *name = (const char *)merge_rr.items[i].util;
|
2009-02-15 06:21:04 +08:00
|
|
|
diff_two(rerere_path(name, "preimage"), path, path, path);
|
2006-12-21 00:39:41 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
usage(git_rerere_usage);
|
|
|
|
|
2008-07-22 02:03:49 +08:00
|
|
|
string_list_clear(&merge_rr, 1);
|
2006-12-21 00:39:41 +08:00
|
|
|
return 0;
|
|
|
|
}
|