New config push.default to decide default behavior for push
[git] / builtin-push.c
1 /*
2  * "git push"
3  */
4 #include "cache.h"
5 #include "refs.h"
6 #include "run-command.h"
7 #include "builtin.h"
8 #include "remote.h"
9 #include "transport.h"
10 #include "parse-options.h"
11
12 static const char * const push_usage[] = {
13         "git push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=<git-receive-pack>] [--repo=<repository>] [-f | --force] [-v] [<repository> <refspec>...]",
14         NULL,
15 };
16
17 static int thin;
18 static const char *receivepack;
19
20 static const char **refspec;
21 static int refspec_nr;
22
23 static void add_refspec(const char *ref)
24 {
25         int nr = refspec_nr + 1;
26         refspec = xrealloc(refspec, nr * sizeof(char *));
27         refspec[nr-1] = ref;
28         refspec_nr = nr;
29 }
30
31 static void set_refspecs(const char **refs, int nr)
32 {
33         int i;
34         for (i = 0; i < nr; i++) {
35                 const char *ref = refs[i];
36                 if (!strcmp("tag", ref)) {
37                         char *tag;
38                         int len;
39                         if (nr <= ++i)
40                                 die("tag shorthand without <tag>");
41                         len = strlen(refs[i]) + 11;
42                         tag = xmalloc(len);
43                         strcpy(tag, "refs/tags/");
44                         strcat(tag, refs[i]);
45                         ref = tag;
46                 }
47                 add_refspec(ref);
48         }
49 }
50
51 static void setup_push_tracking(void)
52 {
53         struct strbuf refspec = STRBUF_INIT;
54         struct branch *branch = branch_get(NULL);
55         if (!branch)
56                 die("You are not currently on a branch.");
57         if (!branch->merge_nr)
58                 die("The current branch %s is not tracking anything.",
59                     branch->name);
60         if (branch->merge_nr != 1)
61                 die("The current branch %s is tracking multiple branches, "
62                     "refusing to push.", branch->name);
63         strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
64         add_refspec(refspec.buf);
65 }
66
67 static void setup_default_push_refspecs(void)
68 {
69         git_config(git_default_config, NULL);
70         switch (push_default) {
71         case PUSH_DEFAULT_UNSPECIFIED:
72                 /* fallthrough */
73
74         case PUSH_DEFAULT_MATCHING:
75                 add_refspec(":");
76                 break;
77
78         case PUSH_DEFAULT_TRACKING:
79                 setup_push_tracking();
80                 break;
81
82         case PUSH_DEFAULT_CURRENT:
83                 add_refspec("HEAD");
84                 break;
85
86         case PUSH_DEFAULT_NOTHING:
87                 die("You didn't specify any refspecs to push, and "
88                     "push.default is \"nothing\".");
89                 break;
90         }
91 }
92
93 static int do_push(const char *repo, int flags)
94 {
95         int i, errs;
96         struct remote *remote = remote_get(repo);
97
98         if (!remote)
99                 die("bad repository '%s'", repo);
100
101         if (remote->mirror)
102                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
103
104         if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
105                 if (!strcmp(*refspec, "refs/tags/*"))
106                         return error("--all and --tags are incompatible");
107                 return error("--all can't be combined with refspecs");
108         }
109
110         if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
111                 if (!strcmp(*refspec, "refs/tags/*"))
112                         return error("--mirror and --tags are incompatible");
113                 return error("--mirror can't be combined with refspecs");
114         }
115
116         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
117                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
118                 return error("--all and --mirror are incompatible");
119         }
120
121         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
122                 if (remote->push_refspec_nr) {
123                         refspec = remote->push_refspec;
124                         refspec_nr = remote->push_refspec_nr;
125                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
126                         setup_default_push_refspecs();
127         }
128         errs = 0;
129         for (i = 0; i < remote->url_nr; i++) {
130                 struct transport *transport =
131                         transport_get(remote, remote->url[i]);
132                 int err;
133                 if (receivepack)
134                         transport_set_option(transport,
135                                              TRANS_OPT_RECEIVEPACK, receivepack);
136                 if (thin)
137                         transport_set_option(transport, TRANS_OPT_THIN, "yes");
138
139                 if (flags & TRANSPORT_PUSH_VERBOSE)
140                         fprintf(stderr, "Pushing to %s\n", remote->url[i]);
141                 err = transport_push(transport, refspec_nr, refspec, flags);
142                 err |= transport_disconnect(transport);
143
144                 if (!err)
145                         continue;
146
147                 error("failed to push some refs to '%s'", remote->url[i]);
148                 errs++;
149         }
150         return !!errs;
151 }
152
153 int cmd_push(int argc, const char **argv, const char *prefix)
154 {
155         int flags = 0;
156         int tags = 0;
157         int rc;
158         const char *repo = NULL;        /* default repository */
159
160         struct option options[] = {
161                 OPT_BIT('v', "verbose", &flags, "be verbose", TRANSPORT_PUSH_VERBOSE),
162                 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
163                 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
164                 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
165                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
166                 OPT_BOOLEAN( 0 , "tags", &tags, "push tags"),
167                 OPT_BIT( 0 , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
168                 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
169                 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
170                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
171                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
172                 OPT_END()
173         };
174
175         argc = parse_options(argc, argv, options, push_usage, 0);
176
177         if (tags)
178                 add_refspec("refs/tags/*");
179
180         if (argc > 0) {
181                 repo = argv[0];
182                 set_refspecs(argv + 1, argc - 1);
183         }
184
185         rc = do_push(repo, flags);
186         if (rc == -1)
187                 usage_with_options(push_usage, options);
188         else
189                 return rc;
190 }