]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-add.c
Merge branch 'maint'
[thirdparty/git.git] / builtin-add.c
CommitLineData
0d781539
LT
1/*
2 * "git add" builtin command
3 *
4 * Copyright (C) 2006 Linus Torvalds
5 */
0d781539
LT
6#include "cache.h"
7#include "builtin.h"
8#include "dir.h"
93872e07 9#include "cache-tree.h"
0d781539
LT
10
11static const char builtin_add_usage[] =
12"git-add [-n] [-v] <filepattern>...";
13
0d781539
LT
14static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
15{
f2593398
LT
16 char *seen;
17 int i, specs;
0d781539
LT
18 struct dir_entry **src, **dst;
19
f2593398
LT
20 for (specs = 0; pathspec[specs]; specs++)
21 /* nothing */;
28f75818 22 seen = xcalloc(specs, 1);
f2593398 23
0d781539
LT
24 src = dst = dir->entries;
25 i = dir->nr;
26 while (--i >= 0) {
27 struct dir_entry *entry = *src++;
3c6a370b 28 if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
0d781539
LT
29 free(entry);
30 continue;
31 }
32 *dst++ = entry;
33 }
34 dir->nr = dst - dir->entries;
f2593398
LT
35
36 for (i = 0; i < specs; i++) {
37 struct stat st;
38 const char *match;
39 if (seen[i])
40 continue;
41
42 /* Existing file? We must have ignored it */
43 match = pathspec[i];
e8f990b4 44 if (!match[0] || !lstat(match, &st))
f2593398
LT
45 continue;
46 die("pathspec '%s' did not match any files", match);
47 }
0d781539
LT
48}
49
50static void fill_directory(struct dir_struct *dir, const char **pathspec)
51{
52 const char *path, *base;
53 int baselen;
54
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);
61
62 /*
63 * Calculate common prefix for the pathspec, and
64 * use that to optimize the directory walk
65 */
66 baselen = common_prefix(pathspec);
67 path = ".";
68 base = "";
69 if (baselen) {
70 char *common = xmalloc(baselen + 1);
0d781539
LT
71 memcpy(common, *pathspec, baselen);
72 common[baselen] = 0;
73 path = base = common;
74 }
75
76 /* Read the directory and prune it */
77 read_directory(dir, path, base, baselen);
78 if (pathspec)
79 prune_directory(dir, pathspec, baselen);
80}
81
021b6e45 82static struct lock_file lock_file;
0d781539 83
a633fca0 84int cmd_add(int argc, const char **argv, const char *prefix)
0d781539
LT
85{
86 int i, newfd;
87 int verbose = 0, show_only = 0;
0d781539
LT
88 const char **pathspec;
89 struct dir_struct dir;
90
91 git_config(git_default_config);
92
40aaae88 93 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
0d781539 94
0d781539
LT
95 for (i = 1; i < argc; i++) {
96 const char *arg = argv[i];
97
98 if (arg[0] != '-')
99 break;
100 if (!strcmp(arg, "--")) {
101 i++;
102 break;
103 }
104 if (!strcmp(arg, "-n")) {
105 show_only = 1;
106 continue;
107 }
108 if (!strcmp(arg, "-v")) {
109 verbose = 1;
110 continue;
111 }
8cdf3364 112 usage(builtin_add_usage);
0d781539 113 }
0d781539
LT
114 pathspec = get_pathspec(prefix, argv + i);
115
116 fill_directory(&dir, pathspec);
117
118 if (show_only) {
119 const char *sep = "", *eof = "";
120 for (i = 0; i < dir.nr; i++) {
121 printf("%s%s", sep, dir.entries[i]->name);
122 sep = " ";
123 eof = "\n";
124 }
125 fputs(eof, stdout);
126 return 0;
127 }
128
366bfcb6
NP
129 if (read_cache() < 0)
130 die("index file corrupt");
131
0d781539
LT
132 for (i = 0; i < dir.nr; i++)
133 add_file_to_index(dir.entries[i]->name, verbose);
134
135 if (active_cache_changed) {
136 if (write_cache(newfd, active_cache, active_nr) ||
6244b249 137 close(newfd) || commit_lock_file(&lock_file))
0d781539
LT
138 die("Unable to write new index file");
139 }
140
141 return 0;
142}