Merge branch 'np/lookup-object-hashing'
[gitweb.git] / builtin / add.c
index 34c935826011ba5a02d66421d034e9f93a6e27f2..226f758869358444ad1c39e5e0a05c961fb76859 100644 (file)
@@ -166,14 +166,16 @@ static void update_callback(struct diff_queue_struct *q,
        }
 }
 
-static void update_files_in_cache(const char *prefix, const char **pathspec,
+static void update_files_in_cache(const char *prefix,
+                                 const struct pathspec *pathspec,
                                  struct update_callback_data *data)
 {
        struct rev_info rev;
 
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
-       init_pathspec(&rev.prune_data, pathspec);
+       if (pathspec)
+               copy_pathspec(&rev.prune_data, pathspec);
        rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
        rev.diffopt.format_callback = update_callback;
        rev.diffopt.format_callback_data = data;
@@ -181,7 +183,8 @@ static void update_files_in_cache(const char *prefix, const char **pathspec,
        run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
 }
 
-int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
+int add_files_to_cache(const char *prefix,
+                      const struct pathspec *pathspec, int flags)
 {
        struct update_callback_data data;
 
@@ -192,23 +195,21 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
 }
 
 #define WARN_IMPLICIT_DOT (1u << 0)
-static char *prune_directory(struct dir_struct *dir, const char **pathspec,
+static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
                             int prefix, unsigned flag)
 {
        char *seen;
-       int i, specs;
+       int i;
        struct dir_entry **src, **dst;
 
-       for (specs = 0; pathspec[specs];  specs++)
-               /* nothing */;
-       seen = xcalloc(specs, 1);
+       seen = xcalloc(pathspec->nr, 1);
 
        src = dst = dir->entries;
        i = dir->nr;
        while (--i >= 0) {
                struct dir_entry *entry = *src++;
-               if (match_pathspec(pathspec, entry->name, entry->len,
-                                  prefix, seen))
+               if (match_pathspec_depth(pathspec, entry->name, entry->len,
+                                        prefix, seen))
                        *dst++ = entry;
                else if (flag & WARN_IMPLICIT_DOT)
                        /*
@@ -222,7 +223,7 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,
                        warn_pathless_add();
        }
        dir->nr = dst - dir->entries;
-       add_pathspec_matches_against_index(pathspec, seen, specs);
+       add_pathspec_matches_against_index(pathspec, seen);
        return seen;
 }
 
@@ -269,13 +270,7 @@ int interactive_add(int argc, const char **argv, const char *prefix, int patch)
 {
        struct pathspec pathspec;
 
-       /*
-        * git-add--interactive itself does not parse pathspec. It
-        * simply passes the pathspec to other builtin commands. Let's
-        * hope all of them support all magic, or we'll need to limit
-        * the magic here.
-        */
-       parse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
+       parse_pathspec(&pathspec, 0,
                       PATHSPEC_PREFER_FULL |
                       PATHSPEC_SYMLINK_LEADING_PATH |
                       PATHSPEC_PREFIX_ORIGIN,
@@ -301,21 +296,22 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
        if (read_cache() < 0)
-               die (_("Could not read the index"));
+               die(_("Could not read the index"));
 
        init_revisions(&rev, prefix);
        rev.diffopt.context = 7;
 
        argc = setup_revisions(argc, argv, &rev, NULL);
        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
+       rev.diffopt.use_color = 0;
        DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
        out = open(file, O_CREAT | O_WRONLY, 0666);
        if (out < 0)
-               die (_("Could not open '%s' for writing."), file);
+               die(_("Could not open '%s' for writing."), file);
        rev.diffopt.file = xfdopen(out, "w");
        rev.diffopt.close_file = 1;
        if (run_diff_files(&rev, 0))
-               die (_("Could not write patch"));
+               die(_("Could not write patch"));
 
        launch_editor(file, NULL, NULL);
 
@@ -328,7 +324,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        child.git_cmd = 1;
        child.argv = apply_argv;
        if (run_command(&child))
-               die (_("Could not apply '%s'"), file);
+               die(_("Could not apply '%s'"), file);
 
        unlink(file);
        free(file);
@@ -520,7 +516,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                /* This picks up the paths that are not tracked */
                baselen = fill_directory(&dir, implicit_dot ? &empty_pathspec : &pathspec);
                if (pathspec.nr)
-                       seen = prune_directory(&dir, pathspec.raw, baselen,
+                       seen = prune_directory(&dir, &pathspec, baselen,
                                        implicit_dot ? WARN_IMPLICIT_DOT : 0);
        }
 
@@ -535,23 +531,30 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                int i;
 
                if (!seen)
-                       seen = find_pathspecs_matching_against_index(pathspec.raw);
+                       seen = find_pathspecs_matching_against_index(&pathspec);
 
                /*
                 * file_exists() assumes exact match
                 */
-               GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);
-
-               for (i = 0; pathspec.raw[i]; i++) {
-                       if (!seen[i] && pathspec.raw[i][0]
-                           && !file_exists(pathspec.raw[i])) {
+               GUARD_PATHSPEC(&pathspec,
+                              PATHSPEC_FROMTOP |
+                              PATHSPEC_LITERAL |
+                              PATHSPEC_GLOB |
+                              PATHSPEC_ICASE);
+
+               for (i = 0; i < pathspec.nr; i++) {
+                       const char *path = pathspec.items[i].match;
+                       if (!seen[i] &&
+                           ((pathspec.items[i].magic &
+                             (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||
+                            !file_exists(path))) {
                                if (ignore_missing) {
                                        int dtype = DT_UNKNOWN;
-                                       if (is_excluded(&dir, pathspec.raw[i], &dtype))
-                                               dir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));
+                                       if (is_excluded(&dir, path, &dtype))
+                                               dir_add_ignored(&dir, path, pathspec.items[i].len);
                                } else
                                        die(_("pathspec '%s' did not match any files"),
-                                           pathspec.raw[i]);
+                                           pathspec.items[i].original);
                        }
                }
                free(seen);
@@ -571,7 +574,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                memset(&pathspec, 0, sizeof(pathspec));
        }
        update_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;
-       update_files_in_cache(prefix, pathspec.raw, &update_data);
+       update_files_in_cache(prefix, &pathspec, &update_data);
 
        exit_status |= !!update_data.add_errors;
        if (add_new_files)
@@ -579,7 +582,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
        unplug_bulk_checkin();
 
- finish:
+finish:
        if (active_cache_changed) {
                if (write_cache(newfd, active_cache, active_nr) ||
                    commit_locked_index(&lock_file))