Diff clean-up.
[git/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[] =
dda2d79a
JH
10"git-diff-files [-q] "
11"[<common diff options>] [<path>...]"
12COMMON_DIFF_OPTIONS_HELP;
b8f80925 13
6b5ee137 14static struct diff_options diff_options;
0a7668e9 15static int silent = 0;
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{
bf0f910d 37 static const unsigned char null_sha1[20] = { 0, };
c0fd1f51 38 const char **pathspec;
d288a700
LT
39 const char *prefix = setup_git_directory();
40 int entries, i;
e83c5163 41
6b5ee137 42 diff_setup(&diff_options);
b8f80925 43 while (1 < argc && argv[1][0] == '-') {
6b5ee137 44 if (!strcmp(argv[1], "-q"))
d15aa430 45 silent = 1;
0a7668e9 46 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 47 ; /* no-op */
d15aa430
JH
48 else if (!strcmp(argv[1], "-s"))
49 ; /* no-op */
6b5ee137
JH
50 else {
51 int diff_opt_cnt;
52 diff_opt_cnt = diff_opt_parse(&diff_options,
53 argv+1, argc-1);
54 if (diff_opt_cnt < 0)
0e3994fa 55 usage(diff_files_usage);
6b5ee137
JH
56 else if (diff_opt_cnt) {
57 argv += diff_opt_cnt;
58 argc -= diff_opt_cnt;
59 continue;
60 }
61 else
0e3994fa 62 usage(diff_files_usage);
427dcb4b 63 }
b8f80925 64 argv++; argc--;
e2e5e98a
PB
65 }
66
d288a700
LT
67 /* Find the directory, and set up the pathspec */
68 pathspec = get_pathspec(prefix, argv + 1);
69 entries = read_cache();
c0fd1f51 70
6b5ee137 71 if (diff_setup_done(&diff_options) < 0)
4727f640
JH
72 usage(diff_files_usage);
73
b8f80925
JH
74 /* At this point, if argc == 1, then we are doing everything.
75 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
76 */
e83c5163
LT
77 if (entries < 0) {
78 perror("read_cache");
79 exit(1);
80 }
be3cfa85 81
e83c5163
LT
82 for (i = 0; i < entries; i++) {
83 struct stat st;
67574c40 84 unsigned int oldmode;
e83c5163 85 struct cache_entry *ce = active_cache[i];
d94c6128 86 int changed;
e83c5163 87
c0fd1f51
LT
88 if (!ce_path_match(ce, pathspec))
89 continue;
90
9fec8b26 91 if (ce_stage(ce)) {
4a6bf9e1 92 show_unmerge(ce->name);
9fec8b26
JH
93 while (i < entries &&
94 !strcmp(ce->name, active_cache[i]->name))
95 i++;
96 i--; /* compensate for loop control increments */
97 continue;
98 }
57fe64a4 99
ffbe1add 100 if (lstat(ce->name, &st) < 0) {
41174694 101 if (errno != ENOENT && errno != ENOTDIR) {
0a7668e9 102 perror(ce->name);
ca2a0798 103 continue;
57fe64a4 104 }
d15aa430 105 if (silent)
0a7668e9 106 continue;
4a6bf9e1 107 show_file('-', ce);
e83c5163
LT
108 continue;
109 }
5d728c84 110 changed = ce_match_stat(ce, &st);
6b5ee137 111 if (!changed && !diff_options.find_copies_harder)
e83c5163 112 continue;
0a7668e9 113 oldmode = ntohl(ce->ce_mode);
67574c40 114 show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
4727f640 115 ce->sha1, (changed ? null_sha1 : ce->sha1),
4a6bf9e1 116 ce->name);
e83c5163 117 }
6b5ee137
JH
118 diffcore_std(&diff_options);
119 diff_flush(&diff_options);
e83c5163
LT
120 return 0;
121}