git-add: Add support for --refresh option.
This allows to refresh only a subset of the project files, based on the specified pathspecs. Signed-off-by: Alexandre Julliard <julliard@winehq.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alexandre Julliard committed
Aug 11, 2007 at 23:59 UTC
d616813d75b888b7c29bbad19808fe5cffa5380c
5 files changed
+51
-5
Documentation/git-add.txt
+5
-1
@@ -7,7 +7,7 @@ git-add - Add file contents to the index
7
8
SYNOPSIS
9
--------
10
-'git-add' [-n] [-v] [-f] [--interactive | -i] [-u] [--] <file>...
10
+'git-add' [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <file>...
11
12
DESCRIPTION
13
-----------
@@ -66,6 +66,10 @@ OPTIONS
66
command line. If no paths are specified, all tracked files are
67
updated.
68
69
+\--refresh::
70
+ Don't add the file(s), but only refresh their stat()
71
+ information in the index.
72
+
73
\--::
74
This option can be used to separate command-line options from
75
the list of files, (useful when filenames might be mistaken
builtin-add.c
+27
-1
@@ -123,6 +123,23 @@ static void update(int verbose, const char **files)
123
run_diff_files(&rev, 0);
124
}
125
126
+static void refresh(int verbose, const char **pathspec)
127
+{
128
+ char *seen;
129
+ int i, specs;
130
+
131
+ for (specs = 0; pathspec[specs]; specs++)
132
+ /* nothing */;
133
+ seen = xcalloc(specs, 1);
134
+ if (read_cache() < 0)
135
+ die("index file corrupt");
136
+ refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen);
137
+ for (i = 0; i < specs; i++) {
138
+ if (!seen[i])
139
+ die("pathspec '%s' did not match any files", pathspec[i]);
140
+ }
141
+}
142
+
143
static int git_add_config(const char *var, const char *value)
144
{
145
if (!strcmp(var, "core.excludesfile")) {
@@ -143,7 +160,7 @@ static const char ignore_warning[] =
160
int cmd_add(int argc, const char **argv, const char *prefix)
161
{
162
int i, newfd;
146
- int verbose = 0, show_only = 0, ignored_too = 0;
163
+ int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
164
const char **pathspec;
165
struct dir_struct dir;
166
int add_interactive = 0;
@@ -191,6 +208,10 @@ int cmd_add(int argc, const char **argv, const char *prefix)
208
take_worktree_changes = 1;
209
continue;
210
}
211
+ if (!strcmp(arg, "--refresh")) {
212
+ refresh_only = 1;
213
+ continue;
214
+ }
215
usage(builtin_add_usage);
216
}
217
@@ -206,6 +227,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)
227
}
228
pathspec = get_pathspec(prefix, argv + i);
229
230
+ if (refresh_only) {
231
+ refresh(verbose, pathspec);
232
+ goto finish;
233
+ }
234
+
235
fill_directory(&dir, pathspec, ignored_too);
236
237
if (show_only) {
cache.h
+2
-2
@@ -173,7 +173,7 @@ extern struct index_state the_index;
173
#define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
174
#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
175
#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose))
176
-#define refresh_cache(flags) refresh_index(&the_index, flags)
176
+#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL)
177
#define ce_match_stat(ce, st, really) ie_match_stat(&the_index, (ce), (st), (really))
178
#define ce_modified(ce, st, really) ie_modified(&the_index, (ce), (st), (really))
179
#endif
@@ -278,7 +278,7 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
278
#define REFRESH_UNMERGED 0x0002 /* allow unmerged */
279
#define REFRESH_QUIET 0x0004 /* be quiet about it */
280
#define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */
281
-extern int refresh_index(struct index_state *, unsigned int flags);
281
+extern int refresh_index(struct index_state *, unsigned int flags, const char **pathspec, char *seen);
282
283
struct lock_file {
284
struct lock_file *next;
read-cache.c
+5
-1
@@ -7,6 +7,7 @@
7
#include "cache.h"
8
#include "cache-tree.h"
9
#include "refs.h"
10
+#include "dir.h"
11
12
/* Index extensions.
13
*
@@ -798,7 +799,7 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,
799
return updated;
800
}
801
801
-int refresh_index(struct index_state *istate, unsigned int flags)
802
+int refresh_index(struct index_state *istate, unsigned int flags, const char **pathspec, char *seen)
803
{
804
int i;
805
int has_errors = 0;
@@ -824,6 +825,9 @@ int refresh_index(struct index_state *istate, unsigned int flags)
825
continue;
826
}
827
828
+ if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
829
+ continue;
830
+
831
new = refresh_cache_ent(istate, ce, really, &cache_errno);
832
if (new == ce)
833
continue;
t/t3700-add.sh
+12
@@ -143,4 +143,16 @@ test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over st
143
git ls-files --stage | grep "^120000 .* 0 symlink$"
144
'
145
146
+test_expect_success 'git add --refresh' '
147
+ >foo && git add foo && git commit -a -m "commit all" &&
148
+ test -z "`git diff-index HEAD -- foo`" &&
149
+ git read-tree HEAD &&
150
+ case "`git diff-index HEAD -- foo`" in
151
+ :100644" "*"M foo") echo ok;;
152
+ *) echo fail; (exit 1);;
153
+ esac &&
154
+ git add --refresh -- foo &&
155
+ test -z "`git diff-index HEAD -- foo`"
156
+'
157
+
158
test_done