rerere.c 4.1 KB
Newer Older
1
#include "builtin.h"
J
Johannes Schindelin 已提交
2
#include "cache.h"
3
#include "dir.h"
4
#include "parse-options.h"
5
#include "string-list.h"
6
#include "rerere.h"
J
Johannes Schindelin 已提交
7 8 9
#include "xdiff/xdiff.h"
#include "xdiff-interface.h"

10 11 12 13
static const char * const rerere_usage[] = {
	"git rerere [clear | status | diff | gc]",
	NULL,
};
J
Johannes Schindelin 已提交
14 15 16 17 18

/* these values are days */
static int cutoff_noresolve = 15;
static int cutoff_resolve = 60;

19 20 21
static time_t rerere_created_at(const char *name)
{
	struct stat st;
22
	return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
23 24
}

S
SZEDER Gábor 已提交
25 26 27 28 29 30
static time_t rerere_last_used_at(const char *name)
{
	struct stat st;
	return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
}

J
Johannes Schindelin 已提交
31 32
static void unlink_rr_item(const char *name)
{
33 34 35
	unlink(rerere_path(name, "thisimage"));
	unlink(rerere_path(name, "preimage"));
	unlink(rerere_path(name, "postimage"));
J
Johannes Schindelin 已提交
36 37 38
	rmdir(git_path("rr-cache/%s", name));
}

39 40 41 42 43 44 45 46 47 48 49
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;
}

50
static void garbage_collect(struct string_list *rr)
J
Johannes Schindelin 已提交
51
{
52
	struct string_list to_remove = STRING_LIST_INIT_DUP;
J
Johannes Schindelin 已提交
53 54
	DIR *dir;
	struct dirent *e;
55
	int i, cutoff;
J
Johannes Schindelin 已提交
56 57
	time_t now = time(NULL), then;

58
	git_config(git_rerere_gc_config, NULL);
59
	dir = opendir(git_path("rr-cache"));
60 61
	if (!dir)
		die_errno("unable to open rr-cache directory");
J
Johannes Schindelin 已提交
62
	while ((e = readdir(dir))) {
63
		if (is_dot_or_dotdot(e->d_name))
J
Johannes Schindelin 已提交
64
			continue;
S
SZEDER Gábor 已提交
65 66 67 68 69 70 71 72 73 74

		then = rerere_last_used_at(e->d_name);
		if (then) {
			cutoff = cutoff_resolve;
		} else {
			then = rerere_created_at(e->d_name);
			if (!then)
				continue;
			cutoff = cutoff_noresolve;
		}
75
		if (then < now - cutoff * 86400)
76
			string_list_append(&to_remove, e->d_name);
J
Johannes Schindelin 已提交
77 78
	}
	for (i = 0; i < to_remove.nr; i++)
79 80
		unlink_rr_item(to_remove.items[i].string);
	string_list_clear(&to_remove, 0);
J
Johannes Schindelin 已提交
81 82 83 84 85 86
}

static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
{
	int i;
	for (i = 0; i < nbuf; i++)
87 88
		if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
			return -1;
J
Johannes Schindelin 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	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);
B
Brian Downing 已提交
105
	memset(&xpp, 0, sizeof(xpp));
R
René Scharfe 已提交
106
	xpp.flags = 0;
107
	memset(&xecfg, 0, sizeof(xecfg));
J
Johannes Schindelin 已提交
108 109
	xecfg.ctxlen = 3;
	ecb.outf = outf;
J
Junio C Hamano 已提交
110
	xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
J
Johannes Schindelin 已提交
111 112 113 114 115 116

	free(minus.ptr);
	free(plus.ptr);
	return 0;
}

117 118
int cmd_rerere(int argc, const char **argv, const char *prefix)
{
119
	struct string_list merge_rr = STRING_LIST_INIT_DUP;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	int i, fd, autoupdate = -1, flags = 0;

	struct option options[] = {
		OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
			"register clean resolutions in index", 1),
		OPT_END(),
	};

	argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);

	if (autoupdate == 1)
		flags = RERERE_AUTOUPDATE;
	if (autoupdate == 0)
		flags = RERERE_NOAUTOUPDATE;

	if (argc < 1)
136
		return rerere(flags);
137

138 139
	if (!strcmp(argv[0], "forget")) {
		const char **pathspec = get_pathspec(prefix, argv + 1);
140 141
		return rerere_forget(pathspec);
	}
142

143
	fd = setup_rerere(&merge_rr, flags);
144 145
	if (fd < 0)
		return 0;
J
Johannes Schindelin 已提交
146

147
	if (!strcmp(argv[0], "clear")) {
J
Johannes Schindelin 已提交
148 149
		for (i = 0; i < merge_rr.nr; i++) {
			const char *name = (const char *)merge_rr.items[i].util;
150
			if (!has_rerere_resolution(name))
J
Johannes Schindelin 已提交
151 152
				unlink_rr_item(name);
		}
153
		unlink_or_warn(git_path("MERGE_RR"));
154
	} else if (!strcmp(argv[0], "gc"))
J
Johannes Schindelin 已提交
155
		garbage_collect(&merge_rr);
156
	else if (!strcmp(argv[0], "status"))
J
Johannes Schindelin 已提交
157
		for (i = 0; i < merge_rr.nr; i++)
158
			printf("%s\n", merge_rr.items[i].string);
159
	else if (!strcmp(argv[0], "diff"))
J
Johannes Schindelin 已提交
160
		for (i = 0; i < merge_rr.nr; i++) {
161
			const char *path = merge_rr.items[i].string;
J
Johannes Schindelin 已提交
162
			const char *name = (const char *)merge_rr.items[i].util;
163
			diff_two(rerere_path(name, "preimage"), path, path, path);
J
Johannes Schindelin 已提交
164 165
		}
	else
166
		usage_with_options(rerere_usage, options);
J
Johannes Schindelin 已提交
167

168
	string_list_clear(&merge_rr, 1);
J
Johannes Schindelin 已提交
169 170
	return 0;
}