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",
49 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
52 const char *path
, *base
;
55 /* Set up the default git porcelain excludes */
56 memset(dir
, 0, sizeof(*dir
));
58 dir
->collect_ignored
= 1;
59 dir
->exclude_per_dir
= ".gitignore";
60 path
= git_path("info/exclude");
61 if (!access(path
, R_OK
))
62 add_excludes_from_file(dir
, path
);
63 if (excludes_file
!= NULL
&& !access(excludes_file
, R_OK
))
64 add_excludes_from_file(dir
, excludes_file
);
68 * Calculate common prefix for the pathspec, and
69 * use that to optimize the directory walk
71 baselen
= common_prefix(pathspec
);
75 path
= base
= xmemdupz(*pathspec
, baselen
);
77 /* Read the directory and prune it */
78 read_directory(dir
, path
, base
, baselen
, pathspec
);
80 prune_directory(dir
, pathspec
, baselen
);
83 static void update_callback(struct diff_queue_struct
*q
,
84 struct diff_options
*opt
, void *cbdata
)
88 verbose
= *((int *)cbdata
);
89 for (i
= 0; i
< q
->nr
; i
++) {
90 struct diff_filepair
*p
= q
->queue
[i
];
91 const char *path
= p
->one
->path
;
94 die("unexpected diff status %c", p
->status
);
95 case DIFF_STATUS_UNMERGED
:
96 case DIFF_STATUS_MODIFIED
:
97 case DIFF_STATUS_TYPE_CHANGED
:
98 add_file_to_cache(path
, verbose
);
100 case DIFF_STATUS_DELETED
:
101 remove_file_from_cache(path
);
103 printf("remove '%s'\n", path
);
109 static void update(int verbose
, const char *prefix
, const char **files
)
112 init_revisions(&rev
, prefix
);
113 setup_revisions(0, NULL
, &rev
, NULL
);
114 rev
.prune_data
= get_pathspec(prefix
, files
);
115 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
116 rev
.diffopt
.format_callback
= update_callback
;
117 rev
.diffopt
.format_callback_data
= &verbose
;
118 if (read_cache() < 0)
119 die("index file corrupt");
120 run_diff_files(&rev
, 0);
123 static void refresh(int verbose
, const char **pathspec
)
128 for (specs
= 0; pathspec
[specs
]; specs
++)
130 seen
= xcalloc(specs
, 1);
131 if (read_cache() < 0)
132 die("index file corrupt");
133 refresh_index(&the_index
, verbose
? 0 : REFRESH_QUIET
, pathspec
, seen
);
134 for (i
= 0; i
< specs
; i
++) {
136 die("pathspec '%s' did not match any files", pathspec
[i
]);
140 static int git_add_config(const char *var
, const char *value
)
142 if (!strcmp(var
, "core.excludesfile")) {
144 die("core.excludesfile without value");
145 excludes_file
= xstrdup(value
);
149 return git_default_config(var
, value
);
152 static struct lock_file lock_file
;
154 static const char ignore_error
[] =
155 "The following paths are ignored by one of your .gitignore files:\n";
157 int cmd_add(int argc
, const char **argv
, const char *prefix
)
160 int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
161 const char **pathspec
;
162 struct dir_struct dir
;
163 int add_interactive
= 0;
165 for (i
= 1; i
< argc
; i
++) {
166 if (!strcmp("--interactive", argv
[i
]) ||
167 !strcmp("-i", argv
[i
]))
170 if (add_interactive
) {
171 const char *args
[] = { "add--interactive", NULL
};
173 if (add_interactive
!= 1 || argc
!= 2)
174 die("add --interactive does not take any parameters");
179 git_config(git_add_config
);
181 newfd
= hold_locked_index(&lock_file
, 1);
183 for (i
= 1; i
< argc
; i
++) {
184 const char *arg
= argv
[i
];
188 if (!strcmp(arg
, "--")) {
192 if (!strcmp(arg
, "-n")) {
196 if (!strcmp(arg
, "-f")) {
200 if (!strcmp(arg
, "-v")) {
204 if (!strcmp(arg
, "-u")) {
205 take_worktree_changes
= 1;
208 if (!strcmp(arg
, "--refresh")) {
212 usage(builtin_add_usage
);
215 if (take_worktree_changes
) {
216 update(verbose
, prefix
, argv
+ i
);
221 fprintf(stderr
, "Nothing specified, nothing added.\n");
222 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
225 pathspec
= get_pathspec(prefix
, argv
+ i
);
228 refresh(verbose
, pathspec
);
232 fill_directory(&dir
, pathspec
, ignored_too
);
235 const char *sep
= "", *eof
= "";
236 for (i
= 0; i
< dir
.nr
; i
++) {
237 printf("%s%s", sep
, dir
.entries
[i
]->name
);
245 if (read_cache() < 0)
246 die("index file corrupt");
248 if (dir
.ignored_nr
) {
249 fprintf(stderr
, ignore_error
);
250 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
251 fprintf(stderr
, "%s\n", dir
.ignored
[i
]->name
);
253 fprintf(stderr
, "Use -f if you really want to add them.\n");
254 die("no files added");
257 for (i
= 0; i
< dir
.nr
; i
++)
258 add_file_to_cache(dir
.entries
[i
]->name
, verbose
);
261 if (active_cache_changed
) {
262 if (write_cache(newfd
, active_cache
, active_nr
) ||
263 close(newfd
) || commit_locked_index(&lock_file
))
264 die("Unable to write new index file");