Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for tree diff generation |
| 3 | */ |
| 4 | #include "cache.h" |
| 5 | #include "diff.h" |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 6 | #include "diffcore.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 7 | #include "tree.h" |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 8 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 9 | static void show_entry(struct diff_options *opt, const char *prefix, |
| 10 | struct tree_desc *desc, struct strbuf *base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 11 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 12 | static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, |
| 13 | struct strbuf *base, struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 14 | { |
| 15 | unsigned mode1, mode2; |
| 16 | const char *path1, *path2; |
| 17 | const unsigned char *sha1, *sha2; |
| 18 | int cmp, pathlen1, pathlen2; |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 19 | int old_baselen = base->len; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 20 | |
Linus Torvalds | 50f9a85 | 2006-01-31 14:10:56 -0800 | [diff] [blame] | 21 | sha1 = tree_entry_extract(t1, &path1, &mode1); |
| 22 | sha2 = tree_entry_extract(t2, &path2, &mode2); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 23 | |
Nguyễn Thái Ngọc Duy | 0de1633 | 2011-10-24 17:36:09 +1100 | [diff] [blame] | 24 | pathlen1 = tree_entry_len(&t1->entry); |
| 25 | pathlen2 = tree_entry_len(&t2->entry); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 26 | cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2); |
| 27 | if (cmp < 0) { |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 28 | show_entry(opt, "-", t1, base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 29 | return -1; |
| 30 | } |
| 31 | if (cmp > 0) { |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 32 | show_entry(opt, "+", t2, base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 33 | return 1; |
| 34 | } |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 35 | if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 36 | return 0; |
| 37 | |
| 38 | /* |
| 39 | * If the filemode has changed to/from a directory from/to a regular |
| 40 | * file, we need to consider it a remove and an add. |
| 41 | */ |
| 42 | if (S_ISDIR(mode1) != S_ISDIR(mode2)) { |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 43 | show_entry(opt, "-", t1, base); |
| 44 | show_entry(opt, "+", t2, base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 48 | strbuf_add(base, path1, pathlen1); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 49 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) { |
Dmitry Potapov | fd55a19 | 2008-07-16 18:54:02 +0400 | [diff] [blame] | 50 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) { |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 51 | opt->change(opt, mode1, mode2, |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 52 | sha1, sha2, 1, 1, base->buf, 0, 0); |
Dmitry Potapov | fd55a19 | 2008-07-16 18:54:02 +0400 | [diff] [blame] | 53 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 54 | strbuf_addch(base, '/'); |
Johannes Schindelin | c0aa335 | 2011-03-22 13:50:08 +0100 | [diff] [blame] | 55 | diff_tree_sha1(sha1, sha2, base->buf, opt); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 56 | } else { |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 57 | opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 58 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 59 | strbuf_setlen(base, old_baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 63 | /* A whole sub-tree went away or appeared */ |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 64 | static void show_tree(struct diff_options *opt, const char *prefix, |
| 65 | struct tree_desc *desc, struct strbuf *base) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 66 | { |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 67 | enum interesting match = entry_not_interesting; |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 68 | for (; desc->size; update_tree_entry(desc)) { |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 69 | if (match != all_entries_interesting) { |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 70 | match = tree_entry_interesting(&desc->entry, base, 0, |
| 71 | &opt->pathspec); |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 72 | if (match == all_entries_not_interesting) |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 73 | break; |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 74 | if (match == entry_not_interesting) |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 75 | continue; |
Junio C Hamano | 1d848f6 | 2007-03-21 17:00:27 -0700 | [diff] [blame] | 76 | } |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 77 | show_entry(opt, prefix, desc, base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | /* A file entry went away or appeared */ |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 82 | static void show_entry(struct diff_options *opt, const char *prefix, |
| 83 | struct tree_desc *desc, struct strbuf *base) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 84 | { |
| 85 | unsigned mode; |
| 86 | const char *path; |
Linus Torvalds | 50f9a85 | 2006-01-31 14:10:56 -0800 | [diff] [blame] | 87 | const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode); |
Nguyễn Thái Ngọc Duy | 0de1633 | 2011-10-24 17:36:09 +1100 | [diff] [blame] | 88 | int pathlen = tree_entry_len(&desc->entry); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 89 | int old_baselen = base->len; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 90 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 91 | strbuf_add(base, path, pathlen); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 92 | if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 93 | enum object_type type; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 94 | struct tree_desc inner; |
| 95 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 96 | unsigned long size; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 97 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 98 | tree = read_sha1_file(sha1, &type, &size); |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 99 | if (!tree || type != OBJ_TREE) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 100 | die("corrupt tree sha %s", sha1_to_hex(sha1)); |
| 101 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 102 | if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 103 | opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 104 | |
| 105 | strbuf_addch(base, '/'); |
Nick Edelen | df533f3 | 2009-06-13 17:06:09 -0700 | [diff] [blame] | 106 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 107 | init_tree_desc(&inner, tree, size); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 108 | show_tree(opt, prefix, &inner, base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 109 | free(tree); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 110 | } else |
Jeff King | e545010 | 2012-07-28 11:03:01 -0400 | [diff] [blame] | 111 | opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 112 | |
| 113 | strbuf_setlen(base, old_baselen); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 116 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 117 | struct diff_options *opt, |
| 118 | enum interesting *match) |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 119 | { |
| 120 | while (t->size) { |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 121 | *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec); |
| 122 | if (*match) { |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 123 | if (*match == all_entries_not_interesting) |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 124 | t->size = 0; |
| 125 | break; |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 126 | } |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 127 | update_tree_entry(t); |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 131 | int diff_tree(struct tree_desc *t1, struct tree_desc *t2, |
| 132 | const char *base_str, struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 133 | { |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 134 | struct strbuf base; |
| 135 | int baselen = strlen(base_str); |
Nguyễn Thái Ngọc Duy | d688cf0 | 2011-10-24 17:36:10 +1100 | [diff] [blame] | 136 | enum interesting t1_match = entry_not_interesting; |
| 137 | enum interesting t2_match = entry_not_interesting; |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 138 | |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 139 | /* Enable recursion indefinitely */ |
| 140 | opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE); |
| 141 | opt->pathspec.max_depth = -1; |
| 142 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 143 | strbuf_init(&base, PATH_MAX); |
| 144 | strbuf_add(&base, base_str, baselen); |
| 145 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 146 | for (;;) { |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 147 | if (diff_can_quit_early(opt)) |
Junio C Hamano | 822cac0 | 2007-03-14 11:12:51 -0700 | [diff] [blame] | 148 | break; |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 149 | if (opt->pathspec.nr) { |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 150 | skip_uninteresting(t1, &base, opt, &t1_match); |
| 151 | skip_uninteresting(t2, &base, opt, &t2_match); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 152 | } |
| 153 | if (!t1->size) { |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 154 | if (!t2->size) |
| 155 | break; |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 156 | show_entry(opt, "+", t2, &base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 157 | update_tree_entry(t2); |
| 158 | continue; |
| 159 | } |
| 160 | if (!t2->size) { |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 161 | show_entry(opt, "-", t1, &base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 162 | update_tree_entry(t1); |
| 163 | continue; |
| 164 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 165 | switch (compare_tree_entry(t1, t2, &base, opt)) { |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 166 | case -1: |
| 167 | update_tree_entry(t1); |
| 168 | continue; |
| 169 | case 0: |
| 170 | update_tree_entry(t1); |
| 171 | /* Fallthrough */ |
| 172 | case 1: |
| 173 | update_tree_entry(t2); |
| 174 | continue; |
| 175 | } |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 176 | die("git diff-tree: internal error"); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 177 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 178 | |
| 179 | strbuf_release(&base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 183 | /* |
| 184 | * Does it look like the resulting diff might be due to a rename? |
| 185 | * - single entry |
| 186 | * - not a valid previous file |
| 187 | */ |
| 188 | static inline int diff_might_be_rename(void) |
| 189 | { |
| 190 | return diff_queued_diff.nr == 1 && |
| 191 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); |
| 192 | } |
| 193 | |
| 194 | static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt) |
| 195 | { |
| 196 | struct diff_options diff_opts; |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 197 | struct diff_queue_struct *q = &diff_queued_diff; |
| 198 | struct diff_filepair *choice; |
| 199 | const char *paths[1]; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 200 | int i; |
| 201 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 202 | /* Remove the file creation entry from the diff queue, and remember it */ |
| 203 | choice = q->queue[0]; |
| 204 | q->nr = 0; |
| 205 | |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 206 | diff_setup(&diff_opts); |
Pierre Habouzit | 8f67f8a | 2007-11-10 20:05:14 +0100 | [diff] [blame] | 207 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
Bo Yang | 0cdca13 | 2010-05-06 21:52:29 -0700 | [diff] [blame] | 208 | DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 209 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 210 | diff_opts.single_follow = opt->pathspec.raw[0]; |
Linus Torvalds | 6dd4b66 | 2007-10-20 12:31:31 -0700 | [diff] [blame] | 211 | diff_opts.break_opt = opt->break_opt; |
Jeff King | dd98d88 | 2011-12-16 06:27:50 -0500 | [diff] [blame] | 212 | diff_opts.rename_score = opt->rename_score; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 213 | paths[0] = NULL; |
| 214 | diff_tree_setup_paths(paths, &diff_opts); |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 215 | diff_setup_done(&diff_opts); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 216 | diff_tree(t1, t2, base, &diff_opts); |
| 217 | diffcore_std(&diff_opts); |
Mike Hommey | 03b69c7 | 2007-12-11 22:59:55 +0100 | [diff] [blame] | 218 | diff_tree_release_paths(&diff_opts); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 220 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 221 | opt->found_follow = 0; |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 222 | for (i = 0; i < q->nr; i++) { |
| 223 | struct diff_filepair *p = q->queue[i]; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Found a source? Not only do we use that for the new |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 227 | * diff_queued_diff, we will also use that as the path in |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 228 | * the future! |
| 229 | */ |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 230 | if ((p->status == 'R' || p->status == 'C') && |
| 231 | !strcmp(p->two->path, opt->pathspec.raw[0])) { |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 232 | /* Switch the file-pairs around */ |
| 233 | q->queue[i] = choice; |
| 234 | choice = p; |
| 235 | |
| 236 | /* Update the path we use from now on.. */ |
Mike Hommey | 03b69c7 | 2007-12-11 22:59:55 +0100 | [diff] [blame] | 237 | diff_tree_release_paths(opt); |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 238 | opt->pathspec.raw[0] = xstrdup(p->one->path); |
| 239 | diff_tree_setup_paths(opt->pathspec.raw, opt); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * The caller expects us to return a set of vanilla |
| 243 | * filepairs to let a later call to diffcore_std() |
| 244 | * it makes to sort the renames out (among other |
| 245 | * things), but we already have found renames |
| 246 | * ourselves; signal diffcore_std() not to muck with |
| 247 | * rename information. |
| 248 | */ |
| 249 | opt->found_follow = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* |
Mike Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 255 | * Then, discard all the non-relevant file pairs... |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 256 | */ |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 257 | for (i = 0; i < q->nr; i++) { |
| 258 | struct diff_filepair *p = q->queue[i]; |
| 259 | diff_free_filepair(p); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * .. and re-instate the one we want (which might be either the |
| 264 | * original one, or the rename/copy we found) |
| 265 | */ |
| 266 | q->queue[0] = choice; |
| 267 | q->nr = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 270 | int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt) |
| 271 | { |
| 272 | void *tree1, *tree2; |
| 273 | struct tree_desc t1, t2; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 274 | unsigned long size1, size2; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 275 | int retval; |
| 276 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 277 | tree1 = read_object_with_reference(old, tree_type, &size1, NULL); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 278 | if (!tree1) |
| 279 | die("unable to read source tree (%s)", sha1_to_hex(old)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 280 | tree2 = read_object_with_reference(new, tree_type, &size2, NULL); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 281 | if (!tree2) |
| 282 | die("unable to read destination tree (%s)", sha1_to_hex(new)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 283 | init_tree_desc(&t1, tree1, size1); |
| 284 | init_tree_desc(&t2, tree2, size2); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 285 | retval = diff_tree(&t1, &t2, base, opt); |
Junio C Hamano | 39f75d2 | 2010-08-13 10:36:01 -0700 | [diff] [blame] | 286 | if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) { |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 287 | init_tree_desc(&t1, tree1, size1); |
| 288 | init_tree_desc(&t2, tree2, size2); |
| 289 | try_to_follow_renames(&t1, &t2, base, opt); |
| 290 | } |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 291 | free(tree1); |
| 292 | free(tree2); |
| 293 | return retval; |
| 294 | } |
| 295 | |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 296 | int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt) |
| 297 | { |
| 298 | int retval; |
| 299 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 300 | unsigned long size; |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 301 | struct tree_desc empty, real; |
| 302 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 303 | tree = read_object_with_reference(new, tree_type, &size, NULL); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 304 | if (!tree) |
| 305 | die("unable to read root tree (%s)", sha1_to_hex(new)); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 306 | init_tree_desc(&real, tree, size); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 307 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 308 | init_tree_desc(&empty, "", 0); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 309 | retval = diff_tree(&empty, &real, base, opt); |
| 310 | free(tree); |
| 311 | return retval; |
| 312 | } |
| 313 | |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 314 | void diff_tree_release_paths(struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 315 | { |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 316 | free_pathspec(&opt->pathspec); |
Junio C Hamano | a8baa7b | 2006-04-10 16:39:11 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void diff_tree_setup_paths(const char **p, struct diff_options *opt) |
| 320 | { |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 321 | init_pathspec(&opt->pathspec, p); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 322 | } |