builtin / add.con commit Merge branch 'mm/push-default-warning' (15be621)
   1/*
   2 * "git add" builtin command
   3 *
   4 * Copyright (C) 2006 Linus Torvalds
   5 */
   6#include "cache.h"
   7#include "builtin.h"
   8#include "lockfile.h"
   9#include "dir.h"
  10#include "pathspec.h"
  11#include "exec_cmd.h"
  12#include "cache-tree.h"
  13#include "run-command.h"
  14#include "parse-options.h"
  15#include "diff.h"
  16#include "diffcore.h"
  17#include "revision.h"
  18#include "bulk-checkin.h"
  19#include "argv-array.h"
  20
  21static const char * const builtin_add_usage[] = {
  22        N_("git add [<options>] [--] <pathspec>..."),
  23        NULL
  24};
  25static int patch_interactive, add_interactive, edit_interactive;
  26static int take_worktree_changes;
  27
  28struct update_callback_data {
  29        int flags;
  30        int add_errors;
  31};
  32
  33static int fix_unmerged_status(struct diff_filepair *p,
  34                               struct update_callback_data *data)
  35{
  36        if (p->status != DIFF_STATUS_UNMERGED)
  37                return p->status;
  38        if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL) && !p->two->mode)
  39                /*
  40                 * This is not an explicit add request, and the
  41                 * path is missing from the working tree (deleted)
  42                 */
  43                return DIFF_STATUS_DELETED;
  44        else
  45                /*
  46                 * Either an explicit add request, or path exists
  47                 * in the working tree.  An attempt to explicitly
  48                 * add a path that does not exist in the working tree
  49                 * will be caught as an error by the caller immediately.
  50                 */
  51                return DIFF_STATUS_MODIFIED;
  52}
  53
  54static void update_callback(struct diff_queue_struct *q,
  55                            struct diff_options *opt, void *cbdata)
  56{
  57        int i;
  58        struct update_callback_data *data = cbdata;
  59
  60        for (i = 0; i < q->nr; i++) {
  61                struct diff_filepair *p = q->queue[i];
  62                const char *path = p->one->path;
  63                switch (fix_unmerged_status(p, data)) {
  64                default:
  65                        die(_("unexpected diff status %c"), p->status);
  66                case DIFF_STATUS_MODIFIED:
  67                case DIFF_STATUS_TYPE_CHANGED:
  68                        if (add_file_to_index(&the_index, path, data->flags)) {
  69                                if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
  70                                        die(_("updating files failed"));
  71                                data->add_errors++;
  72                        }
  73                        break;
  74                case DIFF_STATUS_DELETED:
  75                        if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
  76                                break;
  77                        if (!(data->flags & ADD_CACHE_PRETEND))
  78                                remove_file_from_index(&the_index, path);
  79                        if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
  80                                printf(_("remove '%s'\n"), path);
  81                        break;
  82                }
  83        }
  84}
  85
  86int add_files_to_cache(const char *prefix,
  87                       const struct pathspec *pathspec, int flags)
  88{
  89        struct update_callback_data data;
  90        struct rev_info rev;
  91
  92        memset(&data, 0, sizeof(data));
  93        data.flags = flags;
  94
  95        init_revisions(&rev, prefix);
  96        setup_revisions(0, NULL, &rev, NULL);
  97        if (pathspec)
  98                copy_pathspec(&rev.prune_data, pathspec);
  99        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 100        rev.diffopt.format_callback = update_callback;
 101        rev.diffopt.format_callback_data = &data;
 102        rev.max_count = 0; /* do not compare unmerged paths with stage #2 */
 103        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
 104        return !!data.add_errors;
 105}
 106
 107static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec, int prefix)
 108{
 109        char *seen;
 110        int i;
 111        struct dir_entry **src, **dst;
 112
 113        seen = xcalloc(pathspec->nr, 1);
 114
 115        src = dst = dir->entries;
 116        i = dir->nr;
 117        while (--i >= 0) {
 118                struct dir_entry *entry = *src++;
 119                if (dir_path_match(entry, pathspec, prefix, seen))
 120                        *dst++ = entry;
 121        }
 122        dir->nr = dst - dir->entries;
 123        add_pathspec_matches_against_index(pathspec, seen);
 124        return seen;
 125}
 126
 127static void refresh(int verbose, const struct pathspec *pathspec)
 128{
 129        char *seen;
 130        int i;
 131
 132        seen = xcalloc(pathspec->nr, 1);
 133        refresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,
 134                      pathspec, seen, _("Unstaged changes after refreshing the index:"));
 135        for (i = 0; i < pathspec->nr; i++) {
 136                if (!seen[i])
 137                        die(_("pathspec '%s' did not match any files"),
 138                            pathspec->items[i].match);
 139        }
 140        free(seen);
 141}
 142
 143int run_add_interactive(const char *revision, const char *patch_mode,
 144                        const struct pathspec *pathspec)
 145{
 146        int status, i;
 147        struct argv_array argv = ARGV_ARRAY_INIT;
 148
 149        argv_array_push(&argv, "add--interactive");
 150        if (patch_mode)
 151                argv_array_push(&argv, patch_mode);
 152        if (revision)
 153                argv_array_push(&argv, revision);
 154        argv_array_push(&argv, "--");
 155        for (i = 0; i < pathspec->nr; i++)
 156                /* pass original pathspec, to be re-parsed */
 157                argv_array_push(&argv, pathspec->items[i].original);
 158
 159        status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
 160        argv_array_clear(&argv);
 161        return status;
 162}
 163
 164int interactive_add(int argc, const char **argv, const char *prefix, int patch)
 165{
 166        struct pathspec pathspec;
 167
 168        parse_pathspec(&pathspec, 0,
 169                       PATHSPEC_PREFER_FULL |
 170                       PATHSPEC_SYMLINK_LEADING_PATH |
 171                       PATHSPEC_PREFIX_ORIGIN,
 172                       prefix, argv);
 173
 174        return run_add_interactive(NULL,
 175                                   patch ? "--patch" : NULL,
 176                                   &pathspec);
 177}
 178
 179static int edit_patch(int argc, const char **argv, const char *prefix)
 180{
 181        char *file = git_pathdup("ADD_EDIT.patch");
 182        const char *apply_argv[] = { "apply", "--recount", "--cached",
 183                NULL, NULL };
 184        struct child_process child = CHILD_PROCESS_INIT;
 185        struct rev_info rev;
 186        int out;
 187        struct stat st;
 188
 189        apply_argv[3] = file;
 190
 191        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 192
 193        if (read_cache() < 0)
 194                die(_("Could not read the index"));
 195
 196        init_revisions(&rev, prefix);
 197        rev.diffopt.context = 7;
 198
 199        argc = setup_revisions(argc, argv, &rev, NULL);
 200        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 201        rev.diffopt.use_color = 0;
 202        DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
 203        out = open(file, O_CREAT | O_WRONLY, 0666);
 204        if (out < 0)
 205                die(_("Could not open '%s' for writing."), file);
 206        rev.diffopt.file = xfdopen(out, "w");
 207        rev.diffopt.close_file = 1;
 208        if (run_diff_files(&rev, 0))
 209                die(_("Could not write patch"));
 210
 211        if (launch_editor(file, NULL, NULL))
 212                die(_("editing patch failed"));
 213
 214        if (stat(file, &st))
 215                die_errno(_("Could not stat '%s'"), file);
 216        if (!st.st_size)
 217                die(_("Empty patch. Aborted."));
 218
 219        child.git_cmd = 1;
 220        child.argv = apply_argv;
 221        if (run_command(&child))
 222                die(_("Could not apply '%s'"), file);
 223
 224        unlink(file);
 225        free(file);
 226        return 0;
 227}
 228
 229static struct lock_file lock_file;
 230
 231static const char ignore_error[] =
 232N_("The following paths are ignored by one of your .gitignore files:\n");
 233
 234static int verbose, show_only, ignored_too, refresh_only;
 235static int ignore_add_errors, intent_to_add, ignore_missing;
 236
 237#define ADDREMOVE_DEFAULT 1
 238static int addremove = ADDREMOVE_DEFAULT;
 239static int addremove_explicit = -1; /* unspecified */
 240
 241static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
 242{
 243        /* if we are told to ignore, we are not adding removals */
 244        *(int *)opt->value = !unset ? 0 : 1;
 245        return 0;
 246}
 247
 248static struct option builtin_add_options[] = {
 249        OPT__DRY_RUN(&show_only, N_("dry run")),
 250        OPT__VERBOSE(&verbose, N_("be verbose")),
 251        OPT_GROUP(""),
 252        OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
 253        OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
 254        OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
 255        OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files")),
 256        OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
 257        OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
 258        OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
 259        { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
 260          NULL /* takes no arguments */,
 261          N_("ignore paths removed in the working tree (same as --no-all)"),
 262          PARSE_OPT_NOARG, ignore_removal_cb },
 263        OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
 264        OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
 265        OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
 266        OPT_END(),
 267};
 268
 269static int add_config(const char *var, const char *value, void *cb)
 270{
 271        if (!strcmp(var, "add.ignoreerrors") ||
 272            !strcmp(var, "add.ignore-errors")) {
 273                ignore_add_errors = git_config_bool(var, value);
 274                return 0;
 275        }
 276        return git_default_config(var, value, cb);
 277}
 278
 279static int add_files(struct dir_struct *dir, int flags)
 280{
 281        int i, exit_status = 0;
 282
 283        if (dir->ignored_nr) {
 284                fprintf(stderr, _(ignore_error));
 285                for (i = 0; i < dir->ignored_nr; i++)
 286                        fprintf(stderr, "%s\n", dir->ignored[i]->name);
 287                fprintf(stderr, _("Use -f if you really want to add them.\n"));
 288                exit_status = 1;
 289        }
 290
 291        for (i = 0; i < dir->nr; i++)
 292                if (add_file_to_cache(dir->entries[i]->name, flags)) {
 293                        if (!ignore_add_errors)
 294                                die(_("adding files failed"));
 295                        exit_status = 1;
 296                }
 297        return exit_status;
 298}
 299
 300int cmd_add(int argc, const char **argv, const char *prefix)
 301{
 302        int exit_status = 0;
 303        struct pathspec pathspec;
 304        struct dir_struct dir;
 305        int flags;
 306        int add_new_files;
 307        int require_pathspec;
 308        char *seen = NULL;
 309
 310        git_config(add_config, NULL);
 311
 312        argc = parse_options(argc, argv, prefix, builtin_add_options,
 313                          builtin_add_usage, PARSE_OPT_KEEP_ARGV0);
 314        if (patch_interactive)
 315                add_interactive = 1;
 316        if (add_interactive)
 317                exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
 318
 319        if (edit_interactive)
 320                return(edit_patch(argc, argv, prefix));
 321        argc--;
 322        argv++;
 323
 324        if (0 <= addremove_explicit)
 325                addremove = addremove_explicit;
 326        else if (take_worktree_changes && ADDREMOVE_DEFAULT)
 327                addremove = 0; /* "-u" was given but not "-A" */
 328
 329        if (addremove && take_worktree_changes)
 330                die(_("-A and -u are mutually incompatible"));
 331
 332        if (!take_worktree_changes && addremove_explicit < 0 && argc)
 333                /* Turn "git add pathspec..." to "git add -A pathspec..." */
 334                addremove = 1;
 335
 336        if (!show_only && ignore_missing)
 337                die(_("Option --ignore-missing can only be used together with --dry-run"));
 338
 339        add_new_files = !take_worktree_changes && !refresh_only;
 340        require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
 341
 342        hold_locked_index(&lock_file, 1);
 343
 344        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
 345                 (show_only ? ADD_CACHE_PRETEND : 0) |
 346                 (intent_to_add ? ADD_CACHE_INTENT : 0) |
 347                 (ignore_add_errors ? ADD_CACHE_IGNORE_ERRORS : 0) |
 348                 (!(addremove || take_worktree_changes)
 349                  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 350
 351        if (require_pathspec && argc == 0) {
 352                fprintf(stderr, _("Nothing specified, nothing added.\n"));
 353                fprintf(stderr, _("Maybe you wanted to say 'git add .'?\n"));
 354                return 0;
 355        }
 356
 357        if (read_cache() < 0)
 358                die(_("index file corrupt"));
 359
 360        /*
 361         * Check the "pathspec '%s' did not match any files" block
 362         * below before enabling new magic.
 363         */
 364        parse_pathspec(&pathspec, 0,
 365                       PATHSPEC_PREFER_FULL |
 366                       PATHSPEC_SYMLINK_LEADING_PATH |
 367                       PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
 368                       prefix, argv);
 369
 370        if (add_new_files) {
 371                int baselen;
 372
 373                /* Set up the default git porcelain excludes */
 374                memset(&dir, 0, sizeof(dir));
 375                if (!ignored_too) {
 376                        dir.flags |= DIR_COLLECT_IGNORED;
 377                        setup_standard_excludes(&dir);
 378                }
 379
 380                /* This picks up the paths that are not tracked */
 381                baselen = fill_directory(&dir, &pathspec);
 382                if (pathspec.nr)
 383                        seen = prune_directory(&dir, &pathspec, baselen);
 384        }
 385
 386        if (refresh_only) {
 387                refresh(verbose, &pathspec);
 388                goto finish;
 389        }
 390
 391        if (pathspec.nr) {
 392                int i;
 393
 394                if (!seen)
 395                        seen = find_pathspecs_matching_against_index(&pathspec);
 396
 397                /*
 398                 * file_exists() assumes exact match
 399                 */
 400                GUARD_PATHSPEC(&pathspec,
 401                               PATHSPEC_FROMTOP |
 402                               PATHSPEC_LITERAL |
 403                               PATHSPEC_GLOB |
 404                               PATHSPEC_ICASE |
 405                               PATHSPEC_EXCLUDE);
 406
 407                for (i = 0; i < pathspec.nr; i++) {
 408                        const char *path = pathspec.items[i].match;
 409                        if (pathspec.items[i].magic & PATHSPEC_EXCLUDE)
 410                                continue;
 411                        if (!seen[i] && path[0] &&
 412                            ((pathspec.items[i].magic &
 413                              (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
 414                             !file_exists(path))) {
 415                                if (ignore_missing) {
 416                                        int dtype = DT_UNKNOWN;
 417                                        if (is_excluded(&dir, path, &dtype))
 418                                                dir_add_ignored(&dir, path, pathspec.items[i].len);
 419                                } else
 420                                        die(_("pathspec '%s' did not match any files"),
 421                                            pathspec.items[i].original);
 422                        }
 423                }
 424                free(seen);
 425        }
 426
 427        plug_bulk_checkin();
 428
 429        exit_status |= add_files_to_cache(prefix, &pathspec, flags);
 430
 431        if (add_new_files)
 432                exit_status |= add_files(&dir, flags);
 433
 434        unplug_bulk_checkin();
 435
 436finish:
 437        if (active_cache_changed) {
 438                if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
 439                        die(_("Unable to write new index file"));
 440        }
 441
 442        return exit_status;
 443}