2 * 'git stage' builtin command
4 * Copyright (C) 2013 Felipe Contreras
8 #include "parse-options.h"
13 static const char *const stage_usage[] = {
14 N_("git stage [options] [--] <paths>..."),
15 N_("git stage add [options] [--] <paths>..."),
16 N_("git stage apply [options] [<patch>...]"),
17 N_("git stage reset [-q|--patch] [--] <paths>..."),
18 N_("git stage diff [options] [<commit]> [--] <paths>..."),
19 N_("git stage rm [options] [--] <paths>..."),
23 static int do_reset(const char *prefix)
25 const char *argv[] = { "reset", "--quiet", NULL };
26 return cmd_reset(2, argv, prefix);
29 static int do_apply(const char *file, const char *prefix)
31 const char *argv[] = { "apply", "--recount", "--cached", file, NULL };
32 return cmd_apply(4, argv, prefix);
35 static int edit(int argc, const char **argv, const char *prefix)
37 char *file = git_pathdup("STAGE_EDIT.patch");
45 init_revisions(&rev, prefix);
46 rev.diffopt.context = 7;
48 argc = setup_revisions(argc, argv, &rev, NULL);
49 add_head_to_pending(&rev);
50 if (!rev.pending.nr) {
52 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
53 add_pending_object(&rev, &tree->object, "HEAD");
56 rev.diffopt.output_format = DIFF_FORMAT_PATCH;
57 rev.diffopt.use_color = 0;
58 DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
60 out = open(file, O_CREAT | O_WRONLY, 0666);
62 die(_("Could not open '%s' for writing."), file);
63 rev.diffopt.file = xfdopen(out, "w");
64 rev.diffopt.close_file = 1;
66 if (run_diff_index(&rev, 1))
67 die(_("Could not write patch"));
68 if (launch_editor(file, NULL, NULL))
72 die_errno(_("Could not stat '%s'"), file);
74 ret = do_reset(prefix);
81 ret = do_apply(file, prefix);
91 int cmd_stage(int argc, const char **argv, const char *prefix)
93 struct option options[] = { OPT_END() };
95 argc = parse_options(argc, argv, prefix, options, stage_usage,
96 PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
99 if (!strcmp(argv[1], "add"))
100 return cmd_add(argc - 1, argv + 1, prefix);
101 if (!strcmp(argv[1], "reset"))
102 return cmd_reset(argc - 1, argv + 1, prefix);
103 if (!strcmp(argv[1], "diff")) {
105 argv[1] = "--staged";
107 return cmd_diff(argc, argv, prefix);
109 if (!strcmp(argv[1], "rm")) {
111 argv[1] = "--cached";
113 return cmd_rm(argc, argv, prefix);
115 if (!strcmp(argv[1], "apply")) {
117 argv[1] = "--cached";
119 return cmd_apply(argc, argv, prefix);
121 if (!strcmp(argv[1], "edit")) {
122 return edit(argc - 1, argv + 1, prefix);
126 return cmd_add(argc, argv, prefix);