]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-add.c
Merge branch 'jc/git-add--interactive'
[git.git] / builtin-add.c
1 /*
2  * "git add" builtin command
3  *
4  * Copyright (C) 2006 Linus Torvalds
5  */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "dir.h"
9 #include "exec_cmd.h"
10 #include "cache-tree.h"
11
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] [--interactive] [--] <filepattern>...";
14
15 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
16 {
17         char *seen;
18         int i, specs;
19         struct dir_entry **src, **dst;
20
21         for (specs = 0; pathspec[specs];  specs++)
22                 /* nothing */;
23         seen = xcalloc(specs, 1);
24
25         src = dst = dir->entries;
26         i = dir->nr;
27         while (--i >= 0) {
28                 struct dir_entry *entry = *src++;
29                 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
30                         free(entry);
31                         continue;
32                 }
33                 *dst++ = entry;
34         }
35         dir->nr = dst - dir->entries;
36
37         for (i = 0; i < specs; i++) {
38                 struct stat st;
39                 const char *match;
40                 if (seen[i])
41                         continue;
42
43                 /* Existing file? We must have ignored it */
44                 match = pathspec[i];
45                 if (!match[0] || !lstat(match, &st))
46                         continue;
47                 die("pathspec '%s' did not match any files", match);
48         }
49 }
50
51 static void fill_directory(struct dir_struct *dir, const char **pathspec)
52 {
53         const char *path, *base;
54         int baselen;
55
56         /* Set up the default git porcelain excludes */
57         memset(dir, 0, sizeof(*dir));
58         dir->exclude_per_dir = ".gitignore";
59         path = git_path("info/exclude");
60         if (!access(path, R_OK))
61                 add_excludes_from_file(dir, path);
62
63         /*
64          * Calculate common prefix for the pathspec, and
65          * use that to optimize the directory walk
66          */
67         baselen = common_prefix(pathspec);
68         path = ".";
69         base = "";
70         if (baselen) {
71                 char *common = xmalloc(baselen + 1);
72                 memcpy(common, *pathspec, baselen);
73                 common[baselen] = 0;
74                 path = base = common;
75         }
76
77         /* Read the directory and prune it */
78         read_directory(dir, path, base, baselen);
79         if (pathspec)
80                 prune_directory(dir, pathspec, baselen);
81 }
82
83 static struct lock_file lock_file;
84
85 int cmd_add(int argc, const char **argv, const char *prefix)
86 {
87         int i, newfd;
88         int verbose = 0, show_only = 0;
89         const char **pathspec;
90         struct dir_struct dir;
91         int add_interactive = 0;
92
93         for (i = 1; i < argc; i++) {
94                 if (!strcmp("--interactive", argv[i]))
95                         add_interactive++;
96         }
97         if (add_interactive) {
98                 const char *args[] = { "add--interactive", NULL };
99
100                 if (add_interactive != 1 || argc != 2)
101                         die("add --interactive does not take any parameters");
102                 execv_git_cmd(args);
103                 exit(1);
104         }
105
106         git_config(git_default_config);
107
108         newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
109
110         for (i = 1; i < argc; i++) {
111                 const char *arg = argv[i];
112
113                 if (arg[0] != '-')
114                         break;
115                 if (!strcmp(arg, "--")) {
116                         i++;
117                         break;
118                 }
119                 if (!strcmp(arg, "-n")) {
120                         show_only = 1;
121                         continue;
122                 }
123                 if (!strcmp(arg, "-v")) {
124                         verbose = 1;
125                         continue;
126                 }
127                 usage(builtin_add_usage);
128         }
129         if (argc <= i) {
130                 fprintf(stderr, "Nothing specified, nothing added.\n");
131                 fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
132                 return 0;
133         }
134         pathspec = get_pathspec(prefix, argv + i);
135
136         fill_directory(&dir, pathspec);
137
138         if (show_only) {
139                 const char *sep = "", *eof = "";
140                 for (i = 0; i < dir.nr; i++) {
141                         printf("%s%s", sep, dir.entries[i]->name);
142                         sep = " ";
143                         eof = "\n";
144                 }
145                 fputs(eof, stdout);
146                 return 0;
147         }
148
149         if (read_cache() < 0)
150                 die("index file corrupt");
151
152         for (i = 0; i < dir.nr; i++)
153                 add_file_to_index(dir.entries[i]->name, verbose);
154
155         if (active_cache_changed) {
156                 if (write_cache(newfd, active_cache, active_nr) ||
157                     close(newfd) || commit_lock_file(&lock_file))
158                         die("Unable to write new index file");
159         }
160
161         return 0;
162 }