test: remove httpd tests that ask for user
[git] / builtin / stage.c
1 /*
2  * 'git stage' builtin command
3  *
4  * Copyright (C) 2013 Felipe Contreras
5  */
6
7 #include "builtin.h"
8 #include "parse-options.h"
9 #include "diff.h"
10 #include "diffcore.h"
11 #include "revision.h"
12
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>..."),
20         NULL
21 };
22
23 static int do_reset(const char *prefix)
24 {
25         const char *argv[] = { "reset", "--quiet", NULL };
26         return cmd_reset(2, argv, prefix);
27 }
28
29 static int do_apply(const char *file, const char *prefix)
30 {
31         const char *argv[] = { "apply", "--recount", "--cached", file, NULL };
32         return cmd_apply(4, argv, prefix);
33 }
34
35 static int edit(int argc, const char **argv, const char *prefix)
36 {
37         char *file = git_pathdup("STAGE_EDIT.patch");
38         int out;
39         struct rev_info rev;
40         int ret = 0;
41         struct stat st;
42
43         read_cache();
44
45         init_revisions(&rev, prefix);
46         rev.diffopt.context = 7;
47
48         argc = setup_revisions(argc, argv, &rev, NULL);
49         add_head_to_pending(&rev);
50         if (!rev.pending.nr) {
51                 struct tree *tree;
52                 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
53                 add_pending_object(&rev, &tree->object, "HEAD");
54         }
55
56         rev.diffopt.output_format = DIFF_FORMAT_PATCH;
57         rev.diffopt.use_color = 0;
58         DIFF_OPT_SET(&rev.diffopt, IGNORE_DIRTY_SUBMODULES);
59
60         out = open(file, O_CREAT | O_WRONLY, 0666);
61         if (out < 0)
62                 die(_("Could not open '%s' for writing."), file);
63         rev.diffopt.file = xfdopen(out, "w");
64         rev.diffopt.close_file = 1;
65
66         if (run_diff_index(&rev, 1))
67                 die(_("Could not write patch"));
68         if (launch_editor(file, NULL, NULL))
69                 exit(1);
70
71         if (stat(file, &st))
72                 die_errno(_("Could not stat '%s'"), file);
73
74         ret = do_reset(prefix);
75         if (ret)
76                 goto leave;
77
78         if (!st.st_size)
79                 goto leave;
80
81         ret = do_apply(file, prefix);
82         if (ret)
83                 goto leave;
84
85 leave:
86         unlink(file);
87         free(file);
88         return ret;
89 }
90
91 int cmd_stage(int argc, const char **argv, const char *prefix)
92 {
93         struct option options[] = { OPT_END() };
94
95         argc = parse_options(argc, argv, prefix, options, stage_usage,
96                         PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH);
97
98         if (argc > 1) {
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")) {
104                         argv[0] = "diff";
105                         argv[1] = "--staged";
106
107                         return cmd_diff(argc, argv, prefix);
108                 }
109                 if (!strcmp(argv[1], "rm")) {
110                         argv[0] = "rm";
111                         argv[1] = "--cached";
112
113                         return cmd_rm(argc, argv, prefix);
114                 }
115                 if (!strcmp(argv[1], "apply")) {
116                         argv[0] = "apply";
117                         argv[1] = "--cached";
118
119                         return cmd_apply(argc, argv, prefix);
120                 }
121                 if (!strcmp(argv[1], "edit")) {
122                         return edit(argc - 1, argv + 1, prefix);
123                 }
124         }
125
126         return cmd_add(argc, argv, prefix);
127 }