]> rtime.felk.cvut.cz Git - git.git/blob - builtin-push.c
transport->progress: use flag authoritatively
[git.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 [<options>] [<repository> <refspec>...]",
14         NULL,
15 };
16
17 static int thin;
18 static int deleterefs;
19 static const char *receivepack;
20 static int verbosity;
21
22 static const char **refspec;
23 static int refspec_nr;
24
25 static void add_refspec(const char *ref)
26 {
27         int nr = refspec_nr + 1;
28         refspec = xrealloc(refspec, nr * sizeof(char *));
29         refspec[nr-1] = ref;
30         refspec_nr = nr;
31 }
32
33 static void set_refspecs(const char **refs, int nr)
34 {
35         int i;
36         for (i = 0; i < nr; i++) {
37                 const char *ref = refs[i];
38                 if (!strcmp("tag", ref)) {
39                         char *tag;
40                         int len;
41                         if (nr <= ++i)
42                                 die("tag shorthand without <tag>");
43                         len = strlen(refs[i]) + 11;
44                         if (deleterefs) {
45                                 tag = xmalloc(len+1);
46                                 strcpy(tag, ":refs/tags/");
47                         } else {
48                                 tag = xmalloc(len);
49                                 strcpy(tag, "refs/tags/");
50                         }
51                         strcat(tag, refs[i]);
52                         ref = tag;
53                 } else if (deleterefs && !strchr(ref, ':')) {
54                         char *delref;
55                         int len = strlen(ref)+1;
56                         delref = xmalloc(len+1);
57                         strcpy(delref, ":");
58                         strcat(delref, ref);
59                         ref = delref;
60                 } else if (deleterefs)
61                         die("--delete only accepts plain target ref names");
62                 add_refspec(ref);
63         }
64 }
65
66 static void setup_push_tracking(void)
67 {
68         struct strbuf refspec = STRBUF_INIT;
69         struct branch *branch = branch_get(NULL);
70         if (!branch)
71                 die("You are not currently on a branch.");
72         if (!branch->merge_nr)
73                 die("The current branch %s is not tracking anything.",
74                     branch->name);
75         if (branch->merge_nr != 1)
76                 die("The current branch %s is tracking multiple branches, "
77                     "refusing to push.", branch->name);
78         strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
79         add_refspec(refspec.buf);
80 }
81
82 static void setup_default_push_refspecs(void)
83 {
84         switch (push_default) {
85         default:
86         case PUSH_DEFAULT_MATCHING:
87                 add_refspec(":");
88                 break;
89
90         case PUSH_DEFAULT_TRACKING:
91                 setup_push_tracking();
92                 break;
93
94         case PUSH_DEFAULT_CURRENT:
95                 add_refspec("HEAD");
96                 break;
97
98         case PUSH_DEFAULT_NOTHING:
99                 die("You didn't specify any refspecs to push, and "
100                     "push.default is \"nothing\".");
101                 break;
102         }
103 }
104
105 static int push_with_options(struct transport *transport, int flags)
106 {
107         int err;
108         int nonfastforward;
109
110         transport_set_verbosity(transport, verbosity, 0);
111
112         if (receivepack)
113                 transport_set_option(transport,
114                                      TRANS_OPT_RECEIVEPACK, receivepack);
115         if (thin)
116                 transport_set_option(transport, TRANS_OPT_THIN, "yes");
117
118         if (verbosity > 0)
119                 fprintf(stderr, "Pushing to %s\n", transport->url);
120         err = transport_push(transport, refspec_nr, refspec, flags,
121                              &nonfastforward);
122         if (err != 0)
123                 error("failed to push some refs to '%s'", transport->url);
124
125         err |= transport_disconnect(transport);
126
127         if (!err)
128                 return 0;
129
130         if (nonfastforward && advice_push_nonfastforward) {
131                 printf("To prevent you from losing history, non-fast-forward updates were rejected\n"
132                        "Merge the remote changes before pushing again.  See the 'Note about\n"
133                        "fast-forwards' section of 'git push --help' for details.\n");
134         }
135
136         return 1;
137 }
138
139 static int do_push(const char *repo, int flags)
140 {
141         int i, errs;
142         struct remote *remote = remote_get(repo);
143         const char **url;
144         int url_nr;
145
146         if (!remote) {
147                 if (repo)
148                         die("bad repository '%s'", repo);
149                 die("No destination configured to push to.");
150         }
151
152         if (remote->mirror)
153                 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
154
155         if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
156                 if (!strcmp(*refspec, "refs/tags/*"))
157                         return error("--all and --tags are incompatible");
158                 return error("--all can't be combined with refspecs");
159         }
160
161         if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
162                 if (!strcmp(*refspec, "refs/tags/*"))
163                         return error("--mirror and --tags are incompatible");
164                 return error("--mirror can't be combined with refspecs");
165         }
166
167         if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
168                                 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
169                 return error("--all and --mirror are incompatible");
170         }
171
172         if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
173                 if (remote->push_refspec_nr) {
174                         refspec = remote->push_refspec;
175                         refspec_nr = remote->push_refspec_nr;
176                 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
177                         setup_default_push_refspecs();
178         }
179         errs = 0;
180         if (remote->pushurl_nr) {
181                 url = remote->pushurl;
182                 url_nr = remote->pushurl_nr;
183         } else {
184                 url = remote->url;
185                 url_nr = remote->url_nr;
186         }
187         if (url_nr) {
188                 for (i = 0; i < url_nr; i++) {
189                         struct transport *transport =
190                                 transport_get(remote, url[i]);
191                         if (push_with_options(transport, flags))
192                                 errs++;
193                 }
194         } else {
195                 struct transport *transport =
196                         transport_get(remote, NULL);
197
198                 if (push_with_options(transport, flags))
199                         errs++;
200         }
201         return !!errs;
202 }
203
204 int cmd_push(int argc, const char **argv, const char *prefix)
205 {
206         int flags = 0;
207         int tags = 0;
208         int rc;
209         const char *repo = NULL;        /* default repository */
210         struct option options[] = {
211                 OPT__VERBOSITY(&verbosity),
212                 OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
213                 OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
214                 OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
215                             (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
216                 OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
217                 OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
218                 OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
219                 OPT_BIT( 0,  "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
220                 OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
221                 OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
222                 OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
223                 OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
224                 OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
225                         TRANSPORT_PUSH_SET_UPSTREAM),
226                 OPT_END()
227         };
228
229         git_config(git_default_config, NULL);
230         argc = parse_options(argc, argv, prefix, options, push_usage, 0);
231
232         if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
233                 die("--delete is incompatible with --all, --mirror and --tags");
234         if (deleterefs && argc < 2)
235                 die("--delete doesn't make sense without any refs");
236
237         if (tags)
238                 add_refspec("refs/tags/*");
239
240         if (argc > 0) {
241                 repo = argv[0];
242                 set_refspecs(argv + 1, argc - 1);
243         }
244
245         rc = do_push(repo, flags);
246         if (rc == -1)
247                 usage_with_options(push_usage, options);
248         else
249                 return rc;
250 }