🌐 AI搜索 & 代理 主页
blob: 6c0696c34f0af95acbfe06f644f7df7fb1e93509 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 22:16:101/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 22:13:136#include "cache.h"
Junio C Hamano4a6bf9e2005-04-27 16:21:007#include "diff.h"
Christopher Lic0fb9762005-04-12 09:04:448
Petr Baudis4d1f1192005-07-29 09:01:269static const char diff_files_usage[] =
Linus Torvalds10637b82005-11-30 05:06:1010"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"
Junio C Hamanodda2d792005-07-13 19:52:3511COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanob8f80922005-04-17 04:29:4512
Junio C Hamano6b5ee132005-09-21 07:00:4713static struct diff_options diff_options;
Linus Torvalds0a7668e2005-04-27 00:17:3614static int silent = 0;
Junio C Hamano15bf57a2005-11-30 10:16:3615static int diff_unmerged_stage = 2;
Linus Torvalds0a7668e2005-04-27 00:17:3616
Junio C Hamano4a6bf9e2005-04-27 16:21:0017static void show_unmerge(const char *path)
Linus Torvalds0a7668e2005-04-27 00:17:3618{
Junio C Hamano6b5ee132005-09-21 07:00:4719 diff_unmerge(&diff_options, path);
Junio C Hamano4a6bf9e2005-04-27 16:21:0020}
21
22static void show_file(int pfx, struct cache_entry *ce)
23{
Junio C Hamano6b5ee132005-09-21 07:00:4724 diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 ce->sha1, ce->name, NULL);
Junio C Hamano4a6bf9e2005-04-27 16:21:0026}
27
28static void show_modified(int oldmode, int mode,
Brian Gerstbf0f9102005-05-18 12:14:0929 const unsigned char *old_sha1, const unsigned char *sha1,
Junio C Hamano4a6bf9e2005-04-27 16:21:0030 char *path)
31{
Junio C Hamano6b5ee132005-09-21 07:00:4732 diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
Linus Torvalds0a7668e2005-04-27 00:17:3633}
34
Junio C Hamano6b5ee132005-09-21 07:00:4735int main(int argc, const char **argv)
Linus Torvaldse83c5162005-04-07 22:13:1336{
Linus Torvaldsc0fd1f52005-07-14 23:55:0637 const char **pathspec;
Linus Torvaldsd288a702005-08-17 01:06:3438 const char *prefix = setup_git_directory();
39 int entries, i;
Linus Torvaldse83c5162005-04-07 22:13:1340
Junio C Hamano9ce392f2005-11-22 06:52:3741 git_config(git_diff_config);
Junio C Hamano6b5ee132005-09-21 07:00:4742 diff_setup(&diff_options);
Junio C Hamanob8f80922005-04-17 04:29:4543 while (1 < argc && argv[1][0] == '-') {
Linus Torvaldsea51d412005-10-18 07:16:4544 if (!strcmp(argv[1], "--")) {
45 argv++;
46 argc--;
47 break;
48 }
Linus Torvalds10637b82005-11-30 05:06:1049 if (!strcmp(argv[1], "-0"))
50 diff_unmerged_stage = 0;
51 else if (!strcmp(argv[1], "-1"))
52 diff_unmerged_stage = 1;
53 else if (!strcmp(argv[1], "-2"))
54 diff_unmerged_stage = 2;
55 else if (!strcmp(argv[1], "-3"))
56 diff_unmerged_stage = 3;
57 else if (!strcmp(argv[1], "--base"))
58 diff_unmerged_stage = 1;
59 else if (!strcmp(argv[1], "--ours"))
60 diff_unmerged_stage = 2;
61 else if (!strcmp(argv[1], "--theirs"))
62 diff_unmerged_stage = 3;
63 else if (!strcmp(argv[1], "-q"))
Junio C Hamanod15aa432005-04-27 22:22:0264 silent = 1;
Linus Torvalds0a7668e2005-04-27 00:17:3665 else if (!strcmp(argv[1], "-r"))
Junio C Hamano4a6bf9e2005-04-27 16:21:0066 ; /* no-op */
Junio C Hamanod15aa432005-04-27 22:22:0267 else if (!strcmp(argv[1], "-s"))
68 ; /* no-op */
Junio C Hamano6b5ee132005-09-21 07:00:4769 else {
70 int diff_opt_cnt;
71 diff_opt_cnt = diff_opt_parse(&diff_options,
72 argv+1, argc-1);
73 if (diff_opt_cnt < 0)
74 usage(diff_files_usage);
75 else if (diff_opt_cnt) {
76 argv += diff_opt_cnt;
77 argc -= diff_opt_cnt;
78 continue;
79 }
80 else
Junio C Hamano0e3994f2005-06-03 08:37:5481 usage(diff_files_usage);
82 }
Junio C Hamanob8f80922005-04-17 04:29:4583 argv++; argc--;
Petr Baudise2e5e982005-04-13 08:40:0984 }
85
Linus Torvaldsd288a702005-08-17 01:06:3486 /* Find the directory, and set up the pathspec */
87 pathspec = get_pathspec(prefix, argv + 1);
88 entries = read_cache();
Linus Torvaldsc0fd1f52005-07-14 23:55:0689
Junio C Hamano6b5ee132005-09-21 07:00:4790 if (diff_setup_done(&diff_options) < 0)
Junio C Hamano4727f642005-06-19 20:14:0591 usage(diff_files_usage);
92
Junio C Hamanob8f80922005-04-17 04:29:4593 /* At this point, if argc == 1, then we are doing everything.
94 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
95 */
Linus Torvaldse83c5162005-04-07 22:13:1396 if (entries < 0) {
97 perror("read_cache");
98 exit(1);
99 }
Junio C Hamanobe3cfa82005-04-26 16:25:05100
Linus Torvaldse83c5162005-04-07 22:13:13101 for (i = 0; i < entries; i++) {
102 struct stat st;
Junio C Hamano3e09cdf2005-10-12 01:45:33103 unsigned int oldmode, newmode;
Linus Torvaldse83c5162005-04-07 22:13:13104 struct cache_entry *ce = active_cache[i];
Junio C Hamanod94c6122005-04-17 04:29:45105 int changed;
Linus Torvaldse83c5162005-04-07 22:13:13106
Linus Torvaldsc0fd1f52005-07-14 23:55:06107 if (!ce_path_match(ce, pathspec))
108 continue;
109
Junio C Hamano9fec8b22005-04-17 04:29:45110 if (ce_stage(ce)) {
Junio C Hamano15bf57a2005-11-30 10:16:36111 show_unmerge(ce->name);
Linus Torvalds10637b82005-11-30 05:06:10112 while (i < entries) {
113 struct cache_entry *nce = active_cache[i];
114
115 if (strcmp(ce->name, nce->name))
116 break;
117 /* diff against the proper unmerged stage */
118 if (ce_stage(nce) == diff_unmerged_stage)
119 ce = nce;
Junio C Hamano9fec8b22005-04-17 04:29:45120 i++;
Linus Torvalds10637b82005-11-30 05:06:10121 }
122 /*
123 * Compensate for loop update
124 */
125 i--;
126 /*
127 * Show the diff for the 'ce' if we found the one
128 * from the desired stage.
129 */
130 if (ce_stage(ce) != diff_unmerged_stage)
131 continue;
Junio C Hamano9fec8b22005-04-17 04:29:45132 }
Junio C Hamano57fe64a2005-05-20 02:00:36133
Kay Sieversffbe1ad2005-05-06 13:45:01134 if (lstat(ce->name, &st) < 0) {
Junio C Hamano41174692005-05-20 16:48:38135 if (errno != ENOENT && errno != ENOTDIR) {
Linus Torvalds0a7668e2005-04-27 00:17:36136 perror(ce->name);
Junio C Hamanoca2a0792005-04-15 22:08:09137 continue;
Junio C Hamano57fe64a2005-05-20 02:00:36138 }
Junio C Hamanod15aa432005-04-27 22:22:02139 if (silent)
Linus Torvalds0a7668e2005-04-27 00:17:36140 continue;
Junio C Hamano4a6bf9e2005-04-27 16:21:00141 show_file('-', ce);
Linus Torvaldse83c5162005-04-07 22:13:13142 continue;
143 }
Brad Roberts5d728c82005-05-15 02:04:25144 changed = ce_match_stat(ce, &st);
Junio C Hamano6b5ee132005-09-21 07:00:47145 if (!changed && !diff_options.find_copies_harder)
Linus Torvaldse83c5162005-04-07 22:13:13146 continue;
Linus Torvalds0a7668e2005-04-27 00:17:36147 oldmode = ntohl(ce->ce_mode);
Junio C Hamano3e09cdf2005-10-12 01:45:33148
149 newmode = DIFF_FILE_CANON_MODE(st.st_mode);
150 if (!trust_executable_bit &&
151 S_ISREG(newmode) && S_ISREG(oldmode) &&
152 ((newmode ^ oldmode) == 0111))
153 newmode = oldmode;
154 show_modified(oldmode, newmode,
Junio C Hamano4727f642005-06-19 20:14:05155 ce->sha1, (changed ? null_sha1 : ce->sha1),
Junio C Hamano4a6bf9e2005-04-27 16:21:00156 ce->name);
Linus Torvaldse83c5162005-04-07 22:13:13157 }
Junio C Hamano6b5ee132005-09-21 07:00:47158 diffcore_std(&diff_options);
159 diff_flush(&diff_options);
Linus Torvaldse83c5162005-04-07 22:13:13160 return 0;
161}