Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git add" builtin command |
| 3 | * |
| 4 | * Copyright (C) 2006 Linus Torvalds |
| 5 | */ |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "builtin.h" |
| 8 | #include "dir.h" |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 9 | #include "exec_cmd.h" |
Junio C Hamano | 93872e0 | 2006-05-20 01:28:49 -0700 | [diff] [blame] | 10 | #include "cache-tree.h" |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 11 | #include "diff.h" |
| 12 | #include "diffcore.h" |
| 13 | #include "commit.h" |
| 14 | #include "revision.h" |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 15 | #include "run-command.h" |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 16 | #include "parse-options.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 17 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 18 | static const char * const builtin_add_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 19 | "git add [options] [--] <filepattern>...", |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 20 | NULL |
| 21 | }; |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 22 | static int patch_interactive = 0, add_interactive = 0; |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 23 | static int take_worktree_changes; |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 24 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 25 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
| 26 | { |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 27 | char *seen; |
| 28 | int i, specs; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 29 | struct dir_entry **src, **dst; |
| 30 | |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 31 | for (specs = 0; pathspec[specs]; specs++) |
| 32 | /* nothing */; |
Peter Eriksen | 28f7581 | 2006-07-25 09:30:18 +0200 | [diff] [blame] | 33 | seen = xcalloc(specs, 1); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 35 | src = dst = dir->entries; |
| 36 | i = dir->nr; |
| 37 | while (--i >= 0) { |
| 38 | struct dir_entry *entry = *src++; |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 39 | if (match_pathspec(pathspec, entry->name, entry->len, |
| 40 | prefix, seen)) |
| 41 | *dst++ = entry; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 42 | } |
| 43 | dir->nr = dst - dir->entries; |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 44 | |
| 45 | for (i = 0; i < specs; i++) { |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 46 | if (!seen[i] && !file_exists(pathspec[i])) |
| 47 | die("pathspec '%s' did not match any files", |
| 48 | pathspec[i]); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 49 | } |
Benoit Sigoure | 399f0a8 | 2007-10-29 08:00:33 +0100 | [diff] [blame] | 50 | free(seen); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 53 | static void fill_directory(struct dir_struct *dir, const char **pathspec, |
| 54 | int ignored_too) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 55 | { |
| 56 | const char *path, *base; |
| 57 | int baselen; |
| 58 | |
| 59 | /* Set up the default git porcelain excludes */ |
| 60 | memset(dir, 0, sizeof(*dir)); |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 61 | if (!ignored_too) { |
| 62 | dir->collect_ignored = 1; |
Junio C Hamano | 039bc64 | 2007-11-14 00:05:00 -0800 | [diff] [blame] | 63 | setup_standard_excludes(dir); |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 64 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Calculate common prefix for the pathspec, and |
| 68 | * use that to optimize the directory walk |
| 69 | */ |
| 70 | baselen = common_prefix(pathspec); |
| 71 | path = "."; |
| 72 | base = ""; |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 73 | if (baselen) |
| 74 | path = base = xmemdupz(*pathspec, baselen); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 75 | |
| 76 | /* Read the directory and prune it */ |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 77 | read_directory(dir, path, base, baselen, pathspec); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 78 | if (pathspec) |
| 79 | prune_directory(dir, pathspec, baselen); |
| 80 | } |
| 81 | |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 82 | struct update_callback_data |
| 83 | { |
| 84 | int flags; |
| 85 | int add_errors; |
| 86 | }; |
| 87 | |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 88 | static void update_callback(struct diff_queue_struct *q, |
| 89 | struct diff_options *opt, void *cbdata) |
| 90 | { |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 91 | int i; |
| 92 | struct update_callback_data *data = cbdata; |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 93 | |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 94 | for (i = 0; i < q->nr; i++) { |
| 95 | struct diff_filepair *p = q->queue[i]; |
| 96 | const char *path = p->one->path; |
| 97 | switch (p->status) { |
| 98 | default: |
Benoit Sigoure | 43b98ac | 2007-09-14 10:29:04 +0200 | [diff] [blame] | 99 | die("unexpected diff status %c", p->status); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 100 | case DIFF_STATUS_UNMERGED: |
| 101 | case DIFF_STATUS_MODIFIED: |
Junio C Hamano | 767c98a | 2007-09-14 00:45:29 -0700 | [diff] [blame] | 102 | case DIFF_STATUS_TYPE_CHANGED: |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 103 | if (add_file_to_cache(path, data->flags)) { |
| 104 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 105 | die("updating files failed"); |
| 106 | data->add_errors++; |
| 107 | } |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 108 | break; |
| 109 | case DIFF_STATUS_DELETED: |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 110 | if (!(data->flags & ADD_CACHE_PRETEND)) |
Junio C Hamano | 38ed1d8 | 2008-05-21 12:04:34 -0700 | [diff] [blame] | 111 | remove_file_from_cache(path); |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 112 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 113 | printf("remove '%s'\n", path); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 119 | int add_files_to_cache(const char *prefix, const char **pathspec, int flags) |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 120 | { |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 121 | struct update_callback_data data; |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 122 | struct rev_info rev; |
Salikh Zakirov | 2ed2c22 | 2007-08-16 02:01:43 +0900 | [diff] [blame] | 123 | init_revisions(&rev, prefix); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 124 | setup_revisions(0, NULL, &rev, NULL); |
Junio C Hamano | b6ec1d6 | 2007-11-18 01:12:04 -0800 | [diff] [blame] | 125 | rev.prune_data = pathspec; |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 126 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 127 | rev.diffopt.format_callback = update_callback; |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 128 | data.flags = flags; |
| 129 | data.add_errors = 0; |
| 130 | rev.diffopt.format_callback_data = &data; |
Junio C Hamano | fb63d7f | 2007-11-09 18:22:52 -0800 | [diff] [blame] | 131 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 132 | return !!data.add_errors; |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 135 | static void refresh(int verbose, const char **pathspec) |
| 136 | { |
| 137 | char *seen; |
| 138 | int i, specs; |
| 139 | |
| 140 | for (specs = 0; pathspec[specs]; specs++) |
| 141 | /* nothing */; |
| 142 | seen = xcalloc(specs, 1); |
Junio C Hamano | d14e740 | 2008-07-20 00:21:38 -0700 | [diff] [blame] | 143 | refresh_index(&the_index, verbose ? REFRESH_SAY_CHANGED : REFRESH_QUIET, |
| 144 | pathspec, seen); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 145 | for (i = 0; i < specs; i++) { |
| 146 | if (!seen[i]) |
| 147 | die("pathspec '%s' did not match any files", pathspec[i]); |
| 148 | } |
Benoit Sigoure | 399f0a8 | 2007-10-29 08:00:33 +0100 | [diff] [blame] | 149 | free(seen); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 152 | static const char **validate_pathspec(int argc, const char **argv, const char *prefix) |
| 153 | { |
| 154 | const char **pathspec = get_pathspec(prefix, argv); |
| 155 | |
| 156 | return pathspec; |
| 157 | } |
| 158 | |
| 159 | int interactive_add(int argc, const char **argv, const char *prefix) |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 160 | { |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 161 | int status, ac; |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 162 | const char **args; |
| 163 | const char **pathspec = NULL; |
Junio C Hamano | 324ccbd | 2007-11-25 10:07:55 -0800 | [diff] [blame] | 164 | |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 165 | if (argc) { |
| 166 | pathspec = validate_pathspec(argc, argv, prefix); |
| 167 | if (!pathspec) |
| 168 | return -1; |
| 169 | } |
| 170 | |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 171 | args = xcalloc(sizeof(const char *), (argc + 4)); |
| 172 | ac = 0; |
| 173 | args[ac++] = "add--interactive"; |
| 174 | if (patch_interactive) |
| 175 | args[ac++] = "--patch"; |
| 176 | args[ac++] = "--"; |
| 177 | if (argc) { |
| 178 | memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc); |
| 179 | ac += argc; |
| 180 | } |
| 181 | args[ac] = NULL; |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 182 | |
Wincent Colaiuta | 7c0ab44 | 2007-11-22 01:02:52 +0100 | [diff] [blame] | 183 | status = run_command_v_opt(args, RUN_GIT_CMD); |
| 184 | free(args); |
| 185 | return status; |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 188 | static struct lock_file lock_file; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 189 | |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 190 | static const char ignore_error[] = |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 191 | "The following paths are ignored by one of your .gitignore files:\n"; |
| 192 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 193 | static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 194 | static int ignore_add_errors, addremove; |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 195 | |
| 196 | static struct option builtin_add_options[] = { |
| 197 | OPT__DRY_RUN(&show_only), |
| 198 | OPT__VERBOSE(&verbose), |
| 199 | OPT_GROUP(""), |
| 200 | OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"), |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 201 | OPT_BOOLEAN('p', "patch", &patch_interactive, "interactive patching"), |
SZEDER Gábor | 69c61c4 | 2008-06-14 11:48:00 +0200 | [diff] [blame] | 202 | OPT_BOOLEAN('f', "force", &ignored_too, "allow adding otherwise ignored files"), |
| 203 | OPT_BOOLEAN('u', "update", &take_worktree_changes, "update tracked files"), |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 204 | OPT_BOOLEAN('A', "all", &addremove, "add all, noticing removal of tracked files"), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 205 | OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"), |
Alex Riesen | 984b83e | 2008-05-12 19:58:29 +0200 | [diff] [blame] | 206 | OPT_BOOLEAN( 0 , "ignore-errors", &ignore_add_errors, "just skip files which cannot be added because of errors"), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 207 | OPT_END(), |
| 208 | }; |
| 209 | |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 210 | static int add_config(const char *var, const char *value, void *cb) |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 211 | { |
| 212 | if (!strcasecmp(var, "add.ignore-errors")) { |
| 213 | ignore_add_errors = git_config_bool(var, value); |
| 214 | return 0; |
| 215 | } |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 216 | return git_default_config(var, value, cb); |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 219 | static int add_files(struct dir_struct *dir, int flags) |
| 220 | { |
| 221 | int i, exit_status = 0; |
| 222 | |
| 223 | if (dir->ignored_nr) { |
| 224 | fprintf(stderr, ignore_error); |
| 225 | for (i = 0; i < dir->ignored_nr; i++) |
| 226 | fprintf(stderr, "%s\n", dir->ignored[i]->name); |
| 227 | fprintf(stderr, "Use -f if you really want to add them.\n"); |
| 228 | die("no files added"); |
| 229 | } |
| 230 | |
| 231 | for (i = 0; i < dir->nr; i++) |
| 232 | if (add_file_to_cache(dir->entries[i]->name, flags)) { |
| 233 | if (!ignore_add_errors) |
| 234 | die("adding files failed"); |
| 235 | exit_status = 1; |
| 236 | } |
| 237 | return exit_status; |
| 238 | } |
| 239 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 240 | int cmd_add(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 241 | { |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 242 | int exit_status = 0; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 243 | int newfd; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 244 | const char **pathspec; |
| 245 | struct dir_struct dir; |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 246 | int flags; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 247 | int add_new_files; |
| 248 | int require_pathspec; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 249 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 250 | argc = parse_options(argc, argv, builtin_add_options, |
| 251 | builtin_add_usage, 0); |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 252 | if (patch_interactive) |
| 253 | add_interactive = 1; |
Wincent Colaiuta | 7c0ab44 | 2007-11-22 01:02:52 +0100 | [diff] [blame] | 254 | if (add_interactive) |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 255 | exit(interactive_add(argc, argv, prefix)); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 256 | |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 257 | git_config(add_config, NULL); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 258 | |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 259 | if (addremove && take_worktree_changes) |
| 260 | die("-A and -u are mutually incompatible"); |
| 261 | if (addremove && !argc) { |
| 262 | static const char *here[2] = { ".", NULL }; |
| 263 | argc = 1; |
| 264 | argv = here; |
| 265 | } |
| 266 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 267 | add_new_files = !take_worktree_changes && !refresh_only; |
| 268 | require_pathspec = !take_worktree_changes; |
| 269 | |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 270 | newfd = hold_locked_index(&lock_file, 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 271 | |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 272 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 273 | (show_only ? ADD_CACHE_PRETEND : 0) | |
| 274 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0)); |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 275 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 276 | if (require_pathspec && argc == 0) { |
Junio C Hamano | 93b0d86 | 2006-12-20 13:06:46 -0800 | [diff] [blame] | 277 | fprintf(stderr, "Nothing specified, nothing added.\n"); |
| 278 | fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); |
| 279 | return 0; |
| 280 | } |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 281 | pathspec = get_pathspec(prefix, argv); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 282 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 283 | /* |
| 284 | * If we are adding new files, we need to scan the working |
| 285 | * tree to find the ones that match pathspecs; this needs |
| 286 | * to be done before we read the index. |
| 287 | */ |
| 288 | if (add_new_files) |
| 289 | fill_directory(&dir, pathspec, ignored_too); |
| 290 | |
| 291 | if (read_cache() < 0) |
| 292 | die("index file corrupt"); |
| 293 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 294 | if (refresh_only) { |
| 295 | refresh(verbose, pathspec); |
| 296 | goto finish; |
| 297 | } |
| 298 | |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 299 | if (take_worktree_changes || addremove) |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 300 | exit_status |= add_files_to_cache(prefix, pathspec, flags); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 301 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 302 | if (add_new_files) |
| 303 | exit_status |= add_files(&dir, flags); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 304 | |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 305 | finish: |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 306 | if (active_cache_changed) { |
| 307 | if (write_cache(newfd, active_cache, active_nr) || |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 308 | commit_locked_index(&lock_file)) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 309 | die("Unable to write new index file"); |
| 310 | } |
| 311 | |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 312 | return exit_status; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 313 | } |