6 #include "run-command.h"
10 #include "parse-options.h"
12 static const char * const push_usage
[] = {
13 "git push [<options>] [<repository> [<refspec>...]]",
18 static int deleterefs
;
19 static const char *receivepack
;
23 static const char **refspec
;
24 static int refspec_nr
;
25 static int refspec_alloc
;
27 static void add_refspec(const char *ref
)
30 ALLOC_GROW(refspec
, refspec_nr
, refspec_alloc
);
31 refspec
[refspec_nr
-1] = ref
;
34 static void set_refspecs(const char **refs
, int nr
)
37 for (i
= 0; i
< nr
; i
++) {
38 const char *ref
= refs
[i
];
39 if (!strcmp("tag", ref
)) {
43 die("tag shorthand without <tag>");
44 len
= strlen(refs
[i
]) + 11;
47 strcpy(tag
, ":refs/tags/");
50 strcpy(tag
, "refs/tags/");
54 } else if (deleterefs
&& !strchr(ref
, ':')) {
56 int len
= strlen(ref
)+1;
57 delref
= xmalloc(len
+1);
61 } else if (deleterefs
)
62 die("--delete only accepts plain target ref names");
67 static void setup_push_upstream(struct remote
*remote
)
69 struct strbuf refspec
= STRBUF_INIT
;
70 struct branch
*branch
= branch_get(NULL
);
72 die("You are not currently on a branch.\n"
73 "To push the history leading to the current (detached HEAD)\n"
76 " git push %s HEAD:<name-of-remote-branch>\n",
78 if (!branch
->merge_nr
|| !branch
->merge
)
79 die("The current branch %s has no upstream branch.\n"
80 "To push the current branch and set the remote as upstream, use\n"
82 " git push --set-upstream %s %s\n",
86 if (branch
->merge_nr
!= 1)
87 die("The current branch %s has multiple upstream branches, "
88 "refusing to push.", branch
->name
);
89 strbuf_addf(&refspec
, "%s:%s", branch
->name
, branch
->merge
[0]->src
);
90 add_refspec(refspec
.buf
);
93 static void setup_default_push_refspecs(struct remote
*remote
)
95 switch (push_default
) {
97 case PUSH_DEFAULT_MATCHING
:
101 case PUSH_DEFAULT_UPSTREAM
:
102 setup_push_upstream(remote
);
105 case PUSH_DEFAULT_CURRENT
:
109 case PUSH_DEFAULT_NOTHING
:
110 die("You didn't specify any refspecs to push, and "
111 "push.default is \"nothing\".");
116 static int push_with_options(struct transport
*transport
, int flags
)
121 transport_set_verbosity(transport
, verbosity
, progress
);
124 transport_set_option(transport
,
125 TRANS_OPT_RECEIVEPACK
, receivepack
);
127 transport_set_option(transport
, TRANS_OPT_THIN
, "yes");
130 fprintf(stderr
, "Pushing to %s\n", transport
->url
);
131 err
= transport_push(transport
, refspec_nr
, refspec
, flags
,
134 error("failed to push some refs to '%s'", transport
->url
);
136 err
|= transport_disconnect(transport
);
141 if (nonfastforward
&& advice_push_nonfastforward
) {
142 fprintf(stderr
, "To prevent you from losing history, non-fast-forward updates were rejected\n"
143 "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
144 "'Note about fast-forwards' section of 'git push --help' for details.\n");
150 static int do_push(const char *repo
, int flags
)
153 struct remote
*remote
= remote_get(repo
);
159 die("bad repository '%s'", repo
);
160 die("No destination configured to push to.");
164 flags
|= (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
);
166 if ((flags
& TRANSPORT_PUSH_ALL
) && refspec
) {
167 if (!strcmp(*refspec
, "refs/tags/*"))
168 return error("--all and --tags are incompatible");
169 return error("--all can't be combined with refspecs");
172 if ((flags
& TRANSPORT_PUSH_MIRROR
) && refspec
) {
173 if (!strcmp(*refspec
, "refs/tags/*"))
174 return error("--mirror and --tags are incompatible");
175 return error("--mirror can't be combined with refspecs");
178 if ((flags
& (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) ==
179 (TRANSPORT_PUSH_ALL
|TRANSPORT_PUSH_MIRROR
)) {
180 return error("--all and --mirror are incompatible");
183 if (!refspec
&& !(flags
& TRANSPORT_PUSH_ALL
)) {
184 if (remote
->push_refspec_nr
) {
185 refspec
= remote
->push_refspec
;
186 refspec_nr
= remote
->push_refspec_nr
;
187 } else if (!(flags
& TRANSPORT_PUSH_MIRROR
))
188 setup_default_push_refspecs(remote
);
191 if (remote
->pushurl_nr
) {
192 url
= remote
->pushurl
;
193 url_nr
= remote
->pushurl_nr
;
196 url_nr
= remote
->url_nr
;
199 for (i
= 0; i
< url_nr
; i
++) {
200 struct transport
*transport
=
201 transport_get(remote
, url
[i
]);
202 if (push_with_options(transport
, flags
))
206 struct transport
*transport
=
207 transport_get(remote
, NULL
);
209 if (push_with_options(transport
, flags
))
215 int cmd_push(int argc
, const char **argv
, const char *prefix
)
220 const char *repo
= NULL
; /* default repository */
221 struct option options
[] = {
222 OPT__VERBOSITY(&verbosity
),
223 OPT_STRING( 0 , "repo", &repo
, "repository", "repository"),
224 OPT_BIT( 0 , "all", &flags
, "push all refs", TRANSPORT_PUSH_ALL
),
225 OPT_BIT( 0 , "mirror", &flags
, "mirror all refs",
226 (TRANSPORT_PUSH_MIRROR
|TRANSPORT_PUSH_FORCE
)),
227 OPT_BOOLEAN( 0, "delete", &deleterefs
, "delete refs"),
228 OPT_BOOLEAN( 0 , "tags", &tags
, "push tags (can't be used with --all or --mirror)"),
229 OPT_BIT('n' , "dry-run", &flags
, "dry run", TRANSPORT_PUSH_DRY_RUN
),
230 OPT_BIT( 0, "porcelain", &flags
, "machine-readable output", TRANSPORT_PUSH_PORCELAIN
),
231 OPT_BIT('f', "force", &flags
, "force updates", TRANSPORT_PUSH_FORCE
),
232 OPT_BOOLEAN( 0 , "thin", &thin
, "use thin pack"),
233 OPT_STRING( 0 , "receive-pack", &receivepack
, "receive-pack", "receive pack program"),
234 OPT_STRING( 0 , "exec", &receivepack
, "receive-pack", "receive pack program"),
235 OPT_BIT('u', "set-upstream", &flags
, "set upstream for git pull/status",
236 TRANSPORT_PUSH_SET_UPSTREAM
),
237 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
241 git_config(git_default_config
, NULL
);
242 argc
= parse_options(argc
, argv
, prefix
, options
, push_usage
, 0);
244 if (deleterefs
&& (tags
|| (flags
& (TRANSPORT_PUSH_ALL
| TRANSPORT_PUSH_MIRROR
))))
245 die("--delete is incompatible with --all, --mirror and --tags");
246 if (deleterefs
&& argc
< 2)
247 die("--delete doesn't make sense without any refs");
250 add_refspec("refs/tags/*");
254 set_refspecs(argv
+ 1, argc
- 1);
257 rc
= do_push(repo
, flags
);
259 usage_with_options(push_usage
, options
);