tests: replace binary GPG keyrings with ASCII-armored keys
[gitweb.git] / builtin / add.c
index 4b045bace12c1fa951785dc09c05a46b09cd9fc7..ae6d3e262bcbb3dc57c0ddf146791380259b31be 100644 (file)
@@ -5,6 +5,7 @@
  */
 #include "cache.h"
 #include "builtin.h"
+#include "lockfile.h"
 #include "dir.h"
 #include "pathspec.h"
 #include "exec_cmd.h"
@@ -15,6 +16,7 @@
 #include "diffcore.h"
 #include "revision.h"
 #include "bulk-checkin.h"
+#include "argv-array.h"
 
 static const char * const builtin_add_usage[] = {
        N_("git add [options] [--] <pathspec>..."),
@@ -141,23 +143,21 @@ static void refresh(int verbose, const struct pathspec *pathspec)
 int run_add_interactive(const char *revision, const char *patch_mode,
                        const struct pathspec *pathspec)
 {
-       int status, ac, i;
-       const char **args;
+       int status, i;
+       struct argv_array argv = ARGV_ARRAY_INIT;
 
-       args = xcalloc(sizeof(const char *), (pathspec->nr + 6));
-       ac = 0;
-       args[ac++] = "add--interactive";
+       argv_array_push(&argv, "add--interactive");
        if (patch_mode)
-               args[ac++] = patch_mode;
+               argv_array_push(&argv, patch_mode);
        if (revision)
-               args[ac++] = revision;
-       args[ac++] = "--";
+               argv_array_push(&argv, revision);
+       argv_array_push(&argv, "--");
        for (i = 0; i < pathspec->nr; i++)
                /* pass original pathspec, to be re-parsed */
-               args[ac++] = pathspec->items[i].original;
+               argv_array_push(&argv, pathspec->items[i].original);
 
-       status = run_command_v_opt(args, RUN_GIT_CMD);
-       free(args);
+       status = run_command_v_opt(argv.argv, RUN_GIT_CMD);
+       argv_array_clear(&argv);
        return status;
 }
 
@@ -181,7 +181,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        char *file = git_pathdup("ADD_EDIT.patch");
        const char *apply_argv[] = { "apply", "--recount", "--cached",
                NULL, NULL };
-       struct child_process child;
+       struct child_process child = CHILD_PROCESS_INIT;
        struct rev_info rev;
        int out;
        struct stat st;
@@ -215,7 +215,6 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
        if (!st.st_size)
                die(_("Empty patch. Aborted."));
 
-       memset(&child, 0, sizeof(child));
        child.git_cmd = 1;
        child.argv = apply_argv;
        if (run_command(&child))
@@ -300,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)
 int cmd_add(int argc, const char **argv, const char *prefix)
 {
        int exit_status = 0;
-       int newfd;
        struct pathspec pathspec;
        struct dir_struct dir;
        int flags;
@@ -346,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        add_new_files = !take_worktree_changes && !refresh_only;
        require_pathspec = !take_worktree_changes;
 
-       newfd = hold_locked_index(&lock_file, 1);
+       hold_locked_index(&lock_file, 1);
 
        flags = ((verbose ? ADD_CACHE_VERBOSE : 0) |
                 (show_only ? ADD_CACHE_PRETEND : 0) |
@@ -444,8 +442,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 
 finish:
        if (active_cache_changed) {
-               if (write_cache(newfd, active_cache, active_nr) ||
-                   commit_locked_index(&lock_file))
+               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
                        die(_("Unable to write new index file"));
        }