2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
12 static const char builtin_add_usage
[] =
13 "git-add [-n] [-v] <filepattern>...";
15 static int common_prefix(const char **pathspec
)
17 const char *path
, *slash
, *next
;
24 slash
= strrchr(path
, '/');
28 prefix
= slash
- path
+ 1;
29 while ((next
= *++pathspec
) != NULL
) {
30 int len
= strlen(next
);
31 if (len
>= prefix
&& !memcmp(path
, next
, len
))
36 if (next
[--len
] != '/')
38 if (memcmp(path
, next
, len
+1))
47 static int match_one(const char *match
, const char *name
, int namelen
)
51 /* If the match was just the prefix, we matched */
52 matchlen
= strlen(match
);
57 * If we don't match the matchstring exactly,
58 * we need to match by fnmatch
60 if (strncmp(match
, name
, matchlen
))
61 return !fnmatch(match
, name
, 0);
64 * If we did match the string exactly, we still
65 * need to make sure that it happened on a path
66 * component boundary (ie either the last character
67 * of the match was '/', or the next character of
68 * the name was '/' or the terminating NUL.
70 return match
[matchlen
-1] == '/' ||
71 name
[matchlen
] == '/' ||
75 static int match(const char **pathspec
, const char *name
, int namelen
, int prefix
, char *seen
)
83 for (retval
= 0; (match
= *pathspec
++) != NULL
; seen
++) {
87 if (match_one(match
, name
, namelen
)) {
95 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
99 struct dir_entry
**src
, **dst
;
101 for (specs
= 0; pathspec
[specs
]; specs
++)
103 seen
= xmalloc(specs
);
104 memset(seen
, 0, specs
);
106 src
= dst
= dir
->entries
;
109 struct dir_entry
*entry
= *src
++;
110 if (!match(pathspec
, entry
->name
, entry
->len
, prefix
, seen
)) {
116 dir
->nr
= dst
- dir
->entries
;
118 for (i
= 0; i
< specs
; i
++) {
124 /* Existing file? We must have ignored it */
126 if (!lstat(match
, &st
))
128 die("pathspec '%s' did not match any files", match
);
132 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
)
134 const char *path
, *base
;
137 /* Set up the default git porcelain excludes */
138 memset(dir
, 0, sizeof(*dir
));
139 dir
->exclude_per_dir
= ".gitignore";
140 path
= git_path("info/exclude");
141 if (!access(path
, R_OK
))
142 add_excludes_from_file(dir
, path
);
145 * Calculate common prefix for the pathspec, and
146 * use that to optimize the directory walk
148 baselen
= common_prefix(pathspec
);
152 char *common
= xmalloc(baselen
+ 1);
153 common
= xmalloc(baselen
+ 1);
154 memcpy(common
, *pathspec
, baselen
);
156 path
= base
= common
;
159 /* Read the directory and prune it */
160 read_directory(dir
, path
, base
, baselen
);
162 prune_directory(dir
, pathspec
, baselen
);
165 static int add_file_to_index(const char *path
, int verbose
)
169 struct cache_entry
*ce
;
171 if (lstat(path
, &st
))
172 die("%s: unable to stat (%s)", path
, strerror(errno
));
174 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
))
175 die("%s: can only add regular files or symbolic links", path
);
177 namelen
= strlen(path
);
178 size
= cache_entry_size(namelen
);
179 ce
= xcalloc(1, size
);
180 memcpy(ce
->name
, path
, namelen
);
181 ce
->ce_flags
= htons(namelen
);
182 fill_stat_cache_info(ce
, &st
);
184 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
185 if (!trust_executable_bit
) {
186 /* If there is an existing entry, pick the mode bits
189 int pos
= cache_name_pos(path
, namelen
);
191 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
194 if (index_path(ce
->sha1
, path
, &st
, 1))
195 die("unable to index file %s", path
);
196 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
))
197 die("unable to add %s to index",path
);
199 printf("add '%s'\n", path
);
203 static struct cache_file cache_file
;
205 int cmd_add(int argc
, const char **argv
, char **envp
)
208 int verbose
= 0, show_only
= 0;
209 const char *prefix
= setup_git_directory();
210 const char **pathspec
;
211 struct dir_struct dir
;
213 git_config(git_default_config
);
215 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
217 die("unable to create new cachefile");
219 if (read_cache() < 0)
220 die("index file corrupt");
222 for (i
= 1; i
< argc
; i
++) {
223 const char *arg
= argv
[i
];
227 if (!strcmp(arg
, "--")) {
231 if (!strcmp(arg
, "-n")) {
235 if (!strcmp(arg
, "-v")) {
239 die(builtin_add_usage
);
241 git_config(git_default_config
);
242 pathspec
= get_pathspec(prefix
, argv
+ i
);
244 fill_directory(&dir
, pathspec
);
247 const char *sep
= "", *eof
= "";
248 for (i
= 0; i
< dir
.nr
; i
++) {
249 printf("%s%s", sep
, dir
.entries
[i
]->name
);
257 for (i
= 0; i
< dir
.nr
; i
++)
258 add_file_to_index(dir
.entries
[i
]->name
, verbose
);
260 if (active_cache_changed
) {
261 if (write_cache(newfd
, active_cache
, active_nr
) ||
262 commit_index_file(&cache_file
))
263 die("Unable to write new index file");