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 | */ |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 6 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 7 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 9 | #include "builtin.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 10 | #include "lockfile.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 11 | #include "dir.h" |
Adam Spiers | 6f525e7 | 2013-01-06 16:58:08 +0000 | [diff] [blame] | 12 | #include "pathspec.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 13 | #include "exec-cmd.h" |
Junio C Hamano | 93872e0 | 2006-05-20 01:28:49 -0700 | [diff] [blame] | 14 | #include "cache-tree.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" |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 17 | #include "diff.h" |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 18 | #include "diffcore.h" |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 19 | #include "revision.h" |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 20 | #include "bulk-checkin.h" |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 21 | #include "argv-array.h" |
Brandon Williams | bdab972 | 2017-05-09 12:17:59 -0700 | [diff] [blame] | 22 | #include "submodule.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 23 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 24 | static const char * const builtin_add_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 25 | N_("git add [<options>] [--] <pathspec>..."), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 26 | NULL |
| 27 | }; |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 28 | static int patch_interactive, add_interactive, edit_interactive; |
Jeff King | 93c44d4 | 2007-05-12 02:42:00 -0400 | [diff] [blame] | 29 | static int take_worktree_changes; |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 30 | static int add_renormalize; |
James Bowes | 896bdfa | 2007-02-27 22:31:10 -0500 | [diff] [blame] | 31 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 32 | struct update_callback_data { |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 33 | int flags; |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 34 | int add_errors; |
| 35 | }; |
| 36 | |
Ramsay Jones | 1e22a99 | 2017-08-09 01:51:23 +0100 | [diff] [blame] | 37 | static void chmod_pathspec(struct pathspec *pathspec, char flip) |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
| 41 | for (i = 0; i < active_nr; i++) { |
| 42 | struct cache_entry *ce = active_cache[i]; |
| 43 | |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 44 | if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL)) |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 45 | continue; |
| 46 | |
Ramsay Jones | 1e22a99 | 2017-08-09 01:51:23 +0100 | [diff] [blame] | 47 | if (chmod_cache_entry(ce, flip) < 0) |
| 48 | fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name); |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 52 | static int fix_unmerged_status(struct diff_filepair *p, |
| 53 | struct update_callback_data *data) |
| 54 | { |
| 55 | if (p->status != DIFF_STATUS_UNMERGED) |
| 56 | return p->status; |
| 57 | if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode) |
| 58 | /* |
| 59 | * This is not an explicit add request, and the |
| 60 | * path is missing from the working tree (deleted) |
| 61 | */ |
| 62 | return DIFF_STATUS_DELETED; |
| 63 | else |
| 64 | /* |
| 65 | * Either an explicit add request, or path exists |
| 66 | * in the working tree. An attempt to explicitly |
| 67 | * add a path that does not exist in the working tree |
| 68 | * will be caught as an error by the caller immediately. |
| 69 | */ |
| 70 | return DIFF_STATUS_MODIFIED; |
| 71 | } |
| 72 | |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 73 | static void update_callback(struct diff_queue_struct *q, |
| 74 | struct diff_options *opt, void *cbdata) |
| 75 | { |
| 76 | int i; |
| 77 | struct update_callback_data *data = cbdata; |
| 78 | |
| 79 | for (i = 0; i < q->nr; i++) { |
| 80 | struct diff_filepair *p = q->queue[i]; |
| 81 | const char *path = p->one->path; |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 82 | switch (fix_unmerged_status(p, data)) { |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 83 | default: |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 84 | die(_("unexpected diff status %c"), p->status); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 85 | case DIFF_STATUS_MODIFIED: |
| 86 | case DIFF_STATUS_TYPE_CHANGED: |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 87 | if (add_file_to_index(&the_index, path, data->flags)) { |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 88 | if (!(data->flags & ADD_CACHE_IGNORE_ERRORS)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 89 | die(_("updating files failed")); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 90 | data->add_errors++; |
| 91 | } |
| 92 | break; |
| 93 | case DIFF_STATUS_DELETED: |
| 94 | if (data->flags & ADD_CACHE_IGNORE_REMOVAL) |
| 95 | break; |
| 96 | if (!(data->flags & ADD_CACHE_PRETEND)) |
| 97 | remove_file_from_index(&the_index, path); |
| 98 | if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE)) |
Ævar Arnfjörð Bjarmason | 475c73e | 2011-02-22 23:41:32 +0000 | [diff] [blame] | 99 | printf(_("remove '%s'\n"), path); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 105 | int add_files_to_cache(const char *prefix, |
| 106 | const struct pathspec *pathspec, int flags) |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 107 | { |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 108 | struct update_callback_data data; |
Jonathan Nieder | 71c7b05 | 2013-03-19 15:50:50 -0700 | [diff] [blame] | 109 | struct rev_info rev; |
| 110 | |
Junio C Hamano | d226b14 | 2013-04-17 12:32:21 -0700 | [diff] [blame] | 111 | memset(&data, 0, sizeof(data)); |
| 112 | data.flags = flags; |
Junio C Hamano | 160c4b1 | 2014-03-07 15:14:47 -0800 | [diff] [blame] | 113 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 114 | repo_init_revisions(the_repository, &rev, prefix); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 115 | setup_revisions(0, NULL, &rev, NULL); |
Nguyễn Thái Ngọc Duy | 3efe8e4 | 2013-07-14 15:35:56 +0700 | [diff] [blame] | 116 | if (pathspec) |
| 117 | copy_pathspec(&rev.prune_data, pathspec); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 118 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; |
| 119 | rev.diffopt.format_callback = update_callback; |
Junio C Hamano | 160c4b1 | 2014-03-07 15:14:47 -0800 | [diff] [blame] | 120 | rev.diffopt.format_callback_data = &data; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 121 | rev.diffopt.flags.override_submodule_config = 1; |
Junio C Hamano | 75973b2 | 2011-04-20 18:11:19 -0700 | [diff] [blame] | 122 | rev.max_count = 0; /* do not compare unmerged paths with stage #2 */ |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 123 | run_diff_files(&rev, DIFF_RACY_IS_MODIFIED); |
Jeff King | fe6a01a | 2017-09-05 09:04:10 -0400 | [diff] [blame] | 124 | clear_pathspec(&rev.prune_data); |
Linus Torvalds | fb7d3f3 | 2010-01-21 11:37:38 -0800 | [diff] [blame] | 125 | return !!data.add_errors; |
| 126 | } |
| 127 | |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 128 | static int renormalize_tracked_files(const struct pathspec *pathspec, int flags) |
| 129 | { |
| 130 | int i, retval = 0; |
| 131 | |
| 132 | for (i = 0; i < active_nr; i++) { |
| 133 | struct cache_entry *ce = active_cache[i]; |
| 134 | |
| 135 | if (ce_stage(ce)) |
| 136 | continue; /* do not touch unmerged paths */ |
| 137 | if (!S_ISREG(ce->ce_mode) && !S_ISLNK(ce->ce_mode)) |
| 138 | continue; /* do not touch non blobs */ |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 139 | if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL)) |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 140 | continue; |
Jeff King | 9e5da3d | 2019-01-17 11:27:11 -0500 | [diff] [blame] | 141 | retval |= add_file_to_cache(ce->name, flags | ADD_CACHE_RENORMALIZE); |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return retval; |
| 145 | } |
| 146 | |
Junio C Hamano | 053a6b1 | 2014-03-07 15:14:01 -0800 | [diff] [blame] | 147 | static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 148 | { |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 149 | char *seen; |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 150 | int i; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 151 | struct dir_entry **src, **dst; |
| 152 | |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 153 | seen = xcalloc(pathspec->nr, 1); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 154 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 155 | src = dst = dir->entries; |
| 156 | i = dir->nr; |
| 157 | while (--i >= 0) { |
| 158 | struct dir_entry *entry = *src++; |
Nguyễn Thái Ngọc Duy | 6d2df28 | 2018-08-13 18:14:22 +0200 | [diff] [blame] | 159 | if (dir_path_match(&the_index, entry, pathspec, prefix, seen)) |
Junio C Hamano | 4d06f8a | 2006-12-29 11:01:31 -0800 | [diff] [blame] | 160 | *dst++ = entry; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 161 | } |
| 162 | dir->nr = dst - dir->entries; |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 163 | add_pathspec_matches_against_index(pathspec, &the_index, seen); |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 164 | return seen; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 167 | static void refresh(int verbose, const struct pathspec *pathspec) |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 168 | { |
| 169 | char *seen; |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 170 | int i; |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 171 | |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 172 | seen = xcalloc(pathspec->nr, 1); |
Matthieu Moy | 43673fd | 2009-08-21 10:57:58 +0200 | [diff] [blame] | 173 | refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET, |
Ævar Arnfjörð Bjarmason | ed2a808 | 2011-02-22 23:41:33 +0000 | [diff] [blame] | 174 | pathspec, seen, _("Unstaged changes after refreshing the index:")); |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 175 | for (i = 0; i < pathspec->nr; i++) { |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 176 | if (!seen[i]) |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 177 | die(_("pathspec '%s' did not match any files"), |
| 178 | pathspec->items[i].match); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 179 | } |
Nguyễn Thái Ngọc Duy | ec36c42 | 2018-12-06 16:42:06 +0100 | [diff] [blame] | 180 | free(seen); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 183 | int run_add_interactive(const char *revision, const char *patch_mode, |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 184 | const struct pathspec *pathspec) |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 185 | { |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 186 | int status, i; |
| 187 | struct argv_array argv = ARGV_ARRAY_INIT; |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 188 | |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 189 | argv_array_push(&argv, "add--interactive"); |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 190 | if (patch_mode) |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 191 | argv_array_push(&argv, patch_mode); |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 192 | if (revision) |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 193 | argv_array_push(&argv, revision); |
| 194 | argv_array_push(&argv, "--"); |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 195 | for (i = 0; i < pathspec->nr; i++) |
| 196 | /* pass original pathspec, to be re-parsed */ |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 197 | argv_array_push(&argv, pathspec->items[i].original); |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 198 | |
Fabian Ruch | c45a18e | 2014-03-15 12:14:40 +0100 | [diff] [blame] | 199 | status = run_command_v_opt(argv.argv, RUN_GIT_CMD); |
| 200 | argv_array_clear(&argv); |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 201 | return status; |
| 202 | } |
| 203 | |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 204 | int interactive_add(int argc, const char **argv, const char *prefix, int patch) |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 205 | { |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 206 | struct pathspec pathspec; |
Junio C Hamano | 324ccbd | 2007-11-25 10:07:55 -0800 | [diff] [blame] | 207 | |
Nguyễn Thái Ngọc Duy | 625c330 | 2013-09-05 10:40:39 +0700 | [diff] [blame] | 208 | parse_pathspec(&pathspec, 0, |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 209 | PATHSPEC_PREFER_FULL | |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 210 | PATHSPEC_SYMLINK_LEADING_PATH | |
| 211 | PATHSPEC_PREFIX_ORIGIN, |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 212 | prefix, argv); |
Junio C Hamano | 3f06188 | 2007-11-25 10:10:10 -0800 | [diff] [blame] | 213 | |
Thomas Rast | 46b5139 | 2009-08-13 14:29:41 +0200 | [diff] [blame] | 214 | return run_add_interactive(NULL, |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 215 | patch ? "--patch" : NULL, |
Nguyễn Thái Ngọc Duy | 480ca64 | 2013-07-14 15:35:50 +0700 | [diff] [blame] | 216 | &pathspec); |
Kristian Høgsberg | 5868016 | 2007-09-17 20:06:44 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 219 | static int edit_patch(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 220 | { |
Ramsay Jones | d292bfa | 2012-09-04 18:30:21 +0100 | [diff] [blame] | 221 | char *file = git_pathdup("ADD_EDIT.patch"); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 222 | const char *apply_argv[] = { "apply", "--recount", "--cached", |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 223 | NULL, NULL }; |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 224 | struct child_process child = CHILD_PROCESS_INIT; |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 225 | struct rev_info rev; |
| 226 | int out; |
| 227 | struct stat st; |
| 228 | |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 229 | apply_argv[3] = file; |
| 230 | |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 231 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
| 232 | |
| 233 | if (read_cache() < 0) |
Felipe Contreras | d521abf | 2013-08-30 16:56:49 -0500 | [diff] [blame] | 234 | die(_("Could not read the index")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 235 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 236 | repo_init_revisions(the_repository, &rev, prefix); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 237 | rev.diffopt.context = 7; |
| 238 | |
| 239 | argc = setup_revisions(argc, argv, &rev, NULL); |
| 240 | rev.diffopt.output_format = DIFF_FORMAT_PATCH; |
Andrew Wong | 7f3b8c6 | 2013-07-18 18:58:04 -0400 | [diff] [blame] | 241 | rev.diffopt.use_color = 0; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 242 | rev.diffopt.flags.ignore_dirty_submodules = 1; |
Johannes Schindelin | fa6f225 | 2019-01-15 07:42:52 -0800 | [diff] [blame] | 243 | out = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0666); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 244 | if (out < 0) |
Felipe Contreras | d521abf | 2013-08-30 16:56:49 -0500 | [diff] [blame] | 245 | die(_("Could not open '%s' for writing."), file); |
Jim Meyering | 4169837 | 2009-09-12 10:43:27 +0200 | [diff] [blame] | 246 | rev.diffopt.file = xfdopen(out, "w"); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 247 | rev.diffopt.close_file = 1; |
| 248 | if (run_diff_files(&rev, 0)) |
Felipe Contreras | d521abf | 2013-08-30 16:56:49 -0500 | [diff] [blame] | 249 | die(_("Could not write patch")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 250 | |
Jeff King | cb64800 | 2015-05-12 21:21:58 -0400 | [diff] [blame] | 251 | if (launch_editor(file, NULL, NULL)) |
| 252 | die(_("editing patch failed")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 253 | |
| 254 | if (stat(file, &st)) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 255 | die_errno(_("Could not stat '%s'"), file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 256 | if (!st.st_size) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 257 | die(_("Empty patch. Aborted.")); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 258 | |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 259 | child.git_cmd = 1; |
| 260 | child.argv = apply_argv; |
| 261 | if (run_command(&child)) |
Felipe Contreras | d521abf | 2013-08-30 16:56:49 -0500 | [diff] [blame] | 262 | die(_("Could not apply '%s'"), file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 263 | |
| 264 | unlink(file); |
Ramsay Jones | d292bfa | 2012-09-04 18:30:21 +0100 | [diff] [blame] | 265 | free(file); |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Petr Baudis | b39c53e | 2007-08-29 00:41:23 +0200 | [diff] [blame] | 269 | static const char ignore_error[] = |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 270 | N_("The following paths are ignored by one of your .gitignore files:\n"); |
Junio C Hamano | 6a1ad32 | 2006-12-25 17:46:38 -0800 | [diff] [blame] | 271 | |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 272 | static int verbose, show_only, ignored_too, refresh_only; |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 273 | static int ignore_add_errors, intent_to_add, ignore_missing; |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 274 | static int warn_on_embedded_repo = 1; |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 275 | |
Junio C Hamano | fdc97ab | 2013-04-22 14:30:22 -0700 | [diff] [blame] | 276 | #define ADDREMOVE_DEFAULT 1 |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 277 | static int addremove = ADDREMOVE_DEFAULT; |
| 278 | static int addremove_explicit = -1; /* unspecified */ |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 279 | |
Edward Thomson | 4e55ed3 | 2016-05-31 17:08:18 -0500 | [diff] [blame] | 280 | static char *chmod_arg; |
| 281 | |
Junio C Hamano | 9f60f49 | 2013-04-22 13:29:20 -0700 | [diff] [blame] | 282 | static int ignore_removal_cb(const struct option *opt, const char *arg, int unset) |
| 283 | { |
| 284 | /* if we are told to ignore, we are not adding removals */ |
| 285 | *(int *)opt->value = !unset ? 0 : 1; |
| 286 | return 0; |
| 287 | } |
| 288 | |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 289 | static struct option builtin_add_options[] = { |
Nguyễn Thái Ngọc Duy | 1b56024 | 2012-08-20 19:31:52 +0700 | [diff] [blame] | 290 | OPT__DRY_RUN(&show_only, N_("dry run")), |
| 291 | OPT__VERBOSE(&verbose, N_("be verbose")), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 292 | OPT_GROUP(""), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 293 | OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")), |
| 294 | OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")), |
| 295 | OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")), |
Nguyễn Thái Ngọc Duy | 1224781 | 2018-02-09 18:01:42 +0700 | [diff] [blame] | 296 | OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 297 | OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")), |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 298 | OPT_BOOL(0, "renormalize", &add_renormalize, N_("renormalize EOL of tracked files (implies -u)")), |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 299 | OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")), |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 300 | OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")), |
Junio C Hamano | 9f60f49 | 2013-04-22 13:29:20 -0700 | [diff] [blame] | 301 | { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit, |
| 302 | NULL /* takes no arguments */, |
| 303 | N_("ignore paths removed in the working tree (same as --no-all)"), |
| 304 | PARSE_OPT_NOARG, ignore_removal_cb }, |
Junio C Hamano | 300c0a2 | 2013-03-08 22:21:13 -0800 | [diff] [blame] | 305 | OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")), |
| 306 | OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")), |
| 307 | OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")), |
René Scharfe | 5f0df44 | 2018-08-02 21:18:14 +0200 | [diff] [blame] | 308 | OPT_STRING(0, "chmod", &chmod_arg, "(+|-)x", |
| 309 | N_("override the executable bit of the listed files")), |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 310 | OPT_HIDDEN_BOOL(0, "warn-embedded-repo", &warn_on_embedded_repo, |
| 311 | N_("warn when adding an embedded repository")), |
Kristian Høgsberg | 5c46f75 | 2007-10-03 17:45:02 -0400 | [diff] [blame] | 312 | OPT_END(), |
| 313 | }; |
| 314 | |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 315 | static int add_config(const char *var, const char *value, void *cb) |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 316 | { |
Jonathan Nieder | 8c2be75 | 2011-05-14 15:19:21 -0500 | [diff] [blame] | 317 | if (!strcmp(var, "add.ignoreerrors") || |
| 318 | !strcmp(var, "add.ignore-errors")) { |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 319 | ignore_add_errors = git_config_bool(var, value); |
| 320 | return 0; |
| 321 | } |
Junio C Hamano | 9bd81e4 | 2008-05-25 14:25:02 -0700 | [diff] [blame] | 322 | return git_default_config(var, value, cb); |
Alex Riesen | dad25e4 | 2008-05-12 19:59:23 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 325 | static const char embedded_advice[] = N_( |
| 326 | "You've added another git repository inside your current repository.\n" |
| 327 | "Clones of the outer repository will not contain the contents of\n" |
| 328 | "the embedded repository and will not know how to obtain it.\n" |
| 329 | "If you meant to add a submodule, use:\n" |
| 330 | "\n" |
| 331 | " git submodule add <url> %s\n" |
| 332 | "\n" |
| 333 | "If you added this path by mistake, you can remove it from the\n" |
| 334 | "index with:\n" |
| 335 | "\n" |
| 336 | " git rm --cached %s\n" |
| 337 | "\n" |
| 338 | "See \"git help submodule\" for more information." |
| 339 | ); |
| 340 | |
| 341 | static void check_embedded_repo(const char *path) |
| 342 | { |
| 343 | struct strbuf name = STRBUF_INIT; |
| 344 | |
| 345 | if (!warn_on_embedded_repo) |
| 346 | return; |
| 347 | if (!ends_with(path, "/")) |
| 348 | return; |
| 349 | |
| 350 | /* Drop trailing slash for aesthetics */ |
| 351 | strbuf_addstr(&name, path); |
| 352 | strbuf_strip_suffix(&name, "/"); |
| 353 | |
| 354 | warning(_("adding embedded git repository: %s"), name.buf); |
| 355 | if (advice_add_embedded_repo) { |
| 356 | advise(embedded_advice, name.buf, name.buf); |
| 357 | /* there may be multiple entries; advise only once */ |
| 358 | advice_add_embedded_repo = 0; |
| 359 | } |
| 360 | |
| 361 | strbuf_release(&name); |
| 362 | } |
| 363 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 364 | static int add_files(struct dir_struct *dir, int flags) |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 365 | { |
| 366 | int i, exit_status = 0; |
| 367 | |
| 368 | if (dir->ignored_nr) { |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 369 | fprintf(stderr, _(ignore_error)); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 370 | for (i = 0; i < dir->ignored_nr; i++) |
| 371 | fprintf(stderr, "%s\n", dir->ignored[i]->name); |
Ævar Arnfjörð Bjarmason | 439fb82 | 2011-02-22 23:41:30 +0000 | [diff] [blame] | 372 | fprintf(stderr, _("Use -f if you really want to add them.\n")); |
Michael J Gruber | 1d31e5a | 2014-11-21 17:08:19 +0100 | [diff] [blame] | 373 | exit_status = 1; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 376 | for (i = 0; i < dir->nr; i++) { |
| 377 | check_embedded_repo(dir->entries[i]->name); |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 378 | if (add_file_to_index(&the_index, dir->entries[i]->name, flags)) { |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 379 | if (!ignore_add_errors) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 380 | die(_("adding files failed")); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 381 | exit_status = 1; |
| 382 | } |
Jeff King | 5321399 | 2017-06-14 06:58:22 -0400 | [diff] [blame] | 383 | } |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 384 | return exit_status; |
| 385 | } |
| 386 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 387 | int cmd_add(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 388 | { |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 389 | int exit_status = 0; |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 390 | struct pathspec pathspec; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 391 | struct dir_struct dir; |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 392 | int flags; |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 393 | int add_new_files; |
| 394 | int require_pathspec; |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 395 | char *seen = NULL; |
Martin Ågren | 0fa5a2e | 2018-05-09 22:55:39 +0200 | [diff] [blame] | 396 | struct lock_file lock_file = LOCK_INIT; |
Junio C Hamano | 5cde71d | 2006-12-10 20:55:50 -0800 | [diff] [blame] | 397 | |
Stephen Boyd | ed342fd | 2009-06-18 02:17:54 -0700 | [diff] [blame] | 398 | git_config(add_config, NULL); |
| 399 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 400 | argc = parse_options(argc, argv, prefix, builtin_add_options, |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 401 | builtin_add_usage, PARSE_OPT_KEEP_ARGV0); |
Wincent Colaiuta | b63e995 | 2007-11-25 14:15:42 +0100 | [diff] [blame] | 402 | if (patch_interactive) |
| 403 | add_interactive = 1; |
Wincent Colaiuta | 7c0ab44 | 2007-11-22 01:02:52 +0100 | [diff] [blame] | 404 | if (add_interactive) |
Conrad Irwin | b4bd466 | 2011-05-07 10:58:07 -0700 | [diff] [blame] | 405 | exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive)); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 406 | |
Johannes Schindelin | c59cb03 | 2009-04-08 23:30:24 +0200 | [diff] [blame] | 407 | if (edit_interactive) |
| 408 | return(edit_patch(argc, argv, prefix)); |
| 409 | argc--; |
| 410 | argv++; |
| 411 | |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 412 | if (0 <= addremove_explicit) |
| 413 | addremove = addremove_explicit; |
| 414 | else if (take_worktree_changes && ADDREMOVE_DEFAULT) |
| 415 | addremove = 0; /* "-u" was given but not "-A" */ |
| 416 | |
Junio C Hamano | 3ba1f11 | 2008-07-19 19:51:11 -0700 | [diff] [blame] | 417 | if (addremove && take_worktree_changes) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 418 | die(_("-A and -u are mutually incompatible")); |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 419 | |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 420 | if (!take_worktree_changes && addremove_explicit < 0 && argc) |
Junio C Hamano | fdc97ab | 2013-04-22 14:30:22 -0700 | [diff] [blame] | 421 | /* Turn "git add pathspec..." to "git add -A pathspec..." */ |
| 422 | addremove = 1; |
Junio C Hamano | 45c45e3 | 2011-04-19 12:18:20 -0700 | [diff] [blame] | 423 | |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 424 | if (!show_only && ignore_missing) |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 425 | die(_("Option --ignore-missing can only be used together with --dry-run")); |
Junio C Hamano | 808d3d7 | 2013-03-19 15:53:17 -0700 | [diff] [blame] | 426 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 427 | if (chmod_arg && ((chmod_arg[0] != '-' && chmod_arg[0] != '+') || |
| 428 | chmod_arg[1] != 'x' || chmod_arg[2])) |
Edward Thomson | 4e55ed3 | 2016-05-31 17:08:18 -0500 | [diff] [blame] | 429 | die(_("--chmod param '%s' must be either -x or +x"), chmod_arg); |
| 430 | |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 431 | add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize; |
Junio C Hamano | 29abb33 | 2015-10-24 19:31:11 -0700 | [diff] [blame] | 432 | require_pathspec = !(take_worktree_changes || (0 < addremove_explicit)); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 433 | |
Junio C Hamano | b3e83cc | 2016-12-07 10:33:54 -0800 | [diff] [blame] | 434 | hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 435 | |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 436 | flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | |
Junio C Hamano | 0166592 | 2008-05-25 14:03:50 -0700 | [diff] [blame] | 437 | (show_only ? ADD_CACHE_PRETEND : 0) | |
Junio C Hamano | 3942581 | 2008-08-21 01:44:53 -0700 | [diff] [blame] | 438 | (intent_to_add ? ADD_CACHE_INTENT : 0) | |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 439 | (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) | |
| 440 | (!(addremove || take_worktree_changes) |
Junio C Hamano | 808d3d7 | 2013-03-19 15:53:17 -0700 | [diff] [blame] | 441 | ? ADD_CACHE_IGNORE_REMOVAL : 0)); |
Gustaf Hendeby | 205ffa9 | 2008-05-22 23:59:42 +0200 | [diff] [blame] | 442 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 443 | if (require_pathspec && argc == 0) { |
Ævar Arnfjörð Bjarmason | 990ac4b | 2011-02-22 23:41:29 +0000 | [diff] [blame] | 444 | fprintf(stderr, _("Nothing specified, nothing added.\n")); |
| 445 | fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n")); |
Junio C Hamano | 93b0d86 | 2006-12-20 13:06:46 -0800 | [diff] [blame] | 446 | return 0; |
| 447 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 448 | |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 449 | /* |
| 450 | * Check the "pathspec '%s' did not match any files" block |
| 451 | * below before enabling new magic. |
| 452 | */ |
Nguyễn Thái Ngọc Duy | 84d938b | 2018-09-18 19:31:59 +0200 | [diff] [blame] | 453 | parse_pathspec(&pathspec, PATHSPEC_ATTR, |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 454 | PATHSPEC_PREFER_FULL | |
Brandon Williams | c08397e | 2017-05-11 15:04:24 -0700 | [diff] [blame] | 455 | PATHSPEC_SYMLINK_LEADING_PATH, |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 456 | prefix, argv); |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 457 | |
Ben Peart | d1664e7 | 2018-11-02 09:30:50 -0400 | [diff] [blame] | 458 | if (read_cache_preload(&pathspec) < 0) |
| 459 | die(_("index file corrupt")); |
| 460 | |
| 461 | die_in_unpopulated_submodule(&the_index, prefix); |
Brandon Williams | c08397e | 2017-05-11 15:04:24 -0700 | [diff] [blame] | 462 | die_path_inside_submodule(&the_index, &pathspec); |
| 463 | |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 464 | if (add_new_files) { |
| 465 | int baselen; |
| 466 | |
| 467 | /* Set up the default git porcelain excludes */ |
| 468 | memset(&dir, 0, sizeof(dir)); |
| 469 | if (!ignored_too) { |
| 470 | dir.flags |= DIR_COLLECT_IGNORED; |
| 471 | setup_standard_excludes(&dir); |
| 472 | } |
| 473 | |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 474 | /* This picks up the paths that are not tracked */ |
Brandon Williams | 0d32c18 | 2017-05-05 12:53:34 -0700 | [diff] [blame] | 475 | baselen = fill_directory(&dir, &the_index, &pathspec); |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 476 | if (pathspec.nr) |
Junio C Hamano | 053a6b1 | 2014-03-07 15:14:01 -0800 | [diff] [blame] | 477 | seen = prune_directory(&dir, &pathspec, baselen); |
Linus Torvalds | 1d8842d | 2009-05-14 13:22:36 -0700 | [diff] [blame] | 478 | } |
Junio C Hamano | 1e5f764 | 2008-07-22 22:30:40 -0700 | [diff] [blame] | 479 | |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 480 | if (refresh_only) { |
Nguyễn Thái Ngọc Duy | 9b2d614 | 2013-07-14 15:35:54 +0700 | [diff] [blame] | 481 | refresh(verbose, &pathspec); |
Alexandre Julliard | d616813 | 2007-08-11 23:59:01 +0200 | [diff] [blame] | 482 | goto finish; |
| 483 | } |
| 484 | |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 485 | if (pathspec.nr) { |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 486 | int i; |
Junio C Hamano | eb69934 | 2012-06-05 21:44:22 -0700 | [diff] [blame] | 487 | |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 488 | if (!seen) |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 489 | seen = find_pathspecs_matching_against_index(&pathspec, &the_index); |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * file_exists() assumes exact match |
| 493 | */ |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 494 | GUARD_PATHSPEC(&pathspec, |
| 495 | PATHSPEC_FROMTOP | |
| 496 | PATHSPEC_LITERAL | |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 497 | PATHSPEC_GLOB | |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 498 | PATHSPEC_ICASE | |
| 499 | PATHSPEC_EXCLUDE); |
Nguyễn Thái Ngọc Duy | 5a76aff | 2013-07-14 15:35:46 +0700 | [diff] [blame] | 500 | |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 501 | for (i = 0; i < pathspec.nr; i++) { |
| 502 | const char *path = pathspec.items[i].match; |
Nguyễn Thái Ngọc Duy | ef79b1f | 2013-12-06 14:30:48 +0700 | [diff] [blame] | 503 | if (pathspec.items[i].magic & PATHSPEC_EXCLUDE) |
| 504 | continue; |
Nguyễn Thái Ngọc Duy | 64ed07c | 2013-12-23 16:02:41 +0700 | [diff] [blame] | 505 | if (!seen[i] && path[0] && |
Nguyễn Thái Ngọc Duy | 93d9353 | 2013-07-14 15:36:09 +0700 | [diff] [blame] | 506 | ((pathspec.items[i].magic & |
| 507 | (PATHSPEC_GLOB | PATHSPEC_ICASE)) || |
Nguyễn Thái Ngọc Duy | bd30c2e | 2013-07-14 15:36:08 +0700 | [diff] [blame] | 508 | !file_exists(path))) { |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 509 | if (ignore_missing) { |
Nguyễn Thái Ngọc Duy | 0188f6b | 2010-11-11 20:03:22 +0700 | [diff] [blame] | 510 | int dtype = DT_UNKNOWN; |
Brandon Williams | a0bba65 | 2017-05-05 12:53:30 -0700 | [diff] [blame] | 511 | if (is_excluded(&dir, &the_index, path, &dtype)) |
Brandon Williams | 9e58bec | 2017-05-05 12:53:25 -0700 | [diff] [blame] | 512 | dir_add_ignored(&dir, &the_index, |
| 513 | path, pathspec.items[i].len); |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 514 | } else |
Ævar Arnfjörð Bjarmason | 4816885 | 2011-02-22 23:41:31 +0000 | [diff] [blame] | 515 | die(_("pathspec '%s' did not match any files"), |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 516 | pathspec.items[i].original); |
Jens Lehmann | 108da0d | 2010-07-10 00:18:38 +0200 | [diff] [blame] | 517 | } |
Junio C Hamano | 81f45e7 | 2010-02-09 17:30:49 -0500 | [diff] [blame] | 518 | } |
| 519 | free(seen); |
| 520 | } |
| 521 | |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 522 | plug_bulk_checkin(); |
| 523 | |
Torsten Bögershausen | 9472935 | 2017-11-16 17:38:28 +0100 | [diff] [blame] | 524 | if (add_renormalize) |
| 525 | exit_status |= renormalize_tracked_files(&pathspec, flags); |
| 526 | else |
| 527 | exit_status |= add_files_to_cache(prefix, &pathspec, flags); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 528 | |
Junio C Hamano | c972ec0 | 2008-07-19 19:22:25 -0700 | [diff] [blame] | 529 | if (add_new_files) |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 530 | exit_status |= add_files(&dir, flags); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 531 | |
Thomas Gummerer | 610d55a | 2016-09-14 22:07:47 +0100 | [diff] [blame] | 532 | if (chmod_arg && pathspec.nr) |
| 533 | chmod_pathspec(&pathspec, chmod_arg[0]); |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 534 | unplug_bulk_checkin(); |
| 535 | |
Felipe Contreras | d521abf | 2013-08-30 16:56:49 -0500 | [diff] [blame] | 536 | finish: |
Martin Ågren | 6100081 | 2018-03-01 21:40:20 +0100 | [diff] [blame] | 537 | if (write_locked_index(&the_index, &lock_file, |
| 538 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) |
| 539 | die(_("Unable to write new index file")); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 540 | |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 541 | UNLEAK(pathspec); |
| 542 | UNLEAK(dir); |
Alex Riesen | 7ae02a3 | 2008-05-12 19:58:10 +0200 | [diff] [blame] | 543 | return exit_status; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 544 | } |