2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
16 static const char builtin_add_usage
[] =
17 "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
19 static int take_worktree_changes
;
20 static const char *excludes_file
;
22 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
26 struct dir_entry
**src
, **dst
;
28 for (specs
= 0; pathspec
[specs
]; specs
++)
30 seen
= xcalloc(specs
, 1);
32 src
= dst
= dir
->entries
;
35 struct dir_entry
*entry
= *src
++;
36 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
40 dir
->nr
= dst
- dir
->entries
;
42 for (i
= 0; i
< specs
; i
++) {
43 if (!seen
[i
] && !file_exists(pathspec
[i
]))
44 die("pathspec '%s' did not match any files",
50 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
53 const char *path
, *base
;
56 /* Set up the default git porcelain excludes */
57 memset(dir
, 0, sizeof(*dir
));
59 dir
->collect_ignored
= 1;
60 dir
->exclude_per_dir
= ".gitignore";
61 path
= git_path("info/exclude");
62 if (!access(path
, R_OK
))
63 add_excludes_from_file(dir
, path
);
64 if (excludes_file
!= NULL
&& !access(excludes_file
, R_OK
))
65 add_excludes_from_file(dir
, excludes_file
);
69 * Calculate common prefix for the pathspec, and
70 * use that to optimize the directory walk
72 baselen
= common_prefix(pathspec
);
76 char *common
= xmalloc(baselen
+ 1);
77 memcpy(common
, *pathspec
, baselen
);
82 /* Read the directory and prune it */
83 read_directory(dir
, path
, base
, baselen
, pathspec
);
85 prune_directory(dir
, pathspec
, baselen
);
88 static void update_callback(struct diff_queue_struct
*q
,
89 struct diff_options
*opt
, void *cbdata
)
93 verbose
= *((int *)cbdata
);
94 for (i
= 0; i
< q
->nr
; i
++) {
95 struct diff_filepair
*p
= q
->queue
[i
];
96 const char *path
= p
->one
->path
;
99 die("unexpected diff status %c", p
->status
);
100 case DIFF_STATUS_UNMERGED
:
101 case DIFF_STATUS_MODIFIED
:
102 case DIFF_STATUS_TYPE_CHANGED
:
103 add_file_to_cache(path
, verbose
);
105 case DIFF_STATUS_DELETED
:
106 remove_file_from_cache(path
);
107 cache_tree_invalidate_path(active_cache_tree
, path
);
109 printf("remove '%s'\n", path
);
115 static void update(int verbose
, const char *prefix
, const char **files
)
118 init_revisions(&rev
, prefix
);
119 setup_revisions(0, NULL
, &rev
, NULL
);
120 rev
.prune_data
= get_pathspec(prefix
, files
);
121 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
122 rev
.diffopt
.format_callback
= update_callback
;
123 rev
.diffopt
.format_callback_data
= &verbose
;
124 if (read_cache() < 0)
125 die("index file corrupt");
126 run_diff_files(&rev
, 0);
129 static void refresh(int verbose
, const char **pathspec
)
134 for (specs
= 0; pathspec
[specs
]; specs
++)
136 seen
= xcalloc(specs
, 1);
137 if (read_cache() < 0)
138 die("index file corrupt");
139 refresh_index(&the_index
, verbose
? 0 : REFRESH_QUIET
, pathspec
, seen
);
140 for (i
= 0; i
< specs
; i
++) {
142 die("pathspec '%s' did not match any files", pathspec
[i
]);
147 static int git_add_config(const char *var
, const char *value
)
149 if (!strcmp(var
, "core.excludesfile")) {
151 die("core.excludesfile without value");
152 excludes_file
= xstrdup(value
);
156 return git_default_config(var
, value
);
159 static struct lock_file lock_file
;
161 static const char ignore_error
[] =
162 "The following paths are ignored by one of your .gitignore files:\n";
164 int cmd_add(int argc
, const char **argv
, const char *prefix
)
167 int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
168 const char **pathspec
;
169 struct dir_struct dir
;
170 int add_interactive
= 0;
172 for (i
= 1; i
< argc
; i
++) {
173 if (!strcmp("--interactive", argv
[i
]) ||
174 !strcmp("-i", argv
[i
]))
177 if (add_interactive
) {
178 const char *args
[] = { "add--interactive", NULL
};
180 if (add_interactive
!= 1 || argc
!= 2)
181 die("add --interactive does not take any parameters");
186 git_config(git_add_config
);
188 newfd
= hold_locked_index(&lock_file
, 1);
190 for (i
= 1; i
< argc
; i
++) {
191 const char *arg
= argv
[i
];
195 if (!strcmp(arg
, "--")) {
199 if (!strcmp(arg
, "-n")) {
203 if (!strcmp(arg
, "-f")) {
207 if (!strcmp(arg
, "-v")) {
211 if (!strcmp(arg
, "-u")) {
212 take_worktree_changes
= 1;
215 if (!strcmp(arg
, "--refresh")) {
219 usage(builtin_add_usage
);
222 if (take_worktree_changes
) {
223 update(verbose
, prefix
, argv
+ i
);
228 fprintf(stderr
, "Nothing specified, nothing added.\n");
229 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
232 pathspec
= get_pathspec(prefix
, argv
+ i
);
235 refresh(verbose
, pathspec
);
239 fill_directory(&dir
, pathspec
, ignored_too
);
242 const char *sep
= "", *eof
= "";
243 for (i
= 0; i
< dir
.nr
; i
++) {
244 printf("%s%s", sep
, dir
.entries
[i
]->name
);
252 if (read_cache() < 0)
253 die("index file corrupt");
255 if (dir
.ignored_nr
) {
256 fprintf(stderr
, ignore_error
);
257 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
258 fprintf(stderr
, "%s\n", dir
.ignored
[i
]->name
);
260 fprintf(stderr
, "Use -f if you really want to add them.\n");
261 die("no files added");
264 for (i
= 0; i
< dir
.nr
; i
++)
265 add_file_to_cache(dir
.entries
[i
]->name
, verbose
);
268 if (active_cache_changed
) {
269 if (write_cache(newfd
, active_cache
, active_nr
) ||
270 close(newfd
) || commit_locked_index(&lock_file
))
271 die("Unable to write new index file");