blob: 12535c9b4f48cd196943c1914fefee7c47cecec2 [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;
32
33 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
34 return 1;
35
36 printf("--- a/%s\n+++ b/%s\n", label1, label2);
37 fflush(stdout);
Brian Downing9ccd0a82008-10-25 15:30:37 +020038 memset(&xpp, 0, sizeof(xpp));
René Scharfe582aa002010-05-02 15:04:41 +020039 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +010040 memset(&xecfg, 0, sizeof(xecfg));
Johannes Schindelin658f3652006-12-20 17:39:41 +010041 xecfg.ctxlen = 3;
Johannes Schindelin658f3652006-12-20 17:39:41 +010042 ecb.outf = outf;
Junio C Hamanoc279d7e2007-12-13 13:25:07 -080043 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
Johannes Schindelin658f3652006-12-20 17:39:41 +010044
45 free(minus.ptr);
46 free(plus.ptr);
47 return 0;
48}
49
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -040050int cmd_rerere(int argc, const char **argv, const char *prefix)
51{
Thiago Farina183113a2010-07-04 16:46:19 -030052 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Jeff King9dd330e2015-09-01 18:14:09 -040053 int i, autoupdate = -1, flags = 0;
Kristian Høgsbergd8b7db02007-09-17 20:06:47 -040054
Jonathan Nieder672d1b72010-08-05 06:28:37 -050055 struct option options[] = {
56 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
Nguyễn Thái Ngọc Duy0ff07f22012-08-20 19:32:38 +070057 N_("register clean resolutions in index"), 1),
Jonathan Nieder672d1b72010-08-05 06:28:37 -050058 OPT_END(),
59 };
60
61 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
62
Felipe Contrerasde3d8bb2014-04-29 23:08:29 -050063 git_config(git_xmerge_config, NULL);
64
Jonathan Nieder672d1b72010-08-05 06:28:37 -050065 if (autoupdate == 1)
66 flags = RERERE_AUTOUPDATE;
67 if (autoupdate == 0)
68 flags = RERERE_NOAUTOUPDATE;
69
70 if (argc < 1)
Junio C Hamanocb6020b2009-12-04 00:20:48 -080071 return rerere(flags);
Stephan Beyer5b2fd952008-07-09 14:58:57 +020072
Jonathan Nieder672d1b72010-08-05 06:28:37 -050073 if (!strcmp(argv[0], "forget")) {
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070074 struct pathspec pathspec;
Johannes Sixt5d2c3b02011-03-01 14:21:05 +010075 if (argc < 2)
76 warning("'git rerere forget' without paths is deprecated");
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070077 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
78 prefix, argv + 1);
79 return rerere_forget(&pathspec);
Johannes Sixt5a9f0392010-01-21 09:23:48 +010080 }
Jonathan Nieder99caeed2009-11-09 09:05:01 -060081
Jonathan Nieder672d1b72010-08-05 06:28:37 -050082 if (!strcmp(argv[0], "clear")) {
Junio C Hamano0f891e72011-05-08 12:55:34 -070083 rerere_clear(&merge_rr);
Jonathan Nieder672d1b72010-08-05 06:28:37 -050084 } else if (!strcmp(argv[0], "gc"))
Junio C Hamano0f891e72011-05-08 12:55:34 -070085 rerere_gc(&merge_rr);
Jeff King9dd330e2015-09-01 18:14:09 -040086 else if (!strcmp(argv[0], "status")) {
87 if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
88 return 0;
Johannes Schindelin658f3652006-12-20 17:39:41 +010089 for (i = 0; i < merge_rr.nr; i++)
Johannes Schindelinc455c872008-07-21 19:03:49 +010090 printf("%s\n", merge_rr.items[i].string);
Jeff King9dd330e2015-09-01 18:14:09 -040091 } else if (!strcmp(argv[0], "remaining")) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -050092 rerere_remaining(&merge_rr);
93 for (i = 0; i < merge_rr.nr; i++) {
94 if (merge_rr.items[i].util != RERERE_RESOLVED)
95 printf("%s\n", merge_rr.items[i].string);
96 else
97 /* prepare for later call to
98 * string_list_clear() */
99 merge_rr.items[i].util = NULL;
100 }
Jeff King9dd330e2015-09-01 18:14:09 -0400101 } else if (!strcmp(argv[0], "diff")) {
102 if (setup_rerere(&merge_rr, flags | RERERE_READONLY) < 0)
103 return 0;
Johannes Schindelin658f3652006-12-20 17:39:41 +0100104 for (i = 0; i < merge_rr.nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100105 const char *path = merge_rr.items[i].string;
Johannes Schindelin658f3652006-12-20 17:39:41 +0100106 const char *name = (const char *)merge_rr.items[i].util;
SZEDER Gábor90056962009-02-14 23:21:04 +0100107 diff_two(rerere_path(name, "preimage"), path, path, path);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100108 }
Jeff King9dd330e2015-09-01 18:14:09 -0400109 } else
Jonathan Nieder672d1b72010-08-05 06:28:37 -0500110 usage_with_options(rerere_usage, options);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100111
Johannes Schindelinc455c872008-07-21 19:03:49 +0100112 string_list_clear(&merge_rr, 1);
Johannes Schindelin658f3652006-12-20 17:39:41 +0100113 return 0;
114}