2 * "git add" builtin command
4 * Copyright (C) 2006 Linus Torvalds
10 #include "cache-tree.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 static const char * const builtin_add_usage
[] = {
19 "git-add [options] [--] <filepattern>...",
23 static int take_worktree_changes
;
24 static const char *excludes_file
;
26 static void prune_directory(struct dir_struct
*dir
, const char **pathspec
, int prefix
)
30 struct dir_entry
**src
, **dst
;
32 for (specs
= 0; pathspec
[specs
]; specs
++)
34 seen
= xcalloc(specs
, 1);
36 src
= dst
= dir
->entries
;
39 struct dir_entry
*entry
= *src
++;
40 if (match_pathspec(pathspec
, entry
->name
, entry
->len
,
44 dir
->nr
= dst
- dir
->entries
;
46 for (i
= 0; i
< specs
; i
++) {
47 if (!seen
[i
] && !file_exists(pathspec
[i
]))
48 die("pathspec '%s' did not match any files",
54 static void fill_directory(struct dir_struct
*dir
, const char **pathspec
,
57 const char *path
, *base
;
60 /* Set up the default git porcelain excludes */
61 memset(dir
, 0, sizeof(*dir
));
63 dir
->collect_ignored
= 1;
64 dir
->exclude_per_dir
= ".gitignore";
65 path
= git_path("info/exclude");
66 if (!access(path
, R_OK
))
67 add_excludes_from_file(dir
, path
);
68 if (excludes_file
!= NULL
&& !access(excludes_file
, R_OK
))
69 add_excludes_from_file(dir
, excludes_file
);
73 * Calculate common prefix for the pathspec, and
74 * use that to optimize the directory walk
76 baselen
= common_prefix(pathspec
);
80 path
= base
= xmemdupz(*pathspec
, baselen
);
82 /* Read the directory and prune it */
83 read_directory(dir
, path
, base
, baselen
, pathspec
);
85 prune_directory(dir
, pathspec
, baselen
);
88 static void update_callback(struct diff_queue_struct
*q
,
89 struct diff_options
*opt
, void *cbdata
)
93 verbose
= *((int *)cbdata
);
94 for (i
= 0; i
< q
->nr
; i
++) {
95 struct diff_filepair
*p
= q
->queue
[i
];
96 const char *path
= p
->one
->path
;
99 die("unexpected diff status %c", p
->status
);
100 case DIFF_STATUS_UNMERGED
:
101 case DIFF_STATUS_MODIFIED
:
102 case DIFF_STATUS_TYPE_CHANGED
:
103 add_file_to_cache(path
, verbose
);
105 case DIFF_STATUS_DELETED
:
106 remove_file_from_cache(path
);
108 printf("remove '%s'\n", path
);
114 static void update(int verbose
, const char *prefix
, const char **files
)
117 init_revisions(&rev
, prefix
);
118 setup_revisions(0, NULL
, &rev
, NULL
);
119 rev
.prune_data
= get_pathspec(prefix
, files
);
120 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
121 rev
.diffopt
.format_callback
= update_callback
;
122 rev
.diffopt
.format_callback_data
= &verbose
;
123 if (read_cache() < 0)
124 die("index file corrupt");
125 run_diff_files(&rev
, 0);
128 static void refresh(int verbose
, const char **pathspec
)
133 for (specs
= 0; pathspec
[specs
]; specs
++)
135 seen
= xcalloc(specs
, 1);
136 if (read_cache() < 0)
137 die("index file corrupt");
138 refresh_index(&the_index
, verbose
? 0 : REFRESH_QUIET
, pathspec
, seen
);
139 for (i
= 0; i
< specs
; i
++) {
141 die("pathspec '%s' did not match any files", pathspec
[i
]);
146 static int git_add_config(const char *var
, const char *value
)
148 if (!strcmp(var
, "core.excludesfile")) {
150 die("core.excludesfile without value");
151 excludes_file
= xstrdup(value
);
155 return git_default_config(var
, value
);
158 static struct lock_file lock_file
;
160 static const char ignore_error
[] =
161 "The following paths are ignored by one of your .gitignore files:\n";
163 static int verbose
= 0, show_only
= 0, ignored_too
= 0, refresh_only
= 0;
164 static int add_interactive
= 0;
166 static struct option builtin_add_options
[] = {
167 OPT__DRY_RUN(&show_only
),
168 OPT__VERBOSE(&verbose
),
170 OPT_BOOLEAN('i', "interactive", &add_interactive
, "interactive picking"),
171 OPT_BOOLEAN('f', NULL
, &ignored_too
, "allow adding otherwise ignored files"),
172 OPT_BOOLEAN('u', NULL
, &take_worktree_changes
, "update tracked files"),
173 OPT_BOOLEAN( 0 , "refresh", &refresh_only
, "don't add, only refresh the index"),
177 int cmd_add(int argc
, const char **argv
, const char *prefix
)
179 int i
, newfd
, orig_argc
= argc
;
180 const char **pathspec
;
181 struct dir_struct dir
;
183 argc
= parse_options(argc
, argv
, builtin_add_options
,
184 builtin_add_usage
, 0);
185 if (add_interactive
) {
186 const char *args
[] = { "add--interactive", NULL
};
188 if (add_interactive
!= 1 || orig_argc
!= 2)
189 die("add --interactive does not take any parameters");
194 git_config(git_add_config
);
196 newfd
= hold_locked_index(&lock_file
, 1);
198 if (take_worktree_changes
) {
199 update(verbose
, prefix
, argv
);
204 fprintf(stderr
, "Nothing specified, nothing added.\n");
205 fprintf(stderr
, "Maybe you wanted to say 'git add .'?\n");
208 pathspec
= get_pathspec(prefix
, argv
);
211 refresh(verbose
, pathspec
);
215 fill_directory(&dir
, pathspec
, ignored_too
);
218 const char *sep
= "", *eof
= "";
219 for (i
= 0; i
< dir
.nr
; i
++) {
220 printf("%s%s", sep
, dir
.entries
[i
]->name
);
228 if (read_cache() < 0)
229 die("index file corrupt");
231 if (dir
.ignored_nr
) {
232 fprintf(stderr
, ignore_error
);
233 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
234 fprintf(stderr
, "%s\n", dir
.ignored
[i
]->name
);
236 fprintf(stderr
, "Use -f if you really want to add them.\n");
237 die("no files added");
240 for (i
= 0; i
< dir
.nr
; i
++)
241 add_file_to_cache(dir
.entries
[i
]->name
, verbose
);
244 if (active_cache_changed
) {
245 if (write_cache(newfd
, active_cache
, active_nr
) ||
246 close(newfd
) || commit_locked_index(&lock_file
))
247 die("Unable to write new index file");