git-add -u: match the index with working tree.
This is a shorthand of what "git commit -a" does in preparation for making a commit, which is: git diff-files --name-only -z | git update-index --remove -z --stdin Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Apr 20, 2007 at 01:39 UTC
dfdac5d9b877641d3aad8ec49f64c2730a3487e3
1 file changed
+57
-1
builtin-add.c
+57
-1
@@ -8,10 +8,15 @@
8
#include "dir.h"
9
#include "exec_cmd.h"
10
#include "cache-tree.h"
11
+#include "diff.h"
12
+#include "diffcore.h"
13
+#include "commit.h"
14
+#include "revision.h"
15
16
static const char builtin_add_usage[] =
13
-"git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
17
+"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--] <filepattern>...";
18
19
+static int take_all_worktree_changes;
20
static const char *excludes_file;
21
22
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
@@ -92,6 +97,44 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec)
97
prune_directory(dir, pathspec, baselen);
98
}
99
100
+static void update_callback(struct diff_queue_struct *q,
101
+ struct diff_options *opt, void *cbdata)
102
+{
103
+ int i, verbose;
104
+
105
+ verbose = *((int *)cbdata);
106
+ for (i = 0; i < q->nr; i++) {
107
+ struct diff_filepair *p = q->queue[i];
108
+ const char *path = p->one->path;
109
+ switch (p->status) {
110
+ default:
111
+ die("unexpacted diff status %c", p->status);
112
+ case DIFF_STATUS_UNMERGED:
113
+ case DIFF_STATUS_MODIFIED:
114
+ add_file_to_cache(path, verbose);
115
+ break;
116
+ case DIFF_STATUS_DELETED:
117
+ remove_file_from_cache(path);
118
+ if (verbose)
119
+ printf("remove '%s'\n", path);
120
+ break;
121
+ }
122
+ }
123
+}
124
+
125
+static void update_all(int verbose)
126
+{
127
+ struct rev_info rev;
128
+ init_revisions(&rev, "");
129
+ setup_revisions(0, NULL, &rev, NULL);
130
+ rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
131
+ rev.diffopt.format_callback = update_callback;
132
+ rev.diffopt.format_callback_data = &verbose;
133
+ if (read_cache() < 0)
134
+ die("index file corrupt");
135
+ run_diff_files(&rev, 0);
136
+}
137
+
138
static int git_add_config(const char *var, const char *value)
139
{
140
if (!strcmp(var, "core.excludesfile")) {
@@ -156,8 +199,20 @@ int cmd_add(int argc, const char **argv, const char *prefix)
199
verbose = 1;
200
continue;
201
}
202
+ if (!strcmp(arg, "-u")) {
203
+ take_all_worktree_changes = 1;
204
+ continue;
205
+ }
206
usage(builtin_add_usage);
207
}
208
+
209
+ if (take_all_worktree_changes) {
210
+ if (i < argc)
211
+ die("-u and explicit paths are incompatible");
212
+ update_all(verbose);
213
+ goto finish;
214
+ }
215
+
216
if (argc <= i) {
217
fprintf(stderr, "Nothing specified, nothing added.\n");
218
fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
@@ -207,6 +262,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
262
for (i = 0; i < dir.nr; i++)
263
add_file_to_cache(dir.entries[i]->name, verbose);
264
265
+ finish:
266
if (active_cache_changed) {
267
if (write_cache(newfd, active_cache, active_nr) ||
268
close(newfd) || commit_locked_index(&lock_file))