transport->progress: use flag authoritatively
[git/dscho.git] / builtin-push.c
blobdce3152fec7b73821fa1a27cd841c7bd973d966c
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"
12 static const char * const push_usage[] = {
13 "git push [<options>] [<repository> <refspec>...]",
14 NULL,
17 static int thin;
18 static int deleterefs;
19 static const char *receivepack;
20 static int verbosity;
22 static const char **refspec;
23 static int refspec_nr;
25 static void add_refspec(const char *ref)
27 int nr = refspec_nr + 1;
28 refspec = xrealloc(refspec, nr * sizeof(char *));
29 refspec[nr-1] = ref;
30 refspec_nr = nr;
33 static void set_refspecs(const char **refs, int nr)
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/");
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);
66 static void setup_push_tracking(void)
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);
82 static void setup_default_push_refspecs(void)
84 switch (push_default) {
85 default:
86 case PUSH_DEFAULT_MATCHING:
87 add_refspec(":");
88 break;
90 case PUSH_DEFAULT_TRACKING:
91 setup_push_tracking();
92 break;
94 case PUSH_DEFAULT_CURRENT:
95 add_refspec("HEAD");
96 break;
98 case PUSH_DEFAULT_NOTHING:
99 die("You didn't specify any refspecs to push, and "
100 "push.default is \"nothing\".");
101 break;
105 static int push_with_options(struct transport *transport, int flags)
107 int err;
108 int nonfastforward;
110 transport_set_verbosity(transport, verbosity, 0);
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");
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);
125 err |= transport_disconnect(transport);
127 if (!err)
128 return 0;
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");
136 return 1;
139 static int do_push(const char *repo, int flags)
141 int i, errs;
142 struct remote *remote = remote_get(repo);
143 const char **url;
144 int url_nr;
146 if (!remote) {
147 if (repo)
148 die("bad repository '%s'", repo);
149 die("No destination configured to push to.");
152 if (remote->mirror)
153 flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
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");
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");
167 if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
168 (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
169 return error("--all and --mirror are incompatible");
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();
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;
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++;
194 } else {
195 struct transport *transport =
196 transport_get(remote, NULL);
198 if (push_with_options(transport, flags))
199 errs++;
201 return !!errs;
204 int cmd_push(int argc, const char **argv, const char *prefix)
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()
229 git_config(git_default_config, NULL);
230 argc = parse_options(argc, argv, prefix, options, push_usage, 0);
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");
237 if (tags)
238 add_refspec("refs/tags/*");
240 if (argc > 0) {
241 repo = argv[0];
242 set_refspecs(argv + 1, argc - 1);
245 rc = do_push(repo, flags);
246 if (rc == -1)
247 usage_with_options(push_usage, options);
248 else
249 return rc;