tree-diff: move all action-taking code out of compare_tree_entry()

- let it do only comparison. This way the code is cleaner and more structured - cmp function only compares, and the driver takes action based on comparison result. There should be no change in performance, as effectively, we just move if series from on place into another, and merge it to was-already-there same switch/if, so the result is maybe a little bit faster. Signed-off-by: Kirill Smelkov <kirr@mns.spb.ru> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kirill Smelkov committed Feb 24, 2014 at 20:21 UTC 903bba68abb8153f181d854e9a7075b893f1303f
1 file changed +12 -16
tree-diff.c
+12 -16
@@ -9,8 +9,7 @@
9 static void show_path(struct strbuf *base, struct diff_options *opt,
10 struct tree_desc *t1, struct tree_desc *t2);
11
12 -static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
13 - struct strbuf *base, struct diff_options *opt)
12 +static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2)
13 {
14 unsigned mode1, mode2;
15 const char *path1, *path2;
@@ -28,19 +27,7 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
27 * even when having the same name.
28 */
29 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
31 - if (cmp < 0) {
32 - show_path(base, opt, t1, /*t2=*/NULL);
33 - return -1;
34 - }
35 - if (cmp > 0) {
36 - show_path(base, opt, /*t1=*/NULL, t2);
37 - return 1;
38 - }
39 - if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
40 - return 0;
41 -
42 - show_path(base, opt, t1, t2);
43 - return 0;
30 + return cmp;
31 }
32
33
@@ -160,6 +147,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
147 strbuf_add(&base, base_str, baselen);
148
149 for (;;) {
150 + int cmp;
151 +
152 if (diff_can_quit_early(opt))
153 break;
154 if (opt->pathspec.nr) {
@@ -179,21 +168,28 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
168 continue;
169 }
170
182 - cmp = compare_tree_entry(t1, t2, &base, opt);
171 + cmp = compare_tree_entry(t1, t2);
172
173 /* t1 = t2 */
174 if (cmp == 0) {
175 + if (DIFF_OPT_TST(opt, FIND_COPIES_HARDER) ||
176 + hashcmp(t1->entry.sha1, t2->entry.sha1) ||
177 + (t1->entry.mode != t2->entry.mode))
178 + show_path(&base, opt, t1, t2);
179 +
180 update_tree_entry(t1);
181 update_tree_entry(t2);
182 }
183
184 /* t1 < t2 */
185 else if (cmp < 0) {
186 + show_path(&base, opt, t1, /*t2=*/NULL);
187 update_tree_entry(t1);
188 }
189
190 /* t1 > t2 */
191 else {
192 + show_path(&base, opt, /*t1=*/NULL, t2);
193 update_tree_entry(t2);
194 }
195 }