2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage
[] = {
19 "git-add [options] [--] <filepattern>...",
22 static int patch_interactive
= 0, add_interactive
= 0;
23 static int take_worktree_changes
;
25 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
29 struct dir_entry
**src
, **dst
;
31 for (specs
= 0; pathspec
[specs
]; specs
++)
33 seen
= xcalloc(specs
, 1);
35 src
= dst
= dir
->entries
;
38 struct dir_entry
*entry
= *src
++;
39 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
43 dir
->nr
= dst
- dir
->entries
;
45 for (i
= 0; i
< specs
; i
++) {
46 if (!seen
[i
] && !file_exists(pathspec
[i
]))
47 die("pathspec '%s' did not match any files",
53 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
56 const char *path
, *base
;
59 /* Set up the default git porcelain excludes */
60 memset(dir
, 0, sizeof(*dir
));
62 dir
->collect_ignored
= 1;
63 setup_standard_excludes(dir
);
67 * Calculate common prefix for the pathspec, and
68 * use that to optimize the directory walk
70 baselen
= common_prefix(pathspec
);
74 path
= base
= xmemdupz(*pathspec
, baselen
);
76 /* Read the directory and prune it */
77 read_directory(dir
, path
, base
, baselen
, pathspec
);
79 prune_directory(dir
, pathspec
, baselen
);
82 static void update_callback(struct diff_queue_struct
*q
,
83 struct diff_options
*opt
, void *cbdata
)
87 verbose
= *((int *)cbdata
);
88 for (i
= 0; i
< q
->nr
; i
++) {
89 struct diff_filepair
*p
= q
->queue
[i
];
90 const char *path
= p
->one
->path
;
93 die("unexpected diff status %c", p
->status
);
94 case DIFF_STATUS_UNMERGED
:
95 case DIFF_STATUS_MODIFIED
:
96 case DIFF_STATUS_TYPE_CHANGED
:
97 if (add_file_to_cache(path
, verbose
))
98 die("updating files failed");
100 case DIFF_STATUS_DELETED
:
101 remove_file_from_cache(path
);
103 printf("remove '%s'\n", path
);
109 void add_files_to_cache(int verbose
, const char *prefix
, const char **pathspec
)
112 init_revisions(&rev
, prefix
);
113 setup_revisions(0, NULL
, &rev
, NULL
);
114 rev
.prune_data
= pathspec
;
115 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
116 rev
.diffopt
.format_callback
= update_callback
;
117 rev
.diffopt
.format_callback_data
= &verbose
;
118 run_diff_files(&rev
, DIFF_RACY_IS_MODIFIED
);
121 static void refresh(int verbose
, const char **pathspec
)
126 for (specs
= 0; pathspec
[specs
]; specs
++)
128 seen
= xcalloc(specs
, 1);
129 if (read_cache() < 0)
130 die("index file corrupt");
131 refresh_index(&the_index
, verbose
? 0 : REFRESH_QUIET
, pathspec
, seen
);
132 for (i
= 0; i
< specs
; i
++) {
134 die("pathspec '%s' did not match any files", pathspec
[i
]);
139 static const char **validate_pathspec(int argc
, const char **argv
, const char *prefix
)
141 const char **pathspec
= get_pathspec(prefix
, argv
);
146 int interactive_add(int argc
, const char **argv
, const char *prefix
)
150 const char **pathspec
= NULL
;
153 pathspec
= validate_pathspec(argc
, argv
, prefix
);
158 args
= xcalloc(sizeof(const char *), (argc
+ 4));
160 args
[ac
++] = "add--interactive";
161 if (patch_interactive
)
162 args
[ac
++] = "--patch";
165 memcpy(&(args
[ac
]), pathspec
, sizeof(const char *) * argc
);
170 status
= run_command_v_opt(args
, RUN_GIT_CMD
);
175 static struct lock_file lock_file
;
177 static const char ignore_error
[] =
178 "The following paths are ignored by one of your .gitignore files:\n";
180 static int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
182 static struct option builtin_add_options
[] = {
183 OPT__DRY_RUN(&show_only
),
184 OPT__VERBOSE(&verbose
),
186 OPT_BOOLEAN('i', "interactive", &add_interactive
, "interactive picking"),
187 OPT_BOOLEAN('p', "patch", &patch_interactive
, "interactive patching"),
188 OPT_BOOLEAN('f', NULL
, &ignored_too
, "allow adding otherwise ignored files"),
189 OPT_BOOLEAN('u', NULL
, &take_worktree_changes
, "update tracked files"),
190 OPT_BOOLEAN( 0 , "refresh", &refresh_only
, "don't add, only refresh the index"),
194 int cmd_add(int argc
, const char **argv
, const char *prefix
)
197 const char **pathspec
;
198 struct dir_struct dir
;
200 argc
= parse_options(argc
, argv
, builtin_add_options
,
201 builtin_add_usage
, 0);
202 if (patch_interactive
)
205 exit(interactive_add(argc
, argv
, prefix
));
207 git_config(git_default_config
);
209 newfd
= hold_locked_index(&lock_file
, 1);
211 if (take_worktree_changes
) {
212 const char **pathspec
;
213 if (read_cache() < 0)
214 die("index file corrupt");
215 pathspec
= get_pathspec(prefix
, argv
);
216 add_files_to_cache(verbose
, prefix
, pathspec
);
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
);
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 if (add_file_to_cache(dir
.entries
[i
]->name
, verbose
))
259 die("adding files failed");
262 if (active_cache_changed
) {
263 if (write_cache(newfd
, active_cache
, active_nr
) ||
264 commit_locked_index(&lock_file
))
265 die("Unable to write new index file");