~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-updates

« back to all changes in this revision

Viewing changes to builtin-add.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-04-22 13:31:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20070422133105-xg8fnm18r2cxcbg1
Tags: upstream-1.5.1.2
ImportĀ upstreamĀ versionĀ 1.5.1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Copyright (C) 2006 Linus Torvalds
5
5
 */
6
 
#include <fnmatch.h>
7
 
 
8
6
#include "cache.h"
9
7
#include "builtin.h"
10
8
#include "dir.h"
 
9
#include "exec_cmd.h"
11
10
#include "cache-tree.h"
12
11
 
13
12
static const char builtin_add_usage[] =
14
 
"git-add [-n] [-v] <filepattern>...";
 
13
"git-add [-n] [-v] [-f] [--interactive | -i] [--] <filepattern>...";
 
14
 
 
15
static const char *excludes_file;
15
16
 
16
17
static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
17
18
{
27
28
        i = dir->nr;
28
29
        while (--i >= 0) {
29
30
                struct dir_entry *entry = *src++;
30
 
                if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
31
 
                        free(entry);
32
 
                        continue;
33
 
                }
34
 
                *dst++ = entry;
 
31
                if (match_pathspec(pathspec, entry->name, entry->len,
 
32
                                   prefix, seen))
 
33
                        *dst++ = entry;
35
34
        }
36
35
        dir->nr = dst - dir->entries;
37
36
 
41
40
                if (seen[i])
42
41
                        continue;
43
42
 
 
43
                match = pathspec[i];
 
44
                if (!match[0])
 
45
                        continue;
 
46
 
44
47
                /* Existing file? We must have ignored it */
45
 
                match = pathspec[i];
46
 
                if (!match[0] || !lstat(match, &st))
 
48
                if (!lstat(match, &st)) {
 
49
                        struct dir_entry *ent;
 
50
 
 
51
                        ent = dir_add_name(dir, match, strlen(match));
 
52
                        ent->ignored = 1;
 
53
                        if (S_ISDIR(st.st_mode))
 
54
                                ent->ignored_dir = 1;
47
55
                        continue;
 
56
                }
48
57
                die("pathspec '%s' did not match any files", match);
49
58
        }
50
59
}
60
69
        path = git_path("info/exclude");
61
70
        if (!access(path, R_OK))
62
71
                add_excludes_from_file(dir, path);
 
72
        if (!access(excludes_file, R_OK))
 
73
                add_excludes_from_file(dir, excludes_file);
63
74
 
64
75
        /*
65
76
         * Calculate common prefix for the pathspec, and
81
92
                prune_directory(dir, pathspec, baselen);
82
93
}
83
94
 
 
95
static int git_add_config(const char *var, const char *value)
 
96
{
 
97
        if (!strcmp(var, "core.excludesfile")) {
 
98
                if (!value)
 
99
                        die("core.excludesfile without value");
 
100
                excludes_file = xstrdup(value);
 
101
                return 0;
 
102
        }
 
103
 
 
104
        return git_default_config(var, value);
 
105
}
 
106
 
84
107
static struct lock_file lock_file;
85
108
 
 
109
static const char ignore_warning[] =
 
110
"The following paths are ignored by one of your .gitignore files:\n";
 
111
 
86
112
int cmd_add(int argc, const char **argv, const char *prefix)
87
113
{
88
114
        int i, newfd;
89
 
        int verbose = 0, show_only = 0;
 
115
        int verbose = 0, show_only = 0, ignored_too = 0;
90
116
        const char **pathspec;
91
117
        struct dir_struct dir;
92
 
 
93
 
        git_config(git_default_config);
 
118
        int add_interactive = 0;
 
119
 
 
120
        for (i = 1; i < argc; i++) {
 
121
                if (!strcmp("--interactive", argv[i]) ||
 
122
                    !strcmp("-i", argv[i]))
 
123
                        add_interactive++;
 
124
        }
 
125
        if (add_interactive) {
 
126
                const char *args[] = { "add--interactive", NULL };
 
127
 
 
128
                if (add_interactive != 1 || argc != 2)
 
129
                        die("add --interactive does not take any parameters");
 
130
                execv_git_cmd(args);
 
131
                exit(1);
 
132
        }
 
133
 
 
134
        git_config(git_add_config);
94
135
 
95
136
        newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
96
137
 
97
 
        if (read_cache() < 0)
98
 
                die("index file corrupt");
99
 
 
100
138
        for (i = 1; i < argc; i++) {
101
139
                const char *arg = argv[i];
102
140
 
110
148
                        show_only = 1;
111
149
                        continue;
112
150
                }
 
151
                if (!strcmp(arg, "-f")) {
 
152
                        ignored_too = 1;
 
153
                        continue;
 
154
                }
113
155
                if (!strcmp(arg, "-v")) {
114
156
                        verbose = 1;
115
157
                        continue;
116
158
                }
117
159
                usage(builtin_add_usage);
118
160
        }
 
161
        if (argc <= i) {
 
162
                fprintf(stderr, "Nothing specified, nothing added.\n");
 
163
                fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 
164
                return 0;
 
165
        }
119
166
        pathspec = get_pathspec(prefix, argv + i);
120
167
 
121
168
        fill_directory(&dir, pathspec);
123
170
        if (show_only) {
124
171
                const char *sep = "", *eof = "";
125
172
                for (i = 0; i < dir.nr; i++) {
 
173
                        if (!ignored_too && dir.entries[i]->ignored)
 
174
                                continue;
126
175
                        printf("%s%s", sep, dir.entries[i]->name);
127
176
                        sep = " ";
128
177
                        eof = "\n";
131
180
                return 0;
132
181
        }
133
182
 
 
183
        if (read_cache() < 0)
 
184
                die("index file corrupt");
 
185
 
 
186
        if (!ignored_too) {
 
187
                int has_ignored = 0;
 
188
                for (i = 0; i < dir.nr; i++)
 
189
                        if (dir.entries[i]->ignored)
 
190
                                has_ignored = 1;
 
191
                if (has_ignored) {
 
192
                        fprintf(stderr, ignore_warning);
 
193
                        for (i = 0; i < dir.nr; i++) {
 
194
                                if (!dir.entries[i]->ignored)
 
195
                                        continue;
 
196
                                fprintf(stderr, "%s", dir.entries[i]->name);
 
197
                                if (dir.entries[i]->ignored_dir)
 
198
                                        fprintf(stderr, " (directory)");
 
199
                                fputc('\n', stderr);
 
200
                        }
 
201
                        fprintf(stderr,
 
202
                                "Use -f if you really want to add them.\n");
 
203
                        exit(1);
 
204
                }
 
205
        }
 
206
 
134
207
        for (i = 0; i < dir.nr; i++)
135
208
                add_file_to_index(dir.entries[i]->name, verbose);
136
209