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

12
static const char * const rerere_usage[] = {
13
	N_("git rerere [clear | forget <path>... | status | remaining | diff | gc]"),
14 15
	NULL,
};
J
Johannes Schindelin 已提交
16 17 18 19 20

static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
{
	int i;
	for (i = 0; i < nbuf; i++)
21
		if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
22
			return -1;
J
Johannes Schindelin 已提交
23 24 25 26 27 28 29 30 31 32
	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;
J
Jeff King 已提交
33
	int ret;
J
Johannes Schindelin 已提交
34 35

	if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
J
Jeff King 已提交
36
		return -1;
J
Johannes Schindelin 已提交
37 38 39

	printf("--- a/%s\n+++ b/%s\n", label1, label2);
	fflush(stdout);
B
Brian Downing 已提交
40
	memset(&xpp, 0, sizeof(xpp));
R
René Scharfe 已提交
41
	xpp.flags = 0;
42
	memset(&xecfg, 0, sizeof(xecfg));
J
Johannes Schindelin 已提交
43 44
	xecfg.ctxlen = 3;
	ecb.outf = outf;
J
Jeff King 已提交
45
	ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
J
Johannes Schindelin 已提交
46 47 48

	free(minus.ptr);
	free(plus.ptr);
J
Jeff King 已提交
49
	return ret;
J
Johannes Schindelin 已提交
50 51
}

52 53
int cmd_rerere(int argc, const char **argv, const char *prefix)
{
54
	struct string_list merge_rr = STRING_LIST_INIT_DUP;
55
	int i, autoupdate = -1, flags = 0;
56 57 58

	struct option options[] = {
		OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
59
			N_("register clean resolutions in index"), 1),
60 61 62 63 64
		OPT_END(),
	};

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

65 66
	git_config(git_xmerge_config, NULL);

67 68 69 70 71 72
	if (autoupdate == 1)
		flags = RERERE_AUTOUPDATE;
	if (autoupdate == 0)
		flags = RERERE_NOAUTOUPDATE;

	if (argc < 1)
73
		return rerere(flags);
74

75
	if (!strcmp(argv[0], "forget")) {
76
		struct pathspec pathspec;
77 78
		if (argc < 2)
			warning("'git rerere forget' without paths is deprecated");
79 80 81
		parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
			       prefix, argv + 1);
		return rerere_forget(&pathspec);
82
	}
83

84
	if (!strcmp(argv[0], "clear")) {
85
		rerere_clear(&merge_rr);
86
	} else if (!strcmp(argv[0], "gc"))
87
		rerere_gc(&merge_rr);
88 89 90
	else if (!strcmp(argv[0], "status")) {
		if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
			return 0;
J
Johannes Schindelin 已提交
91
		for (i = 0; i < merge_rr.nr; i++)
92
			printf("%s\n", merge_rr.items[i].string);
93
	} else if (!strcmp(argv[0], "remaining")) {
M
Martin von Zweigbergk 已提交
94 95 96 97 98 99 100 101 102
		rerere_remaining(&merge_rr);
		for (i = 0; i < merge_rr.nr; i++) {
			if (merge_rr.items[i].util != RERERE_RESOLVED)
				printf("%s\n", merge_rr.items[i].string);
			else
				/* prepare for later call to
				 * string_list_clear() */
				merge_rr.items[i].util = NULL;
		}
103 104 105
	} else if (!strcmp(argv[0], "diff")) {
		if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
			return 0;
J
Johannes Schindelin 已提交
106
		for (i = 0; i < merge_rr.nr; i++) {
107
			const char *path = merge_rr.items[i].string;
108
			const struct rerere_id *id = merge_rr.items[i].util;
J
Junio C Hamano 已提交
109 110
			if (diff_two(rerere_path(id, "preimage"), path, path, path))
				die("unable to generate diff for %s", rerere_path(id, NULL));
J
Johannes Schindelin 已提交
111
		}
112
	} else
113
		usage_with_options(rerere_usage, options);
J
Johannes Schindelin 已提交
114

115
	string_list_clear(&merge_rr, 1);
J
Johannes Schindelin 已提交
116 117
	return 0;
}