diff: do not use null sha1 as a sentinel value
[git/git.git] / builtin / rerere.c
CommitLineData
baffc0e7 1#include "builtin.h"
658f3650 2#include "cache.h"
8ca12c0d 3#include "dir.h"
672d1b78 4#include "parse-options.h"
c455c87c 5#include "string-list.h"
5b2fd956 6#include "rerere.h"
658f3650
JS
7#include "xdiff/xdiff.h"
8#include "xdiff-interface.h"
9
672d1b78 10static const char * const rerere_usage[] = {
5d2c3b01 11 "git rerere [clear | forget path... | status | remaining | diff | gc]",
672d1b78
JN
12 NULL,
13};
658f3650 14
658f3650
JS
15static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
16{
17 int i;
18 for (i = 0; i < nbuf; i++)
93822c22
AW
19 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
20 return -1;
658f3650
JS
21 return 0;
22}
23
24static int diff_two(const char *file1, const char *label1,
25 const char *file2, const char *label2)
26{
27 xpparam_t xpp;
28 xdemitconf_t xecfg;
29 xdemitcb_t ecb;
30 mmfile_t minus, plus;
31
32 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
33 return 1;
34
35 printf("--- a/%s\n+++ b/%s\n", label1, label2);
36 fflush(stdout);
9ccd0a88 37 memset(&xpp, 0, sizeof(xpp));
582aa00b 38 xpp.flags = 0;
30b25010 39 memset(&xecfg, 0, sizeof(xecfg));
658f3650 40 xecfg.ctxlen = 3;
658f3650 41 ecb.outf = outf;
c279d7e9 42 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
658f3650
JS
43
44 free(minus.ptr);
45 free(plus.ptr);
46 return 0;
47}
48
d8b7db0a
KH
49int cmd_rerere(int argc, const char **argv, const char *prefix)
50{
183113a5 51 struct string_list merge_rr = STRING_LIST_INIT_DUP;
672d1b78
JN
52 int i, fd, autoupdate = -1, flags = 0;
53
54 struct option options[] = {
55 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
56 "register clean resolutions in index", 1),
57 OPT_END(),
58 };
59
60 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
61
62 if (autoupdate == 1)
63 flags = RERERE_AUTOUPDATE;
64 if (autoupdate == 0)
65 flags = RERERE_NOAUTOUPDATE;
66
67 if (argc < 1)
cb6020bb 68 return rerere(flags);
99caeed0 69
672d1b78 70 if (!strcmp(argv[0], "forget")) {
5d2c3b01
JS
71 const char **pathspec;
72 if (argc < 2)
73 warning("'git rerere forget' without paths is deprecated");
74 pathspec = get_pathspec(prefix, argv + 1);
5a9f0395
JS
75 return rerere_forget(pathspec);
76 }
99caeed0 77
cb6020bb 78 fd = setup_rerere(&merge_rr, flags);
d8b7db0a
KH
79 if (fd < 0)
80 return 0;
658f3650 81
672d1b78 82 if (!strcmp(argv[0], "clear")) {
0f891e7d 83 rerere_clear(&merge_rr);
672d1b78 84 } else if (!strcmp(argv[0], "gc"))
0f891e7d 85 rerere_gc(&merge_rr);
672d1b78 86 else if (!strcmp(argv[0], "status"))
658f3650 87 for (i = 0; i < merge_rr.nr; i++)
c455c87c 88 printf("%s\n", merge_rr.items[i].string);
ac49f5ca
MZ
89 else if (!strcmp(argv[0], "remaining")) {
90 rerere_remaining(&merge_rr);
91 for (i = 0; i < merge_rr.nr; i++) {
92 if (merge_rr.items[i].util != RERERE_RESOLVED)
93 printf("%s\n", merge_rr.items[i].string);
94 else
95 /* prepare for later call to
96 * string_list_clear() */
97 merge_rr.items[i].util = NULL;
98 }
99 } else if (!strcmp(argv[0], "diff"))
658f3650 100 for (i = 0; i < merge_rr.nr; i++) {
c455c87c 101 const char *path = merge_rr.items[i].string;
658f3650 102 const char *name = (const char *)merge_rr.items[i].util;
90056966 103 diff_two(rerere_path(name, "preimage"), path, path, path);
658f3650
JS
104 }
105 else
672d1b78 106 usage_with_options(rerere_usage, options);
658f3650 107
c455c87c 108 string_list_clear(&merge_rr, 1);
658f3650
JS
109 return 0;
110}