🌐 AI搜索 & 代理 主页
blob: 0054883a5e18aaacc682bb8369c6f0376cf75306 [file] [log] [blame]
Linus Torvaldse74f8f62005-04-20 04:00:091#include "cache.h"
Junio C Hamanof92a4462005-04-27 16:21:002#include "diff.h"
Linus Torvaldse74f8f62005-04-20 04:00:093
4static int cached_only = 0;
Linus Torvalds160c8432005-05-05 16:31:095static int match_nonexisting = 0;
Junio C Hamano6b5ee132005-09-21 07:00:476static struct diff_options diff_options;
Linus Torvaldse74f8f62005-04-20 04:00:097
Linus Torvaldse74f8f62005-04-20 04:00:098/* A file entry went away or appeared */
Junio C Hamano6b5ee132005-09-21 07:00:479static void show_file(const char *prefix,
10 struct cache_entry *ce,
11 unsigned char *sha1, unsigned int mode)
Linus Torvaldse74f8f62005-04-20 04:00:0912{
Junio C Hamano6b5ee132005-09-21 07:00:4713 diff_addremove(&diff_options, prefix[0], ntohl(mode),
14 sha1, ce->name, NULL);
Linus Torvaldse74f8f62005-04-20 04:00:0915}
16
Junio C Hamano6b5ee132005-09-21 07:00:4717static int get_stat_data(struct cache_entry *ce,
Junio C Hamano3e09cdf2005-10-12 01:45:3318 unsigned char ** sha1p, unsigned int *modep)
Linus Torvaldse74f8f62005-04-20 04:00:0919{
Linus Torvaldsc9cddab2005-04-27 16:41:1520 unsigned char *sha1 = ce->sha1;
21 unsigned int mode = ce->ce_mode;
Linus Torvaldse74f8f62005-04-20 04:00:0922
23 if (!cached_only) {
24 static unsigned char no_sha1[20];
Linus Torvaldsb5af9102005-04-23 00:15:2825 int changed;
Linus Torvaldse74f8f62005-04-20 04:00:0926 struct stat st;
Linus Torvalds160c8432005-05-05 16:31:0927 if (lstat(ce->name, &st) < 0) {
28 if (errno == ENOENT && match_nonexisting) {
29 *sha1p = sha1;
30 *modep = mode;
31 return 0;
32 }
Linus Torvaldse74f8f62005-04-20 04:00:0933 return -1;
Linus Torvalds160c8432005-05-05 16:31:0934 }
Brad Roberts5d728c82005-05-15 02:04:2535 changed = ce_match_stat(ce, &st);
Linus Torvaldse74f8f62005-04-20 04:00:0936 if (changed) {
Linus Torvaldsc9cddab2005-04-27 16:41:1537 mode = create_ce_mode(st.st_mode);
Junio C Hamano3e09cdf2005-10-12 01:45:3338 if (!trust_executable_bit &&
39 S_ISREG(mode) && S_ISREG(ce->ce_mode) &&
40 ((mode ^ ce->ce_mode) == 0111))
41 mode = ce->ce_mode;
Linus Torvaldsb5af9102005-04-23 00:15:2842 sha1 = no_sha1;
Linus Torvaldse74f8f62005-04-20 04:00:0943 }
44 }
45
Linus Torvaldsc9cddab2005-04-27 16:41:1546 *sha1p = sha1;
47 *modep = mode;
48 return 0;
49}
50
Junio C Hamanob8368252005-04-28 17:13:0151static void show_new_file(struct cache_entry *new)
Linus Torvaldsc9cddab2005-04-27 16:41:1552{
53 unsigned char *sha1;
54 unsigned int mode;
55
Junio C Hamano3e09cdf2005-10-12 01:45:3356 /* New file in the index: it might actually be different in
57 * the working copy.
58 */
Linus Torvaldsc9cddab2005-04-27 16:41:1559 if (get_stat_data(new, &sha1, &mode) < 0)
Junio C Hamanob8368252005-04-28 17:13:0160 return;
Linus Torvaldsc9cddab2005-04-27 16:41:1561
62 show_file("+", new, sha1, mode);
63}
64
Junio C Hamano66026592005-05-05 22:35:4965static int show_modified(struct cache_entry *old,
66 struct cache_entry *new,
67 int report_missing)
Linus Torvaldsc9cddab2005-04-27 16:41:1568{
69 unsigned int mode, oldmode;
70 unsigned char *sha1;
Linus Torvaldsc9cddab2005-04-27 16:41:1571
72 if (get_stat_data(new, &sha1, &mode) < 0) {
Junio C Hamano66026592005-05-05 22:35:4973 if (report_missing)
74 show_file("-", old, old->sha1, old->ce_mode);
Linus Torvaldsc9cddab2005-04-27 16:41:1575 return -1;
76 }
77
78 oldmode = old->ce_mode;
Junio C Hamanoc3e7fbc2005-05-21 09:42:3579 if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
Junio C Hamano6b5ee132005-09-21 07:00:4780 !diff_options.find_copies_harder)
Linus Torvaldse74f8f62005-04-20 04:00:0981 return 0;
82
Linus Torvaldsc9cddab2005-04-27 16:41:1583 mode = ntohl(mode);
84 oldmode = ntohl(oldmode);
85
Junio C Hamano6b5ee132005-09-21 07:00:4786 diff_change(&diff_options, oldmode, mode,
Junio C Hamano57fe64a2005-05-20 02:00:3687 old->sha1, sha1, old->name, NULL);
Linus Torvaldse74f8f62005-04-20 04:00:0988 return 0;
89}
90
Linus Torvaldsfdee7d02005-07-14 23:43:0191static int diff_cache(struct cache_entry **ac, int entries, const char **pathspec)
Linus Torvaldse74f8f62005-04-20 04:00:0992{
Linus Torvaldsb5af9102005-04-23 00:15:2893 while (entries) {
94 struct cache_entry *ce = *ac;
Brad Robertsdbbce552005-05-15 02:04:2595 int same = (entries > 1) && ce_same_name(ce, ac[1]);
Linus Torvaldse74f8f62005-04-20 04:00:0996
Linus Torvaldsfdee7d02005-07-14 23:43:0197 if (!ce_path_match(ce, pathspec))
98 goto skip_entry;
99
Linus Torvaldsb0fe89c2005-04-27 01:03:07100 switch (ce_stage(ce)) {
101 case 0:
102 /* No stage 1 entry? That means it's a new file */
103 if (!same) {
Linus Torvaldsc9cddab2005-04-27 16:41:15104 show_new_file(ce);
Linus Torvaldsb0fe89c2005-04-27 01:03:07105 break;
106 }
107 /* Show difference between old and new */
Junio C Hamano66026592005-05-05 22:35:49108 show_modified(ac[1], ce, 1);
Linus Torvaldsb0fe89c2005-04-27 01:03:07109 break;
110 case 1:
111 /* No stage 3 (merge) entry? That means it's been deleted */
112 if (!same) {
Linus Torvaldsc9cddab2005-04-27 16:41:15113 show_file("-", ce, ce->sha1, ce->ce_mode);
Linus Torvaldsb0fe89c2005-04-27 01:03:07114 break;
115 }
Junio C Hamano66026592005-05-05 22:35:49116 /* We come here with ce pointing at stage 1
117 * (original tree) and ac[1] pointing at stage
118 * 3 (unmerged). show-modified with
119 * report-mising set to false does not say the
120 * file is deleted but reports true if work
121 * tree does not have it, in which case we
122 * fall through to report the unmerged state.
123 * Otherwise, we show the differences between
124 * the original tree and the work tree.
125 */
126 if (!cached_only && !show_modified(ce, ac[1], 0))
127 break;
128 /* fallthru */
Linus Torvaldsb0fe89c2005-04-27 01:03:07129 case 3:
Junio C Hamano6b5ee132005-09-21 07:00:47130 diff_unmerge(&diff_options, ce->name);
Linus Torvaldsb0fe89c2005-04-27 01:03:07131 break;
132
133 default:
134 die("impossible cache entry stage");
135 }
136
Linus Torvaldsfdee7d02005-07-14 23:43:01137skip_entry:
Linus Torvaldsb0fe89c2005-04-27 01:03:07138 /*
139 * Ignore all the different stages for this file,
140 * we've handled the relevant cases now.
141 */
142 do {
Linus Torvaldse74f8f62005-04-20 04:00:09143 ac++;
144 entries--;
Brad Robertsdbbce552005-05-15 02:04:25145 } while (entries && ce_same_name(ce, ac[0]));
Linus Torvaldse74f8f62005-04-20 04:00:09146 }
147 return 0;
148}
149
Linus Torvaldsb0fe89c2005-04-27 01:03:07150/*
151 * This turns all merge entries into "stage 3". That guarantees that
152 * when we read in the new tree (into "stage 1"), we won't lose sight
153 * of the fact that we had unmerged entries.
154 */
155static void mark_merge_entries(void)
Linus Torvaldsb5af9102005-04-23 00:15:28156{
157 int i;
158 for (i = 0; i < active_nr; i++) {
159 struct cache_entry *ce = active_cache[i];
160 if (!ce_stage(ce))
Junio C Hamano5697ecc2005-04-26 17:13:31161 continue;
Linus Torvaldsb0fe89c2005-04-27 01:03:07162 ce->ce_flags |= htons(CE_STAGEMASK);
Linus Torvaldsb5af9102005-04-23 00:15:28163 }
164}
165
Petr Baudis4d1f1192005-07-29 09:01:26166static const char diff_cache_usage[] =
Junio C Hamano215a7ad2005-09-08 00:26:23167"git-diff-index [-m] [--cached] "
Junio C Hamanodda2d792005-07-13 19:52:35168"[<common diff options>] <tree-ish> [<path>...]"
169COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoc5bac172005-04-21 02:49:16170
Junio C Hamano6b5ee132005-09-21 07:00:47171int main(int argc, const char **argv)
Linus Torvaldse74f8f62005-04-20 04:00:09172{
Linus Torvalds6c56c532005-05-25 01:10:11173 const char *tree_name = NULL;
174 unsigned char sha1[20];
Linus Torvaldsd288a702005-08-17 01:06:34175 const char *prefix = setup_git_directory();
Linus Torvalds6c56c532005-05-25 01:10:11176 const char **pathspec = NULL;
Linus Torvaldse74f8f62005-04-20 04:00:09177 void *tree;
178 unsigned long size;
Junio C Hamano5c975582005-05-19 10:32:35179 int ret;
Jonas Fonsecae0f0e892005-06-06 21:27:00180 int allow_options = 1;
Linus Torvalds6c56c532005-05-25 01:10:11181 int i;
Linus Torvaldse74f8f62005-04-20 04:00:09182
Junio C Hamano9ce392f2005-11-22 06:52:37183 git_config(git_diff_config);
Junio C Hamano6b5ee132005-09-21 07:00:47184 diff_setup(&diff_options);
Linus Torvalds6c56c532005-05-25 01:10:11185 for (i = 1; i < argc; i++) {
186 const char *arg = argv[i];
Junio C Hamano6b5ee132005-09-21 07:00:47187 int diff_opt_cnt;
Linus Torvalds6c56c532005-05-25 01:10:11188
Jonas Fonsecae0f0e892005-06-06 21:27:00189 if (!allow_options || *arg != '-') {
Linus Torvaldsd288a702005-08-17 01:06:34190 if (tree_name)
Linus Torvalds6c56c532005-05-25 01:10:11191 break;
Linus Torvalds6c56c532005-05-25 01:10:11192 tree_name = arg;
193 continue;
194 }
195
Jonas Fonsecae0f0e892005-06-06 21:27:00196 if (!strcmp(arg, "--")) {
197 allow_options = 0;
198 continue;
199 }
Linus Torvaldse74f8f62005-04-20 04:00:09200 if (!strcmp(arg, "-r")) {
Alexey Nezhdanov667bb592005-05-19 11:17:16201 /* We accept the -r flag just to look like git-diff-tree */
Linus Torvaldse74f8f62005-04-20 04:00:09202 continue;
203 }
Junio C Hamano6b5ee132005-09-21 07:00:47204 diff_opt_cnt = diff_opt_parse(&diff_options, argv + i,
205 argc - i);
206 if (diff_opt_cnt < 0)
207 usage(diff_cache_usage);
208 else if (diff_opt_cnt) {
209 i += diff_opt_cnt - 1;
Junio C Hamanof92a4462005-04-27 16:21:00210 continue;
211 }
Junio C Hamano6b5ee132005-09-21 07:00:47212
Linus Torvalds160c8432005-05-05 16:31:09213 if (!strcmp(arg, "-m")) {
214 match_nonexisting = 1;
215 continue;
216 }
Linus Torvaldse74f8f62005-04-20 04:00:09217 if (!strcmp(arg, "--cached")) {
218 cached_only = 1;
219 continue;
220 }
Junio C Hamanoc5bac172005-04-21 02:49:16221 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-20 04:00:09222 }
223
Linus Torvaldsd288a702005-08-17 01:06:34224 pathspec = get_pathspec(prefix, argv + i);
225
Junio C Hamano6b5ee132005-09-21 07:00:47226 if (diff_setup_done(&diff_options) < 0)
Junio C Hamano4727f642005-06-19 20:14:05227 usage(diff_cache_usage);
228
Linus Torvalds6c56c532005-05-25 01:10:11229 if (!tree_name || get_sha1(tree_name, sha1))
Junio C Hamanoc5bac172005-04-21 02:49:16230 usage(diff_cache_usage);
Linus Torvaldse74f8f62005-04-20 04:00:09231
Linus Torvaldsd288a702005-08-17 01:06:34232 read_cache();
233
Linus Torvaldsb0fe89c2005-04-27 01:03:07234 mark_merge_entries();
Linus Torvaldsb5af9102005-04-23 00:15:28235
Linus Torvalds6c56c532005-05-25 01:10:11236 tree = read_object_with_reference(sha1, "tree", &size, NULL);
Linus Torvaldse74f8f62005-04-20 04:00:09237 if (!tree)
Linus Torvalds6c56c532005-05-25 01:10:11238 die("bad tree object %s", tree_name);
Linus Torvaldsa74ba542005-07-14 20:19:19239 if (read_tree(tree, size, 1, pathspec))
Linus Torvalds6c56c532005-05-25 01:10:11240 die("unable to read tree object %s", tree_name);
Linus Torvaldse74f8f62005-04-20 04:00:09241
Linus Torvaldsfdee7d02005-07-14 23:43:01242 ret = diff_cache(active_cache, active_nr, pathspec);
Junio C Hamanof345b0a2005-05-30 07:08:37243
Junio C Hamano6b5ee132005-09-21 07:00:47244 diffcore_std(&diff_options);
245 diff_flush(&diff_options);
Junio C Hamano5c975582005-05-19 10:32:35246 return ret;
Linus Torvaldse74f8f62005-04-20 04:00:09247}