From: Matthias Aßhauer <[email protected]>

This patch implements a new "git stash--helper" builtin plumbing
command that will be used to migrate "git-stash.sh" to C.

We start by implementing only the "--non-patch" option that will
handle the core part of the non-patch stashing.

Signed-off-by: Matthias Aßhauer <[email protected]>
---
 Makefile                |  2 ++
 builtin.h               |  1 +
 builtin/stash--helper.c | 13 ++++++++++
 git.c                   |  1 +
 stash.c                 | 65 +++++++++++++++++++++++++++++++++++++++++++++++++
 stash.h                 | 11 +++++++++
 6 files changed, 93 insertions(+)
 create mode 100644 builtin/stash--helper.c
 create mode 100644 stash.c
 create mode 100644 stash.h

diff --git a/Makefile b/Makefile
index fc2f1ab..88c07ea 100644
--- a/Makefile
+++ b/Makefile
@@ -792,6 +792,7 @@ LIB_OBJS += shallow.o
 LIB_OBJS += sideband.o
 LIB_OBJS += sigchain.o
 LIB_OBJS += split-index.o
+LIB_OBJS += stash.o
 LIB_OBJS += strbuf.o
 LIB_OBJS += streaming.o
 LIB_OBJS += string-list.o
@@ -913,6 +914,7 @@ BUILTIN_OBJS += builtin/send-pack.o
 BUILTIN_OBJS += builtin/shortlog.o
 BUILTIN_OBJS += builtin/show-branch.o
 BUILTIN_OBJS += builtin/show-ref.o
+BUILTIN_OBJS += builtin/stash--helper.o
 BUILTIN_OBJS += builtin/stripspace.o
 BUILTIN_OBJS += builtin/submodule--helper.o
 BUILTIN_OBJS += builtin/symbolic-ref.o
diff --git a/builtin.h b/builtin.h
index 6b95006..f1c8b39 100644
--- a/builtin.h
+++ b/builtin.h
@@ -118,6 +118,7 @@ extern int cmd_send_pack(int argc, const char **argv, const 
char *prefix);
 extern int cmd_shortlog(int argc, const char **argv, const char *prefix);
 extern int cmd_show(int argc, const char **argv, const char *prefix);
 extern int cmd_show_branch(int argc, const char **argv, const char *prefix);
+extern int cmd_stash__helper(int argc, const char **argv, const char *prefix);
 extern int cmd_status(int argc, const char **argv, const char *prefix);
 extern int cmd_stripspace(int argc, const char **argv, const char *prefix);
 extern int cmd_submodule__helper(int argc, const char **argv, const char 
*prefix);
diff --git a/builtin/stash--helper.c b/builtin/stash--helper.c
new file mode 100644
index 0000000..542e782
--- /dev/null
+++ b/builtin/stash--helper.c
@@ -0,0 +1,13 @@
+#include "../stash.h"
+#include <string.h>
+
+static const char builtin_stash__helper_usage[] = {
+       "Usage: git stash--helper --non-patch <tmp_indexfile> <i_tree>"
+};
+
+int cmd_stash__helper(int argc, const char **argv, const char *prefix)
+{
+       if (argc == 4 && !strcmp("--non-patch", argv[1]))
+               return stash_non_patch(argv[2], argv[3], prefix);
+       usage(builtin_stash__helper_usage);
+}
diff --git a/git.c b/git.c
index da278c3..9829ee8 100644
--- a/git.c
+++ b/git.c
@@ -470,6 +470,7 @@ static struct cmd_struct commands[] = {
        { "show-branch", cmd_show_branch, RUN_SETUP },
        { "show-ref", cmd_show_ref, RUN_SETUP },
        { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+       { "stash--helper", cmd_stash__helper, RUN_SETUP | NEED_WORK_TREE },
        { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
        { "stripspace", cmd_stripspace },
        { "submodule--helper", cmd_submodule__helper, RUN_SETUP },
diff --git a/stash.c b/stash.c
new file mode 100644
index 0000000..c3d6e67
--- /dev/null
+++ b/stash.c
@@ -0,0 +1,65 @@
+#include "stash.h"
+#include "strbuf.h"
+
+static int prepare_update_index_argv(struct argv_array *args,
+       struct strbuf *buf)
+{
+       struct strbuf **bufs, **b;
+
+       bufs = strbuf_split(buf, '\0');
+       for (b = bufs; *b; b++)
+               argv_array_pushf(args, "%s", (*b)->buf);
+       argv_array_push(args, "--");
+       strbuf_list_free(bufs);
+
+       return 0;
+}
+
+int stash_non_patch(const char *tmp_indexfile, const char *i_tree,
+       const char *prefix)
+{
+       int result;
+       struct child_process read_tree = CHILD_PROCESS_INIT;
+       struct child_process diff = CHILD_PROCESS_INIT;
+       struct child_process update_index = CHILD_PROCESS_INIT;
+       struct child_process write_tree = CHILD_PROCESS_INIT;
+       struct strbuf buf = STRBUF_INIT;
+
+       argv_array_push(&read_tree.args, "read-tree");
+       argv_array_pushf(&read_tree.args, "--index-output=%s", tmp_indexfile);
+       argv_array_pushl(&read_tree.args, "-m", i_tree, NULL);
+
+       argv_array_pushl(&diff.args, "diff", "--name-only", "-z", "HEAD", "--",
+               NULL);
+
+       argv_array_pushl(&update_index.args, "update-index", "--add",
+               "--remove", NULL);
+
+       argv_array_push(&write_tree.args, "write-tree");
+
+       read_tree.env =
+               diff.env =
+               update_index.env =
+               write_tree.env = prefix;
+
+       read_tree.use_shell =
+               diff.use_shell =
+               update_index.use_shell =
+               write_tree.use_shell = 1;
+
+       read_tree.git_cmd =
+               diff.git_cmd =
+               update_index.git_cmd =
+               write_tree.git_cmd = 1;
+
+       result = run_command(&read_tree) ||
+               setenv("GIT_INDEX_FILE", tmp_indexfile, 1) ||
+               capture_command(&diff, &buf, 0) ||
+               prepare_update_index_argv(&update_index.args, &buf) ||
+               run_command(&update_index) ||
+               run_command(&write_tree) ||
+               remove(tmp_indexfile);
+
+       strbuf_release(&buf);
+       return result;
+}
diff --git a/stash.h b/stash.h
new file mode 100644
index 0000000..0880456
--- /dev/null
+++ b/stash.h
@@ -0,0 +1,11 @@
+#ifndef STASH_H
+#define STASH_H
+
+#include "git-compat-util.h"
+#include "gettext.h"
+#include "run-command.h"
+
+extern int stash_non_patch(const char *tmp_indexfile, const char *i_tree,
+       const char *prefix);
+
+#endif /* STASH_H */

--
https://github.com/git/git/pull/191
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to