@matheusbsbs / matheus-bagliano / commits / b0391890d2
Fork 0

Show modified files in git-ls-files

Add -m/--modified to show files that have been modified wrt. the index. [jc: The original came from Brian Gerst on Sep 1st but it only checked if the paths were cache dirty without actually checking the files were modified. I also added the usage string and a new test.] Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 19, 2005 at 15:11 UTC b0391890d25d9e7ca8f7df2199292df68ead8093
6 files changed +140 -78
Documentation/git-ls-files.txt
+6 -3
@@ -1,6 +1,5 @@
1 git-ls-files(1)
2 ===============
3 -v0.1, May 2005
3
4 NAME
5 ----
@@ -10,8 +9,8 @@ git-ls-files - Information about files in the cache/working directory
9 SYNOPSIS
10 --------
11 'git-ls-files' [-z] [-t]
13 - (--[cached|deleted|others|ignored|stage|unmerged|killed])\*
14 - (-[c|d|o|i|s|u|k])\*
12 + (--[cached|deleted|others|ignored|stage|unmerged|killed|modified])\*
13 + (-[c|d|o|i|s|u|k|m])\*
14 [-x <pattern>|--exclude=<pattern>]
15 [-X <file>|--exclude-from=<file>]
16 [--exclude-per-directory=<file>]
@@ -33,6 +32,9 @@ OPTIONS
32 -d|--deleted::
33 Show deleted files in the output
34
35 +-m|--modified::
36 + Show modified files in the output
37 +
38 -o|--others::
39 Show other files in the output
40
@@ -71,6 +73,7 @@ OPTIONS
73 H cached
74 M unmerged
75 R removed/deleted
76 + C modifed/changed
77 K to be killed
78 ? other
79
cache.h
+1
@@ -161,6 +161,7 @@ extern int remove_cache_entry_at(int pos);
161 extern int remove_file_from_cache(char *path);
162 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
163 extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
164 +extern int ce_modified(struct cache_entry *ce, struct stat *st);
165 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
166 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
167 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
ls-files.c
+17 -6
@@ -16,6 +16,7 @@ static int show_others = 0;
16 static int show_ignored = 0;
17 static int show_stage = 0;
18 static int show_unmerged = 0;
19 +static int show_modified = 0;
20 static int show_killed = 0;
21 static int line_terminator = '\n';
22
@@ -28,6 +29,7 @@ static const char *tag_unmerged = "";
29 static const char *tag_removed = "";
30 static const char *tag_other = "";
31 static const char *tag_killed = "";
32 +static const char *tag_modified = "";
33
34 static char *exclude_per_dir = NULL;
35
@@ -443,15 +445,18 @@ static void show_files(void)
445 show_ce_entry(ce_stage(ce) ? tag_unmerged : tag_cached, ce);
446 }
447 }
446 - if (show_deleted) {
448 + if (show_deleted | show_modified) {
449 for (i = 0; i < active_nr; i++) {
450 struct cache_entry *ce = active_cache[i];
451 struct stat st;
452 + int err;
453 if (excluded(ce->name) != show_ignored)
454 continue;
452 - if (!lstat(ce->name, &st))
453 - continue;
454 - show_ce_entry(tag_removed, ce);
455 + err = lstat(ce->name, &st);
456 + if (show_deleted && err)
457 + show_ce_entry(tag_removed, ce);
458 + if (show_modified && ce_modified(ce, &st))
459 + show_ce_entry(tag_modified, ce);
460 }
461 }
462 }
@@ -523,7 +528,7 @@ static void verify_pathspec(void)
528 }
529
530 static const char ls_files_usage[] =
526 - "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
531 + "git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
532 "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
533 "[ --exclude-per-directory=<filename> ]";
534
@@ -547,6 +552,7 @@ int main(int argc, char **argv)
552 tag_cached = "H ";
553 tag_unmerged = "M ";
554 tag_removed = "R ";
555 + tag_modified = "C ";
556 tag_other = "? ";
557 tag_killed = "K ";
558 continue;
@@ -559,6 +565,10 @@ int main(int argc, char **argv)
565 show_deleted = 1;
566 continue;
567 }
568 + if (!strcmp(arg, "-m") || !strcmp(arg, "--modified")) {
569 + show_modified = 1;
570 + continue;
571 + }
572 if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
573 show_others = 1;
574 continue;
@@ -630,7 +640,8 @@ int main(int argc, char **argv)
640 }
641
642 /* With no flags, we default to showing the cached files */
633 - if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))
643 + if (!(show_stage | show_deleted | show_others | show_unmerged |
644 + show_killed | show_modified))
645 show_cached = 1;
646
647 read_cache();
read-cache.c
+77
@@ -83,6 +83,83 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
83 return changed;
84 }
85
86 +static int ce_compare_data(struct cache_entry *ce, struct stat *st)
87 +{
88 + int match = -1;
89 + int fd = open(ce->name, O_RDONLY);
90 +
91 + if (fd >= 0) {
92 + unsigned char sha1[20];
93 + if (!index_fd(sha1, fd, st, 0, NULL))
94 + match = memcmp(sha1, ce->sha1, 20);
95 + close(fd);
96 + }
97 + return match;
98 +}
99 +
100 +static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
101 +{
102 + int match = -1;
103 + char *target;
104 + void *buffer;
105 + unsigned long size;
106 + char type[10];
107 + int len;
108 +
109 + target = xmalloc(expected_size);
110 + len = readlink(ce->name, target, expected_size);
111 + if (len != expected_size) {
112 + free(target);
113 + return -1;
114 + }
115 + buffer = read_sha1_file(ce->sha1, type, &size);
116 + if (!buffer) {
117 + free(target);
118 + return -1;
119 + }
120 + if (size == expected_size)
121 + match = memcmp(buffer, target, size);
122 + free(buffer);
123 + free(target);
124 + return match;
125 +}
126 +
127 +int ce_modified(struct cache_entry *ce, struct stat *st)
128 +{
129 + int changed;
130 + changed = ce_match_stat(ce, st);
131 + if (!changed)
132 + return 0;
133 +
134 + /*
135 + * If the mode or type has changed, there's no point in trying
136 + * to refresh the entry - it's not going to match
137 + */
138 + if (changed & (MODE_CHANGED | TYPE_CHANGED))
139 + return changed;
140 +
141 + /* Immediately after read-tree or update-index --cacheinfo,
142 + * the length field is zero. For other cases the ce_size
143 + * should match the SHA1 recorded in the index entry.
144 + */
145 + if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
146 + return changed;
147 +
148 + switch (st->st_mode & S_IFMT) {
149 + case S_IFREG:
150 + if (ce_compare_data(ce, st))
151 + return changed | DATA_CHANGED;
152 + break;
153 + case S_IFLNK:
154 + if (ce_compare_link(ce, st->st_size))
155 + return changed | DATA_CHANGED;
156 + break;
157 + default:
158 + return changed | TYPE_CHANGED;
159 + }
160 + return 0;
161 +}
162 +
163 int base_name_compare(const char *name1, int len1, int mode1,
164 const char *name2, int len2, int mode2)
165 {
t/t3010-ls-files-killed-modified.sh renamed
+38 -3
@@ -3,7 +3,7 @@
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 -test_description='git-ls-files -k flag test.
6 +test_description='git-ls-files -k and -m flags test.
7
8 This test prepares the following in the cache:
9
@@ -24,6 +24,16 @@ and the following on the filesystem:
24
25 git-ls-files -k should report that existing filesystem
26 objects except path4, path5 and path6/file6 to be killed.
27 +
28 +Also for modification test, the cache and working tree have:
29 +
30 + path7 - an empty file, modified to a non-empty file.
31 + path8 - a non-empty file, modified to an empty file.
32 + path9 - an empty file, cache dirtied.
33 + path10 - a non-empty file, cache dirtied.
34 +
35 +We should report path0, path1, path2/file2, path3/file3, path7 and path8
36 +modified without reporting path9 and path10.
37 '
38 . ./test-lib.sh
39
@@ -32,11 +42,15 @@ ln -s xyzzy path1
42 mkdir path2 path3
43 date >path2/file2
44 date >path3/file3
45 +: >path7
46 +date >path8
47 +: >path9
48 +date >path10
49 test_expect_success \
50 'git-update-index --add to add various paths.' \
37 - "git-update-index --add -- path0 path1 path?/file?"
51 + "git-update-index --add -- path0 path1 path?/file? path7 path8 path9 path10"
52
39 -rm -fr path?
53 +rm -fr path? ;# leave path10 alone
54 date >path2
55 ln -s frotz path3
56 ln -s nitfol path5
@@ -44,6 +58,10 @@ mkdir path0 path1 path6
58 date >path0/file0
59 date >path1/file1
60 date >path6/file6
61 +date >path7
62 +: >path8
63 +: >path9
64 +touch path10
65
66 test_expect_success \
67 'git-ls-files -k to show killed files.' \
@@ -58,4 +76,21 @@ EOF
76 test_expect_success \
77 'validate git-ls-files -k output.' \
78 'diff .output .expected'
79 +
80 +test_expect_success \
81 + 'git-ls-files -m to show modified files.' \
82 + 'git-ls-files -m >.output'
83 +cat >.expected <<EOF
84 +path0
85 +path1
86 +path2/file2
87 +path3/file3
88 +path7
89 +path8
90 +EOF
91 +
92 +test_expect_success \
93 + 'validate git-ls-files -m output.' \
94 + 'diff .output .expected'
95 +
96 test_done
update-index.c
+1 -66
@@ -112,47 +112,6 @@ static int add_file_to_cache(char *path)
112 return 0;
113 }
114
115 -static int compare_data(struct cache_entry *ce, struct stat *st)
116 -{
117 - int match = -1;
118 - int fd = open(ce->name, O_RDONLY);
119 -
120 - if (fd >= 0) {
121 - unsigned char sha1[20];
122 - if (!index_fd(sha1, fd, st, 0, NULL))
123 - match = memcmp(sha1, ce->sha1, 20);
124 - close(fd);
125 - }
126 - return match;
127 -}
128 -
129 -static int compare_link(struct cache_entry *ce, unsigned long expected_size)
130 -{
131 - int match = -1;
132 - char *target;
133 - void *buffer;
134 - unsigned long size;
135 - char type[10];
136 - int len;
137 -
138 - target = xmalloc(expected_size);
139 - len = readlink(ce->name, target, expected_size);
140 - if (len != expected_size) {
141 - free(target);
142 - return -1;
143 - }
144 - buffer = read_sha1_file(ce->sha1, type, &size);
145 - if (!buffer) {
146 - free(target);
147 - return -1;
148 - }
149 - if (size == expected_size)
150 - match = memcmp(buffer, target, size);
151 - free(buffer);
152 - free(target);
153 - return match;
154 -}
155 -
115 /*
116 * "refresh" does not calculate a new sha1 file or bring the
117 * cache up-to-date for mode/content changes. But what it
@@ -177,33 +136,9 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce)
136 if (!changed)
137 return ce;
138
180 - /*
181 - * If the mode or type has changed, there's no point in trying
182 - * to refresh the entry - it's not going to match
183 - */
184 - if (changed & (MODE_CHANGED | TYPE_CHANGED))
185 - return ERR_PTR(-EINVAL);
186 -
187 - /* Immediately after read-tree or update-index --cacheinfo,
188 - * the length field is zero. For other cases the ce_size
189 - * should match the SHA1 recorded in the index entry.
190 - */
191 - if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
139 + if (ce_modified(ce, &st))
140 return ERR_PTR(-EINVAL);
141
194 - switch (st.st_mode & S_IFMT) {
195 - case S_IFREG:
196 - if (compare_data(ce, &st))
197 - return ERR_PTR(-EINVAL);
198 - break;
199 - case S_IFLNK:
200 - if (compare_link(ce, st.st_size))
201 - return ERR_PTR(-EINVAL);
202 - break;
203 - default:
204 - return ERR_PTR(-EINVAL);
205 - }
206 -
142 size = ce_size(ce);
143 updated = xmalloc(size);
144 memcpy(updated, ce, size);