]> rtime.felk.cvut.cz Git - git.git/blobdiff - builtin-add.c
Documentation/config: describe status.submodulesummary
[git.git] / builtin-add.c
index cb6e5906fb76f2460f212c54b557e5ae0ce23579..87d2980313e71afc08df3eacde4a99a8468ff874 100644 (file)
@@ -11,6 +11,7 @@
 #include "run-command.h"
 #include "parse-options.h"
 #include "diff.h"
+#include "diffcore.h"
 #include "revision.h"
 
 static const char * const builtin_add_usage[] = {
@@ -20,6 +21,81 @@ static const char * const builtin_add_usage[] = {
 static int patch_interactive, add_interactive, edit_interactive;
 static int take_worktree_changes;
 
+struct update_callback_data
+{
+       int flags;
+       int add_errors;
+};
+
+static void update_callback(struct diff_queue_struct *q,
+                           struct diff_options *opt, void *cbdata)
+{
+       int i;
+       struct update_callback_data *data = cbdata;
+
+       for (i = 0; i < q->nr; i++) {
+               struct diff_filepair *p = q->queue[i];
+               const char *path = p->one->path;
+               switch (p->status) {
+               default:
+                       die("unexpected diff status %c", p->status);
+               case DIFF_STATUS_UNMERGED:
+                       /*
+                        * ADD_CACHE_IGNORE_REMOVAL is unset if "git
+                        * add -u" is calling us, In such a case, a
+                        * missing work tree file needs to be removed
+                        * if there is an unmerged entry at stage #2,
+                        * but such a diff record is followed by
+                        * another with DIFF_STATUS_DELETED (and if
+                        * there is no stage #2, we won't see DELETED
+                        * nor MODIFIED).  We can simply continue
+                        * either way.
+                        */
+                       if (!(data->flags & ADD_CACHE_IGNORE_REMOVAL))
+                               continue;
+                       /*
+                        * Otherwise, it is "git add path" is asking
+                        * to explicitly add it; we fall through.  A
+                        * missing work tree file is an error and is
+                        * caught by add_file_to_index() in such a
+                        * case.
+                        */
+               case DIFF_STATUS_MODIFIED:
+               case DIFF_STATUS_TYPE_CHANGED:
+                       if (add_file_to_index(&the_index, path, data->flags)) {
+                               if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
+                                       die("updating files failed");
+                               data->add_errors++;
+                       }
+                       break;
+               case DIFF_STATUS_DELETED:
+                       if (data->flags & ADD_CACHE_IGNORE_REMOVAL)
+                               break;
+                       if (!(data->flags & ADD_CACHE_PRETEND))
+                               remove_file_from_index(&the_index, path);
+                       if (data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))
+                               printf("remove '%s'\n", path);
+                       break;
+               }
+       }
+}
+
+int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
+{
+       struct update_callback_data data;
+       struct rev_info rev;
+       init_revisions(&rev, prefix);
+       setup_revisions(0, NULL, &rev, NULL);
+       rev.prune_data = pathspec;
+       rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
+       rev.diffopt.format_callback = update_callback;
+       data.flags = flags;
+       data.add_errors = 0;
+       rev.diffopt.format_callback_data = &data;
+       run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);
+       return !!data.add_errors;
+}
+
 static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
 {
        int num_unmatched = 0, i;
@@ -41,7 +117,19 @@ static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
        }
 }
 
-static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
+static char *find_used_pathspec(const char **pathspec)
+{
+       char *seen;
+       int i;
+
+       for (i = 0; pathspec[i];  i++)
+               ; /* just counting */
+       seen = xcalloc(i, 1);
+       fill_pathspec_matches(pathspec, seen, i);
+       return seen;
+}
+
+static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
 {
        char *seen;
        int i, specs;
@@ -61,13 +149,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
        }
        dir->nr = dst - dir->entries;
        fill_pathspec_matches(pathspec, seen, specs);
-
-       for (i = 0; i < specs; i++) {
-               if (!seen[i] && pathspec[i][0] && !file_exists(pathspec[i]))
-                       die("pathspec '%s' did not match any files",
-                                       pathspec[i]);
-       }
-        free(seen);
+       return seen;
 }
 
 static void treat_gitlinks(const char **pathspec)
@@ -283,6 +365,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        int flags;
        int add_new_files;
        int require_pathspec;
+       char *seen = NULL;
 
        git_config(add_config, NULL);
 
@@ -342,7 +425,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, pathspec);
                if (pathspec)
-                       prune_directory(&dir, pathspec, baselen);
+                       seen = prune_directory(&dir, pathspec, baselen);
        }
 
        if (refresh_only) {
@@ -350,6 +433,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                goto finish;
        }
 
+       if (pathspec) {
+               int i;
+               if (!seen)
+                       seen = find_used_pathspec(pathspec);
+               for (i = 0; pathspec[i]; i++) {
+                       if (!seen[i] && pathspec[i][0]
+                           && !file_exists(pathspec[i]))
+                               die("pathspec '%s' did not match any files",
+                                   pathspec[i]);
+               }
+               free(seen);
+       }
+
        exit_status |= add_files_to_cache(prefix, pathspec, flags);
 
        if (add_new_files)