gitweb: optimize git_shortlog_body.
[git/jnareb-git.git] / builtin-add.c
blobf306f82b16c3c3c76416610cd4cabf0a307924ed
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "cache-tree.h"
11 static const char builtin_add_usage[] =
12 "git-add [-n] [-v] <filepattern>...";
14 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
16 char *seen;
17 int i, specs;
18 struct dir_entry **src, **dst;
20 for (specs = 0; pathspec[specs]; specs++)
21 /* nothing */;
22 seen = xcalloc(specs, 1);
24 src = dst = dir->entries;
25 i = dir->nr;
26 while (--i >= 0) {
27 struct dir_entry *entry = *src++;
28 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
29 free(entry);
30 continue;
32 *dst++ = entry;
34 dir->nr = dst - dir->entries;
36 for (i = 0; i < specs; i++) {
37 struct stat st;
38 const char *match;
39 if (seen[i])
40 continue;
42 /* Existing file? We must have ignored it */
43 match = pathspec[i];
44 if (!match[0] || !lstat(match, &st))
45 continue;
46 die("pathspec '%s' did not match any files", match);
50 static void fill_directory(struct dir_struct *dir, const char **pathspec)
52 const char *path, *base;
53 int baselen;
55 /* Set up the default git porcelain excludes */
56 memset(dir, 0, sizeof(*dir));
57 dir->exclude_per_dir = ".gitignore";
58 path = git_path("info/exclude");
59 if (!access(path, R_OK))
60 add_excludes_from_file(dir, path);
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
66 baselen = common_prefix(pathspec);
67 path = ".";
68 base = "";
69 if (baselen) {
70 char *common = xmalloc(baselen + 1);
71 memcpy(common, *pathspec, baselen);
72 common[baselen] = 0;
73 path = base = common;
76 /* Read the directory and prune it */
77 read_directory(dir, path, base, baselen);
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
82 static struct lock_file lock_file;
84 int cmd_add(int argc, const char **argv, const char *prefix)
86 int i, newfd;
87 int verbose = 0, show_only = 0;
88 const char **pathspec;
89 struct dir_struct dir;
91 git_config(git_default_config);
93 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
95 for (i = 1; i < argc; i++) {
96 const char *arg = argv[i];
98 if (arg[0] != '-')
99 break;
100 if (!strcmp(arg, "--")) {
101 i++;
102 break;
104 if (!strcmp(arg, "-n")) {
105 show_only = 1;
106 continue;
108 if (!strcmp(arg, "-v")) {
109 verbose = 1;
110 continue;
112 usage(builtin_add_usage);
114 if (argc <= i) {
115 fprintf(stderr, "Nothing specified, nothing added.\n");
116 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
117 return 0;
119 pathspec = get_pathspec(prefix, argv + i);
121 fill_directory(&dir, pathspec);
123 if (show_only) {
124 const char *sep = "", *eof = "";
125 for (i = 0; i < dir.nr; i++) {
126 printf("%s%s", sep, dir.entries[i]->name);
127 sep = " ";
128 eof = "\n";
130 fputs(eof, stdout);
131 return 0;
134 if (read_cache() < 0)
135 die("index file corrupt");
137 for (i = 0; i < dir.nr; i++)
138 add_file_to_index(dir.entries[i]->name, verbose);
140 if (active_cache_changed) {
141 if (write_cache(newfd, active_cache, active_nr) ||
142 close(newfd) || commit_lock_file(&lock_file))
143 die("Unable to write new index file");
146 return 0;