blob: 1bf72423bf72d7b7d224c14808b0652d78305d63 [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Johannes Schindelin658f3652006-12-20 17:39:41 +01002#include "cache.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +03003#include "dir.h"
Jonathan Nieder672d1b72010-08-05 06:28:37 -05004#include "parse-options.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01005#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +02006#include "rerere.h"
Johannes Schindelin658f3652006-12-20 17:39:41 +01007#include "xdiff/xdiff.h"
8#include "xdiff-interface.h"
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +07009#include "pathspec.h"
Johannes Schindelin658f3652006-12-20 17:39:41 +010010
Jonathan Nieder672d1b72010-08-05 06:28:37 -050011static const char * const rerere_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070012 N_("git rerere [clear | forget <path>... | status | remaining | diff | gc]"),
Jonathan Nieder672d1b72010-08-05 06:28:37 -050013 NULL,
14};
Johannes Schindelin658f3652006-12-20 17:39:41 +010015
Johannes Schindelin658f3652006-12-20 17:39:41 +010016static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
17{
18 int i;
19 for (i = 0; i < nbuf; i++)
Andy Whitcroft93822c22007-01-08 15:58:23 +000020 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
21 return -1;
Johannes Schindelin658f3652006-12-20 17:39:41 +010022 return 0;
23}
24
25static int diff_two(const char *file1, const char *label1,
26 const char *file2, const char *label2)
27{
28 xpparam_t xpp;
29 xdemitconf_t xecfg;
30 xdemitcb_t ecb;
31 mmfile_t minus, plus;
Jeff King3efb9882015-09-24 19:12:23 -040032 int ret;
Johannes Schindelin658f3652006-12-20 17:39:41 +010033
34 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
Jeff King3efb9882015-09-24 19:12:23 -040035 return -1;
Johannes Schindelin658f3652006-12-20 17:39:41 +010036
37 printf("--- a/%s\n+++ b/%s\n", label1, label2);
38 fflush(stdout);
Brian Downing9ccd0a82008-10-25 15:30:37 +020039 memset(&xpp, 0, sizeof(xpp));
René Scharfe582aa002010-05-02 15:04:41 +020040 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +010041 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin658f3652006-12-20 17:39:41 +010042 xecfg.ctxlen = 3;
Johannes Schindelin658f3652006-12-20 17:39:41 +010043 ecb.outf = outf;
Jeff King3efb9882015-09-24 19:12:23 -040044 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
Johannes Schindelin658f3652006-12-20 17:39:41 +010045
46 free(minus.ptr);
47 free(plus.ptr);
Jeff King3efb9882015-09-24 19:12:23 -040048 return ret;
Johannes Schindelin658f3652006-12-20 17:39:41 +010049}
50
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -040051int cmd_rerere(int argc, const char **argv, const char *prefix)
52{
Thiago Farina183113a2010-07-04 16:46:19 -030053 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Jeff King9dd330e2015-09-01 18:14:09 -040054 int i, autoupdate = -1, flags = 0;
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -040055
Jonathan Nieder672d1b72010-08-05 06:28:37 -050056 struct option options[] = {
57 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
Nguyễn Thái Ngọc Duy0ff07f22012-08-20 19:32:38 +070058 N_("register clean resolutions in index"), 1),
Jonathan Nieder672d1b72010-08-05 06:28:37 -050059 OPT_END(),
60 };
61
62 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
63
Felipe Contrerasde3d8bb2014-04-29 23:08:29 -050064 git_config(git_xmerge_config, NULL);
65
Jonathan Nieder672d1b72010-08-05 06:28:37 -050066 if (autoupdate == 1)
67 flags = RERERE_AUTOUPDATE;
68 if (autoupdate == 0)
69 flags = RERERE_NOAUTOUPDATE;
70
71 if (argc < 1)
Junio C Hamanocb6020b2009-12-04 00:20:48 -080072 return rerere(flags);
Stephan Beyer5b2fd952008-07-09 14:58:57 +020073
Jonathan Nieder672d1b72010-08-05 06:28:37 -050074 if (!strcmp(argv[0], "forget")) {
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070075 struct pathspec pathspec;
Johannes Sixt5d2c3b02011-03-01 14:21:05 +010076 if (argc < 2)
77 warning("'git rerere forget' without paths is deprecated");
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070078 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
79 prefix, argv + 1);
80 return rerere_forget(&pathspec);
Johannes Sixt5a9f0392010-01-21 09:23:48 +010081 }
Jonathan Nieder99caeed2009-11-09 09:05:01 -060082
Jonathan Nieder672d1b72010-08-05 06:28:37 -050083 if (!strcmp(argv[0], "clear")) {
Junio C Hamano0f891e72011-05-08 12:55:34 -070084 rerere_clear(&merge_rr);
Jonathan Nieder672d1b72010-08-05 06:28:37 -050085 } else if (!strcmp(argv[0], "gc"))
Junio C Hamano0f891e72011-05-08 12:55:34 -070086 rerere_gc(&merge_rr);
Jeff King9dd330e2015-09-01 18:14:09 -040087 else if (!strcmp(argv[0], "status")) {
88 if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
89 return 0;
Johannes Schindelin658f3652006-12-20 17:39:41 +010090 for (i = 0; i < merge_rr.nr; i++)
Johannes Schindelinc455c872008-07-21 19:03:49 +010091 printf("%s\n", merge_rr.items[i].string);
Jeff King9dd330e2015-09-01 18:14:09 -040092 } else if (!strcmp(argv[0], "remaining")) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -050093 rerere_remaining(&merge_rr);
94 for (i = 0; i < merge_rr.nr; i++) {
95 if (merge_rr.items[i].util != RERERE_RESOLVED)
96 printf("%s\n", merge_rr.items[i].string);
97 else
98 /* prepare for later call to
99 * string_list_clear() */
100 merge_rr.items[i].util = NULL;
101 }
Jeff King9dd330e2015-09-01 18:14:09 -0400102 } else if (!strcmp(argv[0], "diff")) {
103 if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
104 return 0;
Johannes Schindelin658f3652006-12-20 17:39:41 +0100105 for (i = 0; i < merge_rr.nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100106 const char *path = merge_rr.items[i].string;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700107 const struct rerere_id *id = merge_rr.items[i].util;
Junio C Hamano590f6e42015-10-05 12:46:27 -0700108 if (diff_two(rerere_path(id, "preimage"), path, path, path))
109 die("unable to generate diff for %s", rerere_path(id, NULL));
Johannes Schindelin658f3652006-12-20 17:39:41 +0100110 }
Jeff King9dd330e2015-09-01 18:14:09 -0400111 } else
Jonathan Nieder672d1b72010-08-05 06:28:37 -0500112 usage_with_options(rerere_usage, options);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100113
Johannes Schindelinc455c872008-07-21 19:03:49 +0100114 string_list_clear(&merge_rr, 1);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100115 return 0;
116}