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