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

static const char git_rerere_usage[] =
S
Stephan Beyer 已提交
10
"git rerere [clear | status | diff | gc]";
J
Johannes Schindelin 已提交
11 12 13 14 15

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

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

J
Johannes Schindelin 已提交
22 23
static void unlink_rr_item(const char *name)
{
24 25 26
	unlink(rerere_path(name, "thisimage"));
	unlink(rerere_path(name, "preimage"));
	unlink(rerere_path(name, "postimage"));
J
Johannes Schindelin 已提交
27 28 29
	rmdir(git_path("rr-cache/%s", name));
}

30 31 32 33 34 35 36 37 38 39 40
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;
}

41
static void garbage_collect(struct string_list *rr)
J
Johannes Schindelin 已提交
42
{
43
	struct string_list to_remove = STRING_LIST_INIT_DUP;
J
Johannes Schindelin 已提交
44 45
	DIR *dir;
	struct dirent *e;
46
	int i, cutoff;
J
Johannes Schindelin 已提交
47 48
	time_t now = time(NULL), then;

49
	git_config(git_rerere_gc_config, NULL);
50
	dir = opendir(git_path("rr-cache"));
51 52
	if (!dir)
		die_errno("unable to open rr-cache directory");
J
Johannes Schindelin 已提交
53
	while ((e = readdir(dir))) {
54
		if (is_dot_or_dotdot(e->d_name))
J
Johannes Schindelin 已提交
55
			continue;
56
		then = rerere_created_at(e->d_name);
57
		if (!then)
J
Johannes Schindelin 已提交
58
			continue;
59
		cutoff = (has_rerere_resolution(e->d_name)
60 61
			  ? cutoff_resolve : cutoff_noresolve);
		if (then < now - cutoff * 86400)
62
			string_list_append(&to_remove, e->d_name);
J
Johannes Schindelin 已提交
63 64
	}
	for (i = 0; i < to_remove.nr; i++)
65 66
		unlink_rr_item(to_remove.items[i].string);
	string_list_clear(&to_remove, 0);
J
Johannes Schindelin 已提交
67 68 69 70 71 72
}

static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
{
	int i;
	for (i = 0; i < nbuf; i++)
73 74
		if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
			return -1;
J
Johannes Schindelin 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	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 已提交
91
	memset(&xpp, 0, sizeof(xpp));
R
René Scharfe 已提交
92
	xpp.flags = 0;
93
	memset(&xecfg, 0, sizeof(xecfg));
J
Johannes Schindelin 已提交
94 95
	xecfg.ctxlen = 3;
	ecb.outf = outf;
J
Junio C Hamano 已提交
96
	xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
J
Johannes Schindelin 已提交
97 98 99 100 101 102

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

103 104
int cmd_rerere(int argc, const char **argv, const char *prefix)
{
105
	struct string_list merge_rr = STRING_LIST_INIT_DUP;
106 107 108 109 110 111 112 113 114 115 116 117 118 119
	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++;
		}
	}
120
	if (argc < 2)
121
		return rerere(flags);
122

123 124 125 126
	if (!strcmp(argv[1], "forget")) {
		const char **pathspec = get_pathspec(prefix, argv + 2);
		return rerere_forget(pathspec);
	}
127

128
	fd = setup_rerere(&merge_rr, flags);
129 130
	if (fd < 0)
		return 0;
J
Johannes Schindelin 已提交
131

132
	if (!strcmp(argv[1], "clear")) {
J
Johannes Schindelin 已提交
133 134
		for (i = 0; i < merge_rr.nr; i++) {
			const char *name = (const char *)merge_rr.items[i].util;
135
			if (!has_rerere_resolution(name))
J
Johannes Schindelin 已提交
136 137
				unlink_rr_item(name);
		}
138
		unlink_or_warn(git_path("MERGE_RR"));
J
Johannes Schindelin 已提交
139 140 141 142
	} else if (!strcmp(argv[1], "gc"))
		garbage_collect(&merge_rr);
	else if (!strcmp(argv[1], "status"))
		for (i = 0; i < merge_rr.nr; i++)
143
			printf("%s\n", merge_rr.items[i].string);
J
Johannes Schindelin 已提交
144 145
	else if (!strcmp(argv[1], "diff"))
		for (i = 0; i < merge_rr.nr; i++) {
146
			const char *path = merge_rr.items[i].string;
J
Johannes Schindelin 已提交
147
			const char *name = (const char *)merge_rr.items[i].util;
148
			diff_two(rerere_path(name, "preimage"), path, path, path);
J
Johannes Schindelin 已提交
149 150 151 152
		}
	else
		usage(git_rerere_usage);

153
	string_list_clear(&merge_rr, 1);
J
Johannes Schindelin 已提交
154 155
	return 0;
}