]> git.ipfire.org Git - thirdparty/git.git/blame - diff-files.c
[PATCH] cleanup of in-code names
[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
17710391 9static const char *diff_files_usage =
667bb59b 10"git-diff-files [-p] [-q] [-r] [-z] [-M] [paths...]";
b8f80925 11
4a6bf9e1 12static int generate_patch = 0;
0a7668e9 13static int line_termination = '\n';
5c97558c 14static int detect_rename = 0;
0a7668e9 15static int silent = 0;
0a7668e9 16
b8f80925
JH
17static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)
18{
19 int i;
20 int namelen = ce_namelen(ce);
21 for (i = 0; i < cnt; i++) {
22 int speclen = strlen(spec[i]);
23 if (! strncmp(spec[i], ce->name, speclen) &&
24 speclen <= namelen &&
25 (ce->name[speclen] == 0 ||
26 ce->name[speclen] == '/'))
27 return 1;
28 }
29 return 0;
30}
31
4a6bf9e1 32static void show_unmerge(const char *path)
0a7668e9 33{
4a6bf9e1
JH
34 if (generate_patch)
35 diff_unmerge(path);
36 else
37 printf("U %s%c", path, line_termination);
38}
39
40static void show_file(int pfx, struct cache_entry *ce)
41{
42 if (generate_patch)
43 diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1,
44 ce->name, NULL);
45 else
46 printf("%c%06o\t%s\t%s\t%s%c",
47 pfx, ntohl(ce->ce_mode), "blob",
48 sha1_to_hex(ce->sha1), ce->name, line_termination);
49}
50
51static void show_modified(int oldmode, int mode,
bf0f910d 52 const unsigned char *old_sha1, const unsigned char *sha1,
4a6bf9e1
JH
53 char *path)
54{
55 char old_sha1_hex[41];
56 strcpy(old_sha1_hex, sha1_to_hex(old_sha1));
57
58 if (generate_patch)
59 diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
60 else
61 printf("*%06o->%06o\tblob\t%s->%s\t%s%c",
62 oldmode, mode, old_sha1_hex, sha1_to_hex(sha1), path,
63 line_termination);
0a7668e9
LT
64}
65
e83c5163
LT
66int main(int argc, char **argv)
67{
bf0f910d 68 static const unsigned char null_sha1[20] = { 0, };
e83c5163
LT
69 int entries = read_cache();
70 int i;
71
b8f80925 72 while (1 < argc && argv[1][0] == '-') {
d15aa430 73 if (!strcmp(argv[1], "-p"))
4a6bf9e1 74 generate_patch = 1;
b8f80925 75 else if (!strcmp(argv[1], "-q"))
d15aa430 76 silent = 1;
0a7668e9 77 else if (!strcmp(argv[1], "-r"))
4a6bf9e1 78 ; /* no-op */
d15aa430
JH
79 else if (!strcmp(argv[1], "-s"))
80 ; /* no-op */
81 else if (!strcmp(argv[1], "-z"))
82 line_termination = 0;
5c97558c
JH
83 else if (!strcmp(argv[1], "-M")) {
84 detect_rename = generate_patch = 1;
85 }
b8f80925 86 else
17710391 87 usage(diff_files_usage);
b8f80925 88 argv++; argc--;
e2e5e98a
PB
89 }
90
b8f80925
JH
91 /* At this point, if argc == 1, then we are doing everything.
92 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
93 */
e83c5163
LT
94 if (entries < 0) {
95 perror("read_cache");
96 exit(1);
97 }
be3cfa85 98
5c97558c
JH
99 if (generate_patch)
100 diff_setup(detect_rename, 0, 0, 0, 0);
101
e83c5163
LT
102 for (i = 0; i < entries; i++) {
103 struct stat st;
0a7668e9 104 unsigned int oldmode, mode;
e83c5163 105 struct cache_entry *ce = active_cache[i];
d94c6128 106 int changed;
e83c5163 107
d2522a65 108 if (1 < argc &&
b8f80925
JH
109 ! matches_pathspec(ce, argv+1, argc-1))
110 continue;
111
9fec8b26 112 if (ce_stage(ce)) {
4a6bf9e1 113 show_unmerge(ce->name);
9fec8b26
JH
114 while (i < entries &&
115 !strcmp(ce->name, active_cache[i]->name))
116 i++;
117 i--; /* compensate for loop control increments */
118 continue;
119 }
120
ffbe1add 121 if (lstat(ce->name, &st) < 0) {
0a7668e9
LT
122 if (errno != ENOENT) {
123 perror(ce->name);
ca2a0798 124 continue;
0a7668e9 125 }
d15aa430 126 if (silent)
0a7668e9 127 continue;
4a6bf9e1 128 show_file('-', ce);
e83c5163
LT
129 continue;
130 }
5d728c84 131 changed = ce_match_stat(ce, &st);
5e76011c 132 if (!changed)
e83c5163 133 continue;
e2e5e98a 134
0a7668e9 135 oldmode = ntohl(ce->ce_mode);
95649d6c
JH
136 mode = (S_ISLNK(st.st_mode) ? S_IFLNK :
137 S_IFREG | ce_permissions(st.st_mode));
0a7668e9 138
4a6bf9e1
JH
139 show_modified(oldmode, mode, ce->sha1, null_sha1,
140 ce->name);
e83c5163 141 }
5c97558c
JH
142 if (generate_patch)
143 diff_flush();
e83c5163
LT
144 return 0;
145}