git-add --interactive: add documentation
[git/dscho.git] / builtin-add.c
blob9443bae5352326a1915dd5f6ed65bc1aa4815408
1 /*
2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
5 */
6 #include <fnmatch.h>
8 #include "cache.h"
9 #include "builtin.h"
10 #include "dir.h"
11 #include "exec_cmd.h"
12 #include "cache-tree.h"
14 static const char builtin_add_usage[] =
15 "git-add [-n] [-v] [--interactive] [--] <filepattern>...";
17 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
19 char *seen;
20 int i, specs;
21 struct dir_entry **src, **dst;
23 for (specs = 0; pathspec[specs]; specs++)
24 /* nothing */;
25 seen = xcalloc(specs, 1);
27 src = dst = dir->entries;
28 i = dir->nr;
29 while (--i >= 0) {
30 struct dir_entry *entry = *src++;
31 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
32 free(entry);
33 continue;
35 *dst++ = entry;
37 dir->nr = dst - dir->entries;
39 for (i = 0; i < specs; i++) {
40 struct stat st;
41 const char *match;
42 if (seen[i])
43 continue;
45 /* Existing file? We must have ignored it */
46 match = pathspec[i];
47 if (!match[0] || !lstat(match, &st))
48 continue;
49 die("pathspec '%s' did not match any files", match);
53 static void fill_directory(struct dir_struct *dir, const char **pathspec)
55 const char *path, *base;
56 int baselen;
58 /* Set up the default git porcelain excludes */
59 memset(dir, 0, sizeof(*dir));
60 dir->exclude_per_dir = ".gitignore";
61 path = git_path("info/exclude");
62 if (!access(path, R_OK))
63 add_excludes_from_file(dir, path);
66 * Calculate common prefix for the pathspec, and
67 * use that to optimize the directory walk
69 baselen = common_prefix(pathspec);
70 path = ".";
71 base = "";
72 if (baselen) {
73 char *common = xmalloc(baselen + 1);
74 memcpy(common, *pathspec, baselen);
75 common[baselen] = 0;
76 path = base = common;
79 /* Read the directory and prune it */
80 read_directory(dir, path, base, baselen);
81 if (pathspec)
82 prune_directory(dir, pathspec, baselen);
85 static struct lock_file lock_file;
87 int cmd_add(int argc, const char **argv, const char *prefix)
89 int i, newfd;
90 int verbose = 0, show_only = 0;
91 const char **pathspec;
92 struct dir_struct dir;
93 int add_interactive = 0;
95 for (i = 1; i < argc; i++) {
96 if (!strcmp("--interactive", argv[i]))
97 add_interactive++;
99 if (add_interactive) {
100 const char *args[] = { "add--interactive", NULL };
102 if (add_interactive != 1 || argc != 2)
103 die("add --interactive does not take any parameters");
104 execv_git_cmd(args);
105 exit(1);
108 git_config(git_default_config);
110 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
112 for (i = 1; i < argc; i++) {
113 const char *arg = argv[i];
115 if (arg[0] != '-')
116 break;
117 if (!strcmp(arg, "--")) {
118 i++;
119 break;
121 if (!strcmp(arg, "-n")) {
122 show_only = 1;
123 continue;
125 if (!strcmp(arg, "-v")) {
126 verbose = 1;
127 continue;
129 usage(builtin_add_usage);
131 pathspec = get_pathspec(prefix, argv + i);
133 fill_directory(&dir, pathspec);
135 if (show_only) {
136 const char *sep = "", *eof = "";
137 for (i = 0; i < dir.nr; i++) {
138 printf("%s%s", sep, dir.entries[i]->name);
139 sep = " ";
140 eof = "\n";
142 fputs(eof, stdout);
143 return 0;
146 if (read_cache() < 0)
147 die("index file corrupt");
149 for (i = 0; i < dir.nr; i++)
150 add_file_to_index(dir.entries[i]->name, verbose);
152 if (active_cache_changed) {
153 if (write_cache(newfd, active_cache, active_nr) ||
154 close(newfd) || commit_lock_file(&lock_file))
155 die("Unable to write new index file");
158 return 0;