2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
12 static const char builtin_add_usage
[] =
13 "git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
15 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
19 struct dir_entry
**src
, **dst
;
21 for (specs
= 0; pathspec
[specs
]; specs
++)
23 seen
= xcalloc(specs
, 1);
25 src
= dst
= dir
->entries
;
28 struct dir_entry
*entry
= *src
++;
29 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
33 dir
->nr
= dst
- dir
->entries
;
35 for (i
= 0; i
< specs
; i
++) {
45 /* Existing file? We must have ignored it */
46 if (!lstat(match
, &st
)) {
47 struct dir_entry
*ent
;
49 ent
= dir_add_name(dir
, match
, strlen(match
));
51 if (S_ISDIR(st
.st_mode
))
55 die("pathspec '%s' did not match any files", match
);
59 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
)
61 const char *path
, *base
;
64 /* Set up the default git porcelain excludes */
65 memset(dir
, 0, sizeof(*dir
));
66 dir
->exclude_per_dir
= ".gitignore";
67 path
= git_path("info/exclude");
68 if (!access(path
, R_OK
))
69 add_excludes_from_file(dir
, path
);
72 * Calculate common prefix for the pathspec, and
73 * use that to optimize the directory walk
75 baselen
= common_prefix(pathspec
);
79 char *common
= xmalloc(baselen
+ 1);
80 memcpy(common
, *pathspec
, baselen
);
85 /* Read the directory and prune it */
86 read_directory(dir
, path
, base
, baselen
);
88 prune_directory(dir
, pathspec
, baselen
);
91 static struct lock_file lock_file
;
93 static const char ignore_warning
[] =
94 "The following paths are ignored by one of your .gitignore files:\n";
96 int cmd_add(int argc
, const char **argv
, const char *prefix
)
99 int verbose
= 0, show_only
= 0, ignored_too
= 0;
100 const char **pathspec
;
101 struct dir_struct dir
;
102 int add_interactive
= 0;
104 for (i
= 1; i
< argc
; i
++) {
105 if (!strcmp("--interactive", argv
[i
]) ||
106 !strcmp("-i", argv
[i
]))
109 if (add_interactive
) {
110 const char *args
[] = { "add--interactive", NULL
};
112 if (add_interactive
!= 1 || argc
!= 2)
113 die("add --interactive does not take any parameters");
118 git_config(git_default_config
);
120 newfd
= hold_lock_file_for_update(&lock_file
, get_index_file(), 1);
122 for (i
= 1; i
< argc
; i
++) {
123 const char *arg
= argv
[i
];
127 if (!strcmp(arg
, "--")) {
131 if (!strcmp(arg
, "-n")) {
135 if (!strcmp(arg
, "-f")) {
139 if (!strcmp(arg
, "-v")) {
143 usage(builtin_add_usage
);
146 fprintf(stderr
, "Nothing specified, nothing added.\n");
147 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
150 pathspec
= get_pathspec(prefix
, argv
+ i
);
152 fill_directory(&dir
, pathspec
);
155 const char *sep
= "", *eof
= "";
156 for (i
= 0; i
< dir
.nr
; i
++) {
157 if (!ignored_too
&& dir
.entries
[i
]->ignored
)
159 printf("%s%s", sep
, dir
.entries
[i
]->name
);
167 if (read_cache() < 0)
168 die("index file corrupt");
172 for (i
= 0; i
< dir
.nr
; i
++)
173 if (dir
.entries
[i
]->ignored
)
176 fprintf(stderr
, ignore_warning
);
177 for (i
= 0; i
< dir
.nr
; i
++) {
178 if (!dir
.entries
[i
]->ignored
)
180 fprintf(stderr
, "%s", dir
.entries
[i
]->name
);
181 if (dir
.entries
[i
]->ignored_dir
)
182 fprintf(stderr
, " (directory)");
186 "Use -f if you really want to add them.\n");
191 for (i
= 0; i
< dir
.nr
; i
++)
192 add_file_to_index(dir
.entries
[i
]->name
, verbose
);
194 if (active_cache_changed
) {
195 if (write_cache(newfd
, active_cache
, active_nr
) ||
196 close(newfd
) || commit_lock_file(&lock_file
))
197 die("Unable to write new index file");