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] [--] <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
++) {
52 /* Existing file? We must have ignored it */
53 if (!lstat(match
, &st
)) {
54 struct dir_entry
*ent
;
56 ent
= dir_add_name(dir
, match
, strlen(match
));
58 if (S_ISDIR(st
.st_mode
))
62 die("pathspec '%s' did not match any files", match
);
66 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
)
68 const char *path
, *base
;
71 /* Set up the default git porcelain excludes */
72 memset(dir
, 0, sizeof(*dir
));
73 dir
->exclude_per_dir
= ".gitignore";
74 path
= git_path("info/exclude");
75 if (!access(path
, R_OK
))
76 add_excludes_from_file(dir
, path
);
77 if (!access(excludes_file
, R_OK
))
78 add_excludes_from_file(dir
, excludes_file
);
81 * Calculate common prefix for the pathspec, and
82 * use that to optimize the directory walk
84 baselen
= common_prefix(pathspec
);
88 char *common
= xmalloc(baselen
+ 1);
89 memcpy(common
, *pathspec
, baselen
);
94 /* Read the directory and prune it */
95 read_directory(dir
, path
, base
, baselen
, pathspec
);
97 prune_directory(dir
, pathspec
, baselen
);
100 static void update_callback(struct diff_queue_struct
*q
,
101 struct diff_options
*opt
, void *cbdata
)
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
;
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
);
116 case DIFF_STATUS_DELETED
:
117 remove_file_from_cache(path
);
119 printf("remove '%s'\n", path
);
125 static void update(int verbose
, const char **files
)
128 init_revisions(&rev
, "");
129 setup_revisions(0, NULL
, &rev
, NULL
);
130 rev
.prune_data
= get_pathspec(rev
.prefix
, files
);
131 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
132 rev
.diffopt
.format_callback
= update_callback
;
133 rev
.diffopt
.format_callback_data
= &verbose
;
134 if (read_cache() < 0)
135 die("index file corrupt");
136 run_diff_files(&rev
, 0);
139 static int git_add_config(const char *var
, const char *value
)
141 if (!strcmp(var
, "core.excludesfile")) {
143 die("core.excludesfile without value");
144 excludes_file
= xstrdup(value
);
148 return git_default_config(var
, value
);
151 static struct lock_file lock_file
;
153 static const char ignore_warning
[] =
154 "The following paths are ignored by one of your .gitignore files:\n";
156 int cmd_add(int argc
, const char **argv
, const char *prefix
)
159 int verbose
= 0, show_only
= 0, ignored_too
= 0;
160 const char **pathspec
;
161 struct dir_struct dir
;
162 int add_interactive
= 0;
164 for (i
= 1; i
< argc
; i
++) {
165 if (!strcmp("--interactive", argv
[i
]) ||
166 !strcmp("-i", argv
[i
]))
169 if (add_interactive
) {
170 const char *args
[] = { "add--interactive", NULL
};
172 if (add_interactive
!= 1 || argc
!= 2)
173 die("add --interactive does not take any parameters");
178 git_config(git_add_config
);
180 newfd
= hold_locked_index(&lock_file
, 1);
182 for (i
= 1; i
< argc
; i
++) {
183 const char *arg
= argv
[i
];
187 if (!strcmp(arg
, "--")) {
191 if (!strcmp(arg
, "-n")) {
195 if (!strcmp(arg
, "-f")) {
199 if (!strcmp(arg
, "-v")) {
203 if (!strcmp(arg
, "-u")) {
204 take_worktree_changes
= 1;
207 usage(builtin_add_usage
);
210 if (take_worktree_changes
) {
211 update(verbose
, argv
+ i
);
216 fprintf(stderr
, "Nothing specified, nothing added.\n");
217 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
220 pathspec
= get_pathspec(prefix
, argv
+ i
);
222 fill_directory(&dir
, pathspec
);
225 const char *sep
= "", *eof
= "";
226 for (i
= 0; i
< dir
.nr
; i
++) {
227 if (!ignored_too
&& dir
.entries
[i
]->ignored
)
229 printf("%s%s", sep
, dir
.entries
[i
]->name
);
237 if (read_cache() < 0)
238 die("index file corrupt");
242 for (i
= 0; i
< dir
.nr
; i
++)
243 if (dir
.entries
[i
]->ignored
)
246 fprintf(stderr
, ignore_warning
);
247 for (i
= 0; i
< dir
.nr
; i
++) {
248 if (!dir
.entries
[i
]->ignored
)
250 fprintf(stderr
, "%s", dir
.entries
[i
]->name
);
251 if (dir
.entries
[i
]->ignored_dir
)
252 fprintf(stderr
, " (directory)");
256 "Use -f if you really want to add them.\n");
261 for (i
= 0; i
< dir
.nr
; i
++)
262 add_file_to_cache(dir
.entries
[i
]->name
, verbose
);
265 if (active_cache_changed
) {
266 if (write_cache(newfd
, active_cache
, active_nr
) ||
267 close(newfd
) || commit_locked_index(&lock_file
))
268 die("Unable to write new index file");