/src/git/builtin/rerere.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "builtin.h" |
2 | | #include "config.h" |
3 | | #include "gettext.h" |
4 | | #include "parse-options.h" |
5 | | #include "repository.h" |
6 | | #include "string-list.h" |
7 | | #include "rerere.h" |
8 | | #include "xdiff/xdiff.h" |
9 | | #include "xdiff-interface.h" |
10 | | #include "pathspec.h" |
11 | | |
12 | | static const char * const rerere_usage[] = { |
13 | | N_("git rerere [clear | forget <pathspec>... | diff | status | remaining | gc]"), |
14 | | NULL, |
15 | | }; |
16 | | |
17 | | static int outf(void *dummy UNUSED, mmbuffer_t *ptr, int nbuf) |
18 | 0 | { |
19 | 0 | int i; |
20 | 0 | for (i = 0; i < nbuf; i++) |
21 | 0 | if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0) |
22 | 0 | return -1; |
23 | 0 | return 0; |
24 | 0 | } |
25 | | |
26 | | static int diff_two(const char *file1, const char *label1, |
27 | | const char *file2, const char *label2) |
28 | 0 | { |
29 | 0 | xpparam_t xpp; |
30 | 0 | xdemitconf_t xecfg; |
31 | 0 | xdemitcb_t ecb = { .out_line = outf }; |
32 | 0 | mmfile_t minus, plus; |
33 | 0 | int ret; |
34 | |
|
35 | 0 | if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2)) |
36 | 0 | return -1; |
37 | | |
38 | 0 | printf("--- a/%s\n+++ b/%s\n", label1, label2); |
39 | 0 | fflush(stdout); |
40 | 0 | memset(&xpp, 0, sizeof(xpp)); |
41 | 0 | xpp.flags = 0; |
42 | 0 | memset(&xecfg, 0, sizeof(xecfg)); |
43 | 0 | xecfg.ctxlen = 3; |
44 | 0 | ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb); |
45 | |
|
46 | 0 | free(minus.ptr); |
47 | 0 | free(plus.ptr); |
48 | 0 | return ret; |
49 | 0 | } |
50 | | |
51 | | int cmd_rerere(int argc, const char **argv, const char *prefix) |
52 | 0 | { |
53 | 0 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
54 | 0 | int i, autoupdate = -1, flags = 0; |
55 | |
|
56 | 0 | struct option options[] = { |
57 | 0 | OPT_SET_INT(0, "rerere-autoupdate", &autoupdate, |
58 | 0 | N_("register clean resolutions in index"), 1), |
59 | 0 | OPT_END(), |
60 | 0 | }; |
61 | |
|
62 | 0 | argc = parse_options(argc, argv, prefix, options, rerere_usage, 0); |
63 | |
|
64 | 0 | git_config(git_xmerge_config, NULL); |
65 | |
|
66 | 0 | if (autoupdate == 1) |
67 | 0 | flags = RERERE_AUTOUPDATE; |
68 | 0 | if (autoupdate == 0) |
69 | 0 | flags = RERERE_NOAUTOUPDATE; |
70 | |
|
71 | 0 | if (argc < 1) |
72 | 0 | return repo_rerere(the_repository, flags); |
73 | | |
74 | 0 | if (!strcmp(argv[0], "forget")) { |
75 | 0 | struct pathspec pathspec; |
76 | 0 | int ret; |
77 | |
|
78 | 0 | if (argc < 2) |
79 | 0 | warning(_("'git rerere forget' without paths is deprecated")); |
80 | 0 | parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD, |
81 | 0 | prefix, argv + 1); |
82 | |
|
83 | 0 | ret = rerere_forget(the_repository, &pathspec); |
84 | |
|
85 | 0 | clear_pathspec(&pathspec); |
86 | 0 | return ret; |
87 | 0 | } |
88 | | |
89 | 0 | if (!strcmp(argv[0], "clear")) { |
90 | 0 | rerere_clear(the_repository, &merge_rr); |
91 | 0 | } else if (!strcmp(argv[0], "gc")) |
92 | 0 | rerere_gc(the_repository, &merge_rr); |
93 | 0 | else if (!strcmp(argv[0], "status")) { |
94 | 0 | if (setup_rerere(the_repository, &merge_rr, |
95 | 0 | flags | RERERE_READONLY) < 0) |
96 | 0 | return 0; |
97 | 0 | for (i = 0; i < merge_rr.nr; i++) |
98 | 0 | printf("%s\n", merge_rr.items[i].string); |
99 | 0 | } else if (!strcmp(argv[0], "remaining")) { |
100 | 0 | rerere_remaining(the_repository, &merge_rr); |
101 | 0 | for (i = 0; i < merge_rr.nr; i++) { |
102 | 0 | if (merge_rr.items[i].util != RERERE_RESOLVED) |
103 | 0 | printf("%s\n", merge_rr.items[i].string); |
104 | 0 | else |
105 | | /* prepare for later call to |
106 | | * string_list_clear() */ |
107 | 0 | merge_rr.items[i].util = NULL; |
108 | 0 | } |
109 | 0 | } else if (!strcmp(argv[0], "diff")) { |
110 | 0 | if (setup_rerere(the_repository, &merge_rr, |
111 | 0 | flags | RERERE_READONLY) < 0) |
112 | 0 | return 0; |
113 | 0 | for (i = 0; i < merge_rr.nr; i++) { |
114 | 0 | const char *path = merge_rr.items[i].string; |
115 | 0 | const struct rerere_id *id = merge_rr.items[i].util; |
116 | 0 | if (diff_two(rerere_path(id, "preimage"), path, path, path)) |
117 | 0 | die(_("unable to generate diff for '%s'"), rerere_path(id, NULL)); |
118 | 0 | } |
119 | 0 | } else |
120 | 0 | usage_with_options(rerere_usage, options); |
121 | | |
122 | 0 | string_list_clear(&merge_rr, 1); |
123 | 0 | return 0; |
124 | 0 | } |