You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
3.8 KiB
156 lines
3.8 KiB
18 years ago
|
#include "builtin.h"
|
||
18 years ago
|
#include "cache.h"
|
||
16 years ago
|
#include "dir.h"
|
||
17 years ago
|
#include "string-list.h"
|
||
17 years ago
|
#include "rerere.h"
|
||
18 years ago
|
#include "xdiff/xdiff.h"
|
||
|
#include "xdiff-interface.h"
|
||
|
|
||
|
static const char git_rerere_usage[] =
|
||
17 years ago
|
"git rerere [clear | status | diff | gc]";
|
||
18 years ago
|
|
||
|
/* these values are days */
|
||
|
static int cutoff_noresolve = 15;
|
||
|
static int cutoff_resolve = 60;
|
||
|
|
||
17 years ago
|
static time_t rerere_created_at(const char *name)
|
||
|
{
|
||
|
struct stat st;
|
||
16 years ago
|
return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
|
||
17 years ago
|
}
|
||
|
|
||
18 years ago
|
static void unlink_rr_item(const char *name)
|
||
|
{
|
||
16 years ago
|
unlink(rerere_path(name, "thisimage"));
|
||
|
unlink(rerere_path(name, "preimage"));
|
||
|
unlink(rerere_path(name, "postimage"));
|
||
18 years ago
|
rmdir(git_path("rr-cache/%s", name));
|
||
|
}
|
||
|
|
||
17 years ago
|
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;
|
||
|
}
|
||
|
|
||
17 years ago
|
static void garbage_collect(struct string_list *rr)
|
||
18 years ago
|
{
|
||
17 years ago
|
struct string_list to_remove = { NULL, 0, 0, 1 };
|
||
18 years ago
|
DIR *dir;
|
||
|
struct dirent *e;
|
||
17 years ago
|
int i, cutoff;
|
||
18 years ago
|
time_t now = time(NULL), then;
|
||
|
|
||
17 years ago
|
git_config(git_rerere_gc_config, NULL);
|
||
17 years ago
|
dir = opendir(git_path("rr-cache"));
|
||
15 years ago
|
if (!dir)
|
||
|
die_errno("unable to open rr-cache directory");
|
||
18 years ago
|
while ((e = readdir(dir))) {
|
||
16 years ago
|
if (is_dot_or_dotdot(e->d_name))
|
||
18 years ago
|
continue;
|
||
16 years ago
|
then = rerere_created_at(e->d_name);
|
||
17 years ago
|
if (!then)
|
||
18 years ago
|
continue;
|
||
16 years ago
|
cutoff = (has_rerere_resolution(e->d_name)
|
||
17 years ago
|
? cutoff_resolve : cutoff_noresolve);
|
||
|
if (then < now - cutoff * 86400)
|
||
16 years ago
|
string_list_append(e->d_name, &to_remove);
|
||
18 years ago
|
}
|
||
|
for (i = 0; i < to_remove.nr; i++)
|
||
17 years ago
|
unlink_rr_item(to_remove.items[i].string);
|
||
|
string_list_clear(&to_remove, 0);
|
||
18 years ago
|
}
|
||
|
|
||
|
static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
|
||
|
{
|
||
|
int i;
|
||
|
for (i = 0; i < nbuf; i++)
|
||
18 years ago
|
if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
|
||
|
return -1;
|
||
18 years ago
|
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);
|
||
16 years ago
|
memset(&xpp, 0, sizeof(xpp));
|
||
18 years ago
|
xpp.flags = XDF_NEED_MINIMAL;
|
||
18 years ago
|
memset(&xecfg, 0, sizeof(xecfg));
|
||
18 years ago
|
xecfg.ctxlen = 3;
|
||
|
ecb.outf = outf;
|
||
17 years ago
|
xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
|
||
18 years ago
|
|
||
|
free(minus.ptr);
|
||
|
free(plus.ptr);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
18 years ago
|
int cmd_rerere(int argc, const char **argv, const char *prefix)
|
||
|
{
|
||
17 years ago
|
struct string_list merge_rr = { NULL, 0, 0, 1 };
|
||
15 years ago
|
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++;
|
||
|
}
|
||
|
}
|
||
17 years ago
|
if (argc < 2)
|
||
15 years ago
|
return rerere(flags);
|
||
15 years ago
|
|
||
15 years ago
|
if (!strcmp(argv[1], "forget")) {
|
||
|
const char **pathspec = get_pathspec(prefix, argv + 2);
|
||
|
return rerere_forget(pathspec);
|
||
|
}
|
||
15 years ago
|
|
||
15 years ago
|
fd = setup_rerere(&merge_rr, flags);
|
||
18 years ago
|
if (fd < 0)
|
||
|
return 0;
|
||
18 years ago
|
|
||
17 years ago
|
if (!strcmp(argv[1], "clear")) {
|
||
18 years ago
|
for (i = 0; i < merge_rr.nr; i++) {
|
||
|
const char *name = (const char *)merge_rr.items[i].util;
|
||
16 years ago
|
if (!has_rerere_resolution(name))
|
||
18 years ago
|
unlink_rr_item(name);
|
||
|
}
|
||
16 years ago
|
unlink_or_warn(git_path("rr-cache/MERGE_RR"));
|
||
18 years ago
|
} else if (!strcmp(argv[1], "gc"))
|
||
|
garbage_collect(&merge_rr);
|
||
|
else if (!strcmp(argv[1], "status"))
|
||
|
for (i = 0; i < merge_rr.nr; i++)
|
||
17 years ago
|
printf("%s\n", merge_rr.items[i].string);
|
||
18 years ago
|
else if (!strcmp(argv[1], "diff"))
|
||
|
for (i = 0; i < merge_rr.nr; i++) {
|
||
17 years ago
|
const char *path = merge_rr.items[i].string;
|
||
18 years ago
|
const char *name = (const char *)merge_rr.items[i].util;
|
||
16 years ago
|
diff_two(rerere_path(name, "preimage"), path, path, path);
|
||
18 years ago
|
}
|
||
|
else
|
||
|
usage(git_rerere_usage);
|
||
|
|
||
17 years ago
|
string_list_clear(&merge_rr, 1);
|
||
18 years ago
|
return 0;
|
||
|
}
|