1
+#include "builtin.h"
2
+#include "config.h"
3
+#include "diff.h"
4
+#include "diffcore.h"
5
+#include "gettext.h"
6
+#include "hash.h"
7
+#include "hex.h"
8
+#include "object.h"
9
+#include "parse-options.h"
10
+#include "revision.h"
11
+#include "strbuf.h"
12
+
13
+static unsigned parse_mode_or_die(const char *mode, const char **end)
14
+{
15
+ uint16_t ret;
16
+
17
+ *end = parse_mode(mode, &ret);
18
+ if (!*end)
19
+ die(_("unable to parse mode: %s"), mode);
20
+ return ret;
21
+}
22
+
23
+static void parse_oid_or_die(const char *hex, struct object_id *oid,
24
+ const char **end, const struct git_hash_algo *algop)
25
+{
26
+ if (parse_oid_hex_algop(hex, oid, end, algop) || *(*end)++ != ' ')
27
+ die(_("unable to parse object id: %s"), hex);
28
+}
29
+
30
+int cmd_diff_pairs(int argc, const char **argv, const char *prefix,
31
+ struct repository *repo)
32
+{
33
+ struct strbuf path_dst = STRBUF_INIT;
34
+ struct strbuf path = STRBUF_INIT;
35
+ struct strbuf meta = STRBUF_INIT;
36
+ struct option *parseopts;
37
+ struct rev_info revs;
38
+ int line_term = '\0';
39
+ int ret;
40
+
41
+ const char * const builtin_diff_pairs_usage[] = {
42
+ N_("git diff-pairs -z [<diff-options>]"),
43
+ NULL
44
+ };
45
+ struct option builtin_diff_pairs_options[] = {
46
+ OPT_END()
47
+ };
48
+
49
+ repo_init_revisions(repo, &revs, prefix);
50
+
51
+ /*
52
+ * Diff options are usually parsed implicitly as part of
53
+ * setup_revisions(). Explicitly handle parsing to ensure options are
54
+ * printed in the usage message.
55
+ */
56
+ parseopts = add_diff_options(builtin_diff_pairs_options, &revs.diffopt);
57
+ show_usage_with_options_if_asked(argc, argv, builtin_diff_pairs_usage, parseopts);
58
+
59
+ repo_config(repo, git_diff_basic_config, NULL);
60
+ revs.disable_stdin = 1;
61
+ revs.abbrev = 0;
62
+ revs.diff = 1;
63
+
64
+ argc = parse_options(argc, argv, prefix, parseopts, builtin_diff_pairs_usage,
65
+ PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_DASHDASH);
66
+
67
+ if (setup_revisions(argc, argv, &revs, NULL) > 1)
68
+ usagef(_("unrecognized argument: %s"), argv[0]);
69
+
70
+ /*
71
+ * With the -z option, both command input and raw output are
72
+ * NUL-delimited (this mode does not affect patch output). At present
73
+ * only NUL-delimited raw diff formatted input is supported.
74
+ */
75
+ if (revs.diffopt.line_termination)
76
+ usage(_("working without -z is not supported"));
77
+
78
+ if (revs.prune_data.nr)
79
+ usage(_("pathspec arguments not supported"));
80
+
81
+ if (revs.pending.nr || revs.max_count != -1 ||
82
+ revs.min_age != (timestamp_t)-1 ||
83
+ revs.max_age != (timestamp_t)-1)
84
+ usage(_("revision arguments not allowed"));
85
+
86
+ if (!revs.diffopt.output_format)
87
+ revs.diffopt.output_format = DIFF_FORMAT_PATCH;
88
+
89
+ /*
90
+ * If rename detection is not requested, use rename information from the
91
+ * raw diff formatted input. Setting skip_resolving_statuses ensures
92
+ * diffcore_std() does not mess with rename information already present
93
+ * in queued filepairs.
94
+ */
95
+ if (!revs.diffopt.detect_rename)
96
+ revs.diffopt.skip_resolving_statuses = 1;
97
+
98
+ while (1) {
99
+ struct object_id oid_a, oid_b;
100
+ struct diff_filepair *pair;
101
+ unsigned mode_a, mode_b;
102
+ const char *p;
103
+ char status;
104
+
105
+ if (strbuf_getwholeline(&meta, stdin, line_term) == EOF)
106
+ break;
107
+
108
+ p = meta.buf;
109
+ if (*p != ':')
110
+ die(_("invalid raw diff input"));
111
+ p++;
112
+
113
+ mode_a = parse_mode_or_die(p, &p);
114
+ mode_b = parse_mode_or_die(p, &p);
115
+
116
+ if (S_ISDIR(mode_a) || S_ISDIR(mode_b))
117
+ die(_("tree objects not supported"));
118
+
119
+ parse_oid_or_die(p, &oid_a, &p, repo->hash_algo);
120
+ parse_oid_or_die(p, &oid_b, &p, repo->hash_algo);
121
+
122
+ status = *p++;
123
+
124
+ if (strbuf_getwholeline(&path, stdin, line_term) == EOF)
125
+ die(_("got EOF while reading path"));
126
+
127
+ switch (status) {
128
+ case DIFF_STATUS_ADDED:
129
+ pair = diff_queue_addremove(&diff_queued_diff,
130
+ &revs.diffopt, '+', mode_b,
131
+ &oid_b, 1, path.buf, 0);
132
+ if (pair)
133
+ pair->status = status;
134
+ break;
135
+
136
+ case DIFF_STATUS_DELETED:
137
+ pair = diff_queue_addremove(&diff_queued_diff,
138
+ &revs.diffopt, '-', mode_a,
139
+ &oid_a, 1, path.buf, 0);
140
+ if (pair)
141
+ pair->status = status;
142
+ break;
143
+
144
+ case DIFF_STATUS_TYPE_CHANGED:
145
+ case DIFF_STATUS_MODIFIED:
146
+ pair = diff_queue_change(&diff_queued_diff, &revs.diffopt,
147
+ mode_a, mode_b, &oid_a, &oid_b,
148
+ 1, 1, path.buf, 0, 0);
149
+ if (pair)
150
+ pair->status = status;
151
+ break;
152
+
153
+ case DIFF_STATUS_RENAMED:
154
+ case DIFF_STATUS_COPIED: {
155
+ struct diff_filespec *a, *b;
156
+ unsigned int score;
157
+
158
+ if (strbuf_getwholeline(&path_dst, stdin, line_term) == EOF)
159
+ die(_("got EOF while reading destination path"));
160
+
161
+ a = alloc_filespec(path.buf);
162
+ b = alloc_filespec(path_dst.buf);
163
+ fill_filespec(a, &oid_a, 1, mode_a);
164
+ fill_filespec(b, &oid_b, 1, mode_b);
165
+
166
+ pair = diff_queue(&diff_queued_diff, a, b);
167
+
168
+ if (strtoul_ui(p, 10, &score))
169
+ die(_("unable to parse rename/copy score: %s"), p);
170
+
171
+ pair->score = score * MAX_SCORE / 100;
172
+ pair->status = status;
173
+ pair->renamed_pair = 1;
174
+ }
175
+ break;
176
+
177
+ default:
178
+ die(_("unknown diff status: %c"), status);
179
+ }
180
+ }
181
+
182
+ diffcore_std(&revs.diffopt);
183
+ diff_flush(&revs.diffopt);
184
+ ret = diff_result_code(&revs);
185
+
186
+ strbuf_release(&path_dst);
187
+ strbuf_release(&path);
188
+ strbuf_release(&meta);
189
+ release_revisions(&revs);
190
+ FREE_AND_NULL(parseopts);
191
+
192
+ return ret;
193
+}