]> git.ipfire.org Git - thirdparty/git.git/blame - diff-files.c
GIT 1.1.1
[thirdparty/git.git] / diff-files.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
4a6bf9e1 7#include "diff.h"
c0fb976a 8
4d1f1190 9static const char diff_files_usage[] =
10637b84 10"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"
dda2d79a 11COMMON_DIFF_OPTIONS_HELP;
b8f80925 12
6b5ee137 13static struct diff_options diff_options;
0a7668e9 14static int silent = 0;
15bf57a1 15static int diff_unmerged_stage = 2;
0a7668e9 16
4a6bf9e1 17static void show_unmerge(const char *path)
0a7668e9 18{
6b5ee137 19 diff_unmerge(&diff_options, path);
4a6bf9e1
JH
20}
21
22static void show_file(int pfx, struct cache_entry *ce)
23{
6b5ee137
JH
24 diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25 ce->sha1, ce->name, NULL);
4a6bf9e1
JH
26}
27
28static void show_modified(int oldmode, int mode,
bf0f910d 29 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
30 char *path)
31{
6b5ee137 32 diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
0a7668e9
LT
33}
34
6b5ee137 35int main(int argc, const char **argv)
e83c5163 36{
c0fd1f51 37 const char **pathspec;
d288a700
LT
38 const char *prefix = setup_git_directory();
39 int entries, i;
e83c5163 40
9ce392f4 41 git_config(git_diff_config);
6b5ee137 42 diff_setup(&diff_options);
b8f80925 43 while (1 < argc && argv[1][0] == '-') {
ea51d416
LT
44 if (!strcmp(argv[1], "--")) {
45 argv++;
46 argc--;
47 break;
48 }
10637b84
LT
49 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"))
d15aa430 64 silent = 1;
0a7668e9 65 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 66 ; /* no-op */
d15aa430
JH
67 else if (!strcmp(argv[1], "-s"))
68 ; /* no-op */
6b5ee137
JH
69 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)
0e3994fa 74 usage(diff_files_usage);
6b5ee137
JH
75 else if (diff_opt_cnt) {
76 argv += diff_opt_cnt;
77 argc -= diff_opt_cnt;
78 continue;
79 }
80 else
0e3994fa 81 usage(diff_files_usage);
427dcb4b 82 }
b8f80925 83 argv++; argc--;
e2e5e98a
PB
84 }
85
d288a700
LT
86 /* Find the directory, and set up the pathspec */
87 pathspec = get_pathspec(prefix, argv + 1);
88 entries = read_cache();
c0fd1f51 89
6b5ee137 90 if (diff_setup_done(&diff_options) < 0)
4727f640
JH
91 usage(diff_files_usage);
92
b8f80925
JH
93 /* At this point, if argc == 1, then we are doing everything.
94 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
95 */
e83c5163
LT
96 if (entries < 0) {
97 perror("read_cache");
98 exit(1);
99 }
be3cfa85 100
e83c5163
LT
101 for (i = 0; i < entries; i++) {
102 struct stat st;
3e09cdfd 103 unsigned int oldmode, newmode;
e83c5163 104 struct cache_entry *ce = active_cache[i];
d94c6128 105 int changed;
e83c5163 106
c0fd1f51
LT
107 if (!ce_path_match(ce, pathspec))
108 continue;
109
9fec8b26 110 if (ce_stage(ce)) {
15bf57a1 111 show_unmerge(ce->name);
10637b84
LT
112 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;
9fec8b26 120 i++;
10637b84
LT
121 }
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;
9fec8b26 132 }
57fe64a4 133
ffbe1add 134 if (lstat(ce->name, &st) < 0) {
41174694 135 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 136 perror(ce->name);
ca2a0798 137 continue;
57fe64a4 138 }
d15aa430 139 if (silent)
0a7668e9 140 continue;
4a6bf9e1 141 show_file('-', ce);
e83c5163
LT
142 continue;
143 }
5d728c84 144 changed = ce_match_stat(ce, &st);
6b5ee137 145 if (!changed && !diff_options.find_copies_harder)
e83c5163 146 continue;
0a7668e9 147 oldmode = ntohl(ce->ce_mode);
3e09cdfd
JH
148
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,
4727f640 155 ce->sha1, (changed ? null_sha1 : ce->sha1),
4a6bf9e1 156 ce->name);
e83c5163 157 }
6b5ee137
JH
158 diffcore_std(&diff_options);
159 diff_flush(&diff_options);
e83c5163
LT
160 return 0;
161}