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" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 15 | |
| 16 | static const char builtin_add_usage[] = |
Brian Hetro | 480611d | 2007-08-25 23:20:06 -0400 | [diff] [blame] | 17 | "git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>..."; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 18 | |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 19 | static int take_worktree_changes; |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 20 | static const char *excludes_file; |
| 21 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 22 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
| 23 | { |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 24 | char *seen; |
| 25 | int i, specs; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 26 | struct dir_entry **src, **dst; |
| 27 | |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 28 | for (specs = 0; pathspec[specs]; specs++) |
| 29 | /* nothing */; |
Peter Eriksen | 28f7581 | 2006-07-25 09:30:18 +0200 | [diff] [blame] | 30 | seen = xcalloc(specs, 1); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 32 | src = dst = dir->entries; |
| 33 | i = dir->nr; |
| 34 | while (--i >= 0) { |
| 35 | struct dir_entry *entry = *src++; |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 36 | if (match_pathspec(pathspec, entry->name, entry->len, |
| 37 | prefix, seen)) |
| 38 | *dst++ = entry; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 39 | } |
| 40 | dir->nr = dst - dir->entries; |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 41 | |
| 42 | for (i = 0; i < specs; i++) { |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 43 | if (!seen[i] && !file_exists(pathspec[i])) |
| 44 | die("pathspec '%s' did not match any files", |
| 45 | pathspec[i]); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 46 | } |
Benoit Sigoure | 399f0a8 | 2007-10-29 08:00:33 +0100 | [diff] [blame] | 47 | free(seen); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 50 | static void fill_directory(struct dir_struct *dir, const char **pathspec, |
| 51 | int ignored_too) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 52 | { |
| 53 | const char *path, *base; |
| 54 | int baselen; |
| 55 | |
| 56 | /* Set up the default git porcelain excludes */ |
| 57 | memset(dir, 0, sizeof(*dir)); |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 58 | if (!ignored_too) { |
| 59 | dir->collect_ignored = 1; |
| 60 | dir->exclude_per_dir = ".gitignore"; |
| 61 | path = git_path("info/exclude"); |
| 62 | if (!access(path, R_OK)) |
| 63 | add_excludes_from_file(dir, path); |
Thomas Schwinge | 8b4aee0 | 2007-07-28 20:26:35 +0200 | [diff] [blame] | 64 | if (excludes_file != NULL && !access(excludes_file, R_OK)) |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 65 | add_excludes_from_file(dir, excludes_file); |
| 66 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Calculate common prefix for the pathspec, and |
| 70 | * use that to optimize the directory walk |
| 71 | */ |
| 72 | baselen = common_prefix(pathspec); |
| 73 | path = "."; |
| 74 | base = ""; |
| 75 | if (baselen) { |
| 76 | char *common = xmalloc(baselen + 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 77 | memcpy(common, *pathspec, baselen); |
| 78 | common[baselen] = 0; |
| 79 | path = base = common; |
| 80 | } |
| 81 | |
| 82 | /* Read the directory and prune it */ |
Linus Torvalds | 9fc42d6 | 2007-03-30 20:39:30 -0700 | [diff] [blame] | 83 | read_directory(dir, path, base, baselen, pathspec); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 84 | if (pathspec) |
| 85 | prune_directory(dir, pathspec, baselen); |
| 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 | { |
| 91 | int i, verbose; |
| 92 | |
| 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; |
| 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 | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 103 | add_file_to_cache(path, verbose); |
| 104 | break; |
| 105 | case DIFF_STATUS_DELETED: |
| 106 | remove_file_from_cache(path); |
Junio C Hamano | a4882c2 | 2007-08-15 14:12:14 -0700 | [diff] [blame] | 107 | cache_tree_invalidate_path(active_cache_tree, path); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 108 | if (verbose) |
| 109 | printf("remove '%s'\n", path); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
Salikh Zakirov | 2ed2c22 | 2007-08-16 02:01:43 +0900 | [diff] [blame] | 115 | static void update(int verbose, const char *prefix, const char **files) |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 116 | { |
| 117 | struct rev_info rev; |
Salikh Zakirov | 2ed2c22 | 2007-08-16 02:01:43 +0900 | [diff] [blame] | 118 | init_revisions(&rev, prefix); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 119 | setup_revisions(0, NULL, &rev, NULL); |
Salikh Zakirov | 2ed2c22 | 2007-08-16 02:01:43 +0900 | [diff] [blame] | 120 | rev.prune_data = get_pathspec(prefix, files); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 121 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 122 | rev.diffopt.format_callback = update_callback; |
| 123 | rev.diffopt.format_callback_data = &verbose; |
| 124 | if (read_cache() < 0) |
| 125 | die("index file corrupt"); |
Junio C Hamano | fb63d7f | 2007-11-09 18:22:52 -0800 | [diff] [blame^] | 126 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 129 | static void refresh(int verbose, const char **pathspec) |
| 130 | { |
| 131 | char *seen; |
| 132 | int i, specs; |
| 133 | |
| 134 | for (specs = 0; pathspec[specs]; specs++) |
| 135 | /* nothing */; |
| 136 | seen = xcalloc(specs, 1); |
| 137 | if (read_cache() < 0) |
| 138 | die("index file corrupt"); |
| 139 | refresh_index(&the_index, verbose ? 0 : REFRESH_QUIET, pathspec, seen); |
| 140 | for (i = 0; i < specs; i++) { |
| 141 | if (!seen[i]) |
| 142 | die("pathspec '%s' did not match any files", pathspec[i]); |
| 143 | } |
Benoit Sigoure | 399f0a8 | 2007-10-29 08:00:33 +0100 | [diff] [blame] | 144 | free(seen); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 145 | } |
| 146 | |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 147 | static int git_add_config(const char *var, const char *value) |
| 148 | { |
| 149 | if (!strcmp(var, "core.excludesfile")) { |
| 150 | if (!value) |
| 151 | die("core.excludesfile without value"); |
| 152 | excludes_file = xstrdup(value); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | return git_default_config(var, value); |
| 157 | } |
| 158 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 159 | static struct lock_file lock_file; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 160 | |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 161 | static const char ignore_error[] = |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 162 | "The following paths are ignored by one of your .gitignore files:\n"; |
| 163 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 164 | int cmd_add(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 165 | { |
| 166 | int i, newfd; |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 167 | int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 168 | const char **pathspec; |
| 169 | struct dir_struct dir; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 170 | int add_interactive = 0; |
| 171 | |
| 172 | for (i = 1; i < argc; i++) { |
Junio C Hamano | df59afe | 2007-01-17 10:52:36 -0800 | [diff] [blame] | 173 | if (!strcmp("--interactive", argv[i]) || |
| 174 | !strcmp("-i", argv[i])) |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 175 | add_interactive++; |
| 176 | } |
| 177 | if (add_interactive) { |
| 178 | const char *args[] = { "add--interactive", NULL }; |
| 179 | |
| 180 | if (add_interactive != 1 || argc != 2) |
| 181 | die("add --interactive does not take any parameters"); |
| 182 | execv_git_cmd(args); |
| 183 | exit(1); |
| 184 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 185 | |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 186 | git_config(git_add_config); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 187 | |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 188 | newfd = hold_locked_index(&lock_file, 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 189 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 190 | for (i = 1; i < argc; i++) { |
| 191 | const char *arg = argv[i]; |
| 192 | |
| 193 | if (arg[0] != '-') |
| 194 | break; |
| 195 | if (!strcmp(arg, "--")) { |
| 196 | i++; |
| 197 | break; |
| 198 | } |
| 199 | if (!strcmp(arg, "-n")) { |
| 200 | show_only = 1; |
| 201 | continue; |
| 202 | } |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 203 | if (!strcmp(arg, "-f")) { |
| 204 | ignored_too = 1; |
| 205 | continue; |
| 206 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 207 | if (!strcmp(arg, "-v")) { |
| 208 | verbose = 1; |
| 209 | continue; |
| 210 | } |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 211 | if (!strcmp(arg, "-u")) { |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 212 | take_worktree_changes = 1; |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 213 | continue; |
| 214 | } |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 215 | if (!strcmp(arg, "--refresh")) { |
| 216 | refresh_only = 1; |
| 217 | continue; |
| 218 | } |
Ramsay Allan Jones | 8cdf336 | 2006-08-03 16:48:41 +0100 | [diff] [blame] | 219 | usage(builtin_add_usage); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 220 | } |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 221 | |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 222 | if (take_worktree_changes) { |
Salikh Zakirov | 2ed2c22 | 2007-08-16 02:01:43 +0900 | [diff] [blame] | 223 | update(verbose, prefix, argv + i); |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 224 | goto finish; |
| 225 | } |
| 226 | |
Junio C Hamano | 93b0d86 | 2006-12-20 13:06:46 -0800 | [diff] [blame] | 227 | if (argc <= i) { |
| 228 | fprintf(stderr, "Nothing specified, nothing added.\n"); |
| 229 | fprintf(stderr, "Maybe you wanted to say 'git add .'?\n"); |
| 230 | return 0; |
| 231 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 232 | pathspec = get_pathspec(prefix, argv + i); |
| 233 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 234 | if (refresh_only) { |
| 235 | refresh(verbose, pathspec); |
| 236 | goto finish; |
| 237 | } |
| 238 | |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 239 | fill_directory(&dir, pathspec, ignored_too); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 240 | |
| 241 | if (show_only) { |
| 242 | const char *sep = "", *eof = ""; |
| 243 | for (i = 0; i < dir.nr; i++) { |
| 244 | printf("%s%s", sep, dir.entries[i]->name); |
| 245 | sep = " "; |
| 246 | eof = "\n"; |
| 247 | } |
| 248 | fputs(eof, stdout); |
| 249 | return 0; |
| 250 | } |
| 251 | |
Nicolas Pitre | 366bfcb | 2006-12-04 11:13:39 -0500 | [diff] [blame] | 252 | if (read_cache() < 0) |
| 253 | die("index file corrupt"); |
| 254 | |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 255 | if (dir.ignored_nr) { |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 256 | fprintf(stderr, ignore_error); |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 257 | for (i = 0; i < dir.ignored_nr; i++) { |
| 258 | fprintf(stderr, "%s\n", dir.ignored[i]->name); |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 259 | } |
Jeff King | e96980e | 2007-06-12 23:42:14 +0200 | [diff] [blame] | 260 | fprintf(stderr, "Use -f if you really want to add them.\n"); |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 261 | die("no files added"); |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 264 | for (i = 0; i < dir.nr; i++) |
Junio C Hamano | fd1c3bf | 2007-04-01 22:14:40 -0700 | [diff] [blame] | 265 | add_file_to_cache(dir.entries[i]->name, verbose); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 266 | |
Junio C Hamano | dfdac5d | 2007-04-20 01:39:39 -0700 | [diff] [blame] | 267 | finish: |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 268 | if (active_cache_changed) { |
| 269 | if (write_cache(newfd, active_cache, active_nr) || |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 270 | close(newfd) || commit_locked_index(&lock_file)) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 271 | die("Unable to write new index file"); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |