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 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
= xmalloc(specs
);
24 memset(seen
, 0, specs
);
26 src
= dst
= dir
->entries
;
29 struct dir_entry
*entry
= *src
++;
30 if (!match_pathspec(pathspec
, entry
->name
, entry
->len
, prefix
, seen
)) {
36 dir
->nr
= dst
- dir
->entries
;
38 for (i
= 0; i
< specs
; i
++) {
44 /* Existing file? We must have ignored it */
46 if (!match
[0] || !lstat(match
, &st
))
48 die("pathspec '%s' did not match any files", match
);
52 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
)
54 const char *path
, *base
;
57 /* Set up the default git porcelain excludes */
58 memset(dir
, 0, sizeof(*dir
));
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
);
65 * Calculate common prefix for the pathspec, and
66 * use that to optimize the directory walk
68 baselen
= common_prefix(pathspec
);
72 char *common
= xmalloc(baselen
+ 1);
73 common
= xmalloc(baselen
+ 1);
74 memcpy(common
, *pathspec
, baselen
);
79 /* Read the directory and prune it */
80 read_directory(dir
, path
, base
, baselen
);
82 prune_directory(dir
, pathspec
, baselen
);
85 static int add_file_to_index(const char *path
, int verbose
)
89 struct cache_entry
*ce
;
92 die("%s: unable to stat (%s)", path
, strerror(errno
));
94 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
))
95 die("%s: can only add regular files or symbolic links", path
);
97 namelen
= strlen(path
);
98 size
= cache_entry_size(namelen
);
99 ce
= xcalloc(1, size
);
100 memcpy(ce
->name
, path
, namelen
);
101 ce
->ce_flags
= htons(namelen
);
102 fill_stat_cache_info(ce
, &st
);
104 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
105 if (!trust_executable_bit
) {
106 /* If there is an existing entry, pick the mode bits
109 int pos
= cache_name_pos(path
, namelen
);
111 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
114 if (index_path(ce
->sha1
, path
, &st
, 1))
115 die("unable to index file %s", path
);
116 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
))
117 die("unable to add %s to index",path
);
119 printf("add '%s'\n", path
);
123 static struct cache_file cache_file
;
125 int cmd_add(int argc
, const char **argv
, char **envp
)
128 int verbose
= 0, show_only
= 0;
129 const char *prefix
= setup_git_directory();
130 const char **pathspec
;
131 struct dir_struct dir
;
133 git_config(git_default_config
);
135 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
137 die("unable to create new cachefile");
139 if (read_cache() < 0)
140 die("index file corrupt");
142 for (i
= 1; i
< argc
; i
++) {
143 const char *arg
= argv
[i
];
147 if (!strcmp(arg
, "--")) {
151 if (!strcmp(arg
, "-n")) {
155 if (!strcmp(arg
, "-v")) {
159 die(builtin_add_usage
);
161 git_config(git_default_config
);
162 pathspec
= get_pathspec(prefix
, argv
+ i
);
164 fill_directory(&dir
, pathspec
);
167 const char *sep
= "", *eof
= "";
168 for (i
= 0; i
< dir
.nr
; i
++) {
169 printf("%s%s", sep
, dir
.entries
[i
]->name
);
177 for (i
= 0; i
< dir
.nr
; i
++)
178 add_file_to_index(dir
.entries
[i
]->name
, verbose
);
180 if (active_cache_changed
) {
181 if (write_cache(newfd
, active_cache
, active_nr
) ||
182 commit_index_file(&cache_file
))
183 die("Unable to write new index file");