You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
6.5 KiB
253 lines
6.5 KiB
19 years ago
|
/*
|
||
|
* "git push"
|
||
|
*/
|
||
|
#include "cache.h"
|
||
|
#include "refs.h"
|
||
|
#include "run-command.h"
|
||
|
#include "builtin.h"
|
||
18 years ago
|
#include "remote.h"
|
||
18 years ago
|
#include "transport.h"
|
||
17 years ago
|
#include "parse-options.h"
|
||
19 years ago
|
|
||
17 years ago
|
static const char * const push_usage[] = {
|
||
15 years ago
|
"git push [<options>] [<repository> [<refspec>...]]",
|
||
17 years ago
|
NULL,
|
||
|
};
|
||
19 years ago
|
|
||
17 years ago
|
static int thin;
|
||
15 years ago
|
static int deleterefs;
|
||
18 years ago
|
static const char *receivepack;
|
||
15 years ago
|
static int verbosity;
|
||
15 years ago
|
static int progress;
|
||
19 years ago
|
|
||
19 years ago
|
static const char **refspec;
|
||
|
static int refspec_nr;
|
||
19 years ago
|
|
||
|
static void add_refspec(const char *ref)
|
||
|
{
|
||
|
int nr = refspec_nr + 1;
|
||
|
refspec = xrealloc(refspec, nr * sizeof(char *));
|
||
|
refspec[nr-1] = ref;
|
||
|
refspec_nr = nr;
|
||
|
}
|
||
|
|
||
|
static void set_refspecs(const char **refs, int nr)
|
||
|
{
|
||
18 years ago
|
int i;
|
||
|
for (i = 0; i < nr; i++) {
|
||
|
const char *ref = refs[i];
|
||
|
if (!strcmp("tag", ref)) {
|
||
|
char *tag;
|
||
|
int len;
|
||
|
if (nr <= ++i)
|
||
|
die("tag shorthand without <tag>");
|
||
|
len = strlen(refs[i]) + 11;
|
||
15 years ago
|
if (deleterefs) {
|
||
|
tag = xmalloc(len+1);
|
||
|
strcpy(tag, ":refs/tags/");
|
||
|
} else {
|
||
|
tag = xmalloc(len);
|
||
|
strcpy(tag, "refs/tags/");
|
||
|
}
|
||
18 years ago
|
strcat(tag, refs[i]);
|
||
|
ref = tag;
|
||
15 years ago
|
} else if (deleterefs && !strchr(ref, ':')) {
|
||
|
char *delref;
|
||
|
int len = strlen(ref)+1;
|
||
15 years ago
|
delref = xmalloc(len+1);
|
||
15 years ago
|
strcpy(delref, ":");
|
||
|
strcat(delref, ref);
|
||
|
ref = delref;
|
||
|
} else if (deleterefs)
|
||
|
die("--delete only accepts plain target ref names");
|
||
18 years ago
|
add_refspec(ref);
|
||
19 years ago
|
}
|
||
|
}
|
||
|
|
||
16 years ago
|
static void setup_push_tracking(void)
|
||
|
{
|
||
|
struct strbuf refspec = STRBUF_INIT;
|
||
|
struct branch *branch = branch_get(NULL);
|
||
|
if (!branch)
|
||
|
die("You are not currently on a branch.");
|
||
15 years ago
|
if (!branch->merge_nr || !branch->merge)
|
||
16 years ago
|
die("The current branch %s is not tracking anything.",
|
||
|
branch->name);
|
||
|
if (branch->merge_nr != 1)
|
||
|
die("The current branch %s is tracking multiple branches, "
|
||
|
"refusing to push.", branch->name);
|
||
|
strbuf_addf(&refspec, "%s:%s", branch->name, branch->merge[0]->src);
|
||
|
add_refspec(refspec.buf);
|
||
|
}
|
||
|
|
||
|
static void setup_default_push_refspecs(void)
|
||
|
{
|
||
|
switch (push_default) {
|
||
16 years ago
|
default:
|
||
16 years ago
|
case PUSH_DEFAULT_MATCHING:
|
||
|
add_refspec(":");
|
||
|
break;
|
||
|
|
||
|
case PUSH_DEFAULT_TRACKING:
|
||
|
setup_push_tracking();
|
||
|
break;
|
||
|
|
||
|
case PUSH_DEFAULT_CURRENT:
|
||
|
add_refspec("HEAD");
|
||
|
break;
|
||
|
|
||
|
case PUSH_DEFAULT_NOTHING:
|
||
|
die("You didn't specify any refspecs to push, and "
|
||
|
"push.default is \"nothing\".");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
static int push_with_options(struct transport *transport, int flags)
|
||
|
{
|
||
|
int err;
|
||
|
int nonfastforward;
|
||
15 years ago
|
|
||
15 years ago
|
transport_set_verbosity(transport, verbosity, progress);
|
||
15 years ago
|
|
||
15 years ago
|
if (receivepack)
|
||
|
transport_set_option(transport,
|
||
|
TRANS_OPT_RECEIVEPACK, receivepack);
|
||
|
if (thin)
|
||
|
transport_set_option(transport, TRANS_OPT_THIN, "yes");
|
||
|
|
||
15 years ago
|
if (verbosity > 0)
|
||
15 years ago
|
fprintf(stderr, "Pushing to %s\n", transport->url);
|
||
|
err = transport_push(transport, refspec_nr, refspec, flags,
|
||
|
&nonfastforward);
|
||
15 years ago
|
if (err != 0)
|
||
|
error("failed to push some refs to '%s'", transport->url);
|
||
|
|
||
15 years ago
|
err |= transport_disconnect(transport);
|
||
|
|
||
|
if (!err)
|
||
|
return 0;
|
||
|
|
||
|
if (nonfastforward && advice_push_nonfastforward) {
|
||
15 years ago
|
fprintf(stderr, "To prevent you from losing history, non-fast-forward updates were rejected\n"
|
||
|
"Merge the remote changes before pushing again. See the 'Note about\n"
|
||
|
"fast-forwards' section of 'git push --help' for details.\n");
|
||
15 years ago
|
}
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
18 years ago
|
static int do_push(const char *repo, int flags)
|
||
19 years ago
|
{
|
||
18 years ago
|
int i, errs;
|
||
|
struct remote *remote = remote_get(repo);
|
||
16 years ago
|
const char **url;
|
||
|
int url_nr;
|
||
19 years ago
|
|
||
16 years ago
|
if (!remote) {
|
||
|
if (repo)
|
||
|
die("bad repository '%s'", repo);
|
||
|
die("No destination configured to push to.");
|
||
|
}
|
||
19 years ago
|
|
||
17 years ago
|
if (remote->mirror)
|
||
|
flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);
|
||
|
|
||
17 years ago
|
if ((flags & TRANSPORT_PUSH_ALL) && refspec) {
|
||
|
if (!strcmp(*refspec, "refs/tags/*"))
|
||
|
return error("--all and --tags are incompatible");
|
||
|
return error("--all can't be combined with refspecs");
|
||
|
}
|
||
|
|
||
|
if ((flags & TRANSPORT_PUSH_MIRROR) && refspec) {
|
||
|
if (!strcmp(*refspec, "refs/tags/*"))
|
||
|
return error("--mirror and --tags are incompatible");
|
||
|
return error("--mirror can't be combined with refspecs");
|
||
|
}
|
||
17 years ago
|
|
||
|
if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) ==
|
||
|
(TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) {
|
||
|
return error("--all and --mirror are incompatible");
|
||
|
}
|
||
|
|
||
16 years ago
|
if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
|
||
|
if (remote->push_refspec_nr) {
|
||
|
refspec = remote->push_refspec;
|
||
|
refspec_nr = remote->push_refspec_nr;
|
||
|
} else if (!(flags & TRANSPORT_PUSH_MIRROR))
|
||
|
setup_default_push_refspecs();
|
||
18 years ago
|
}
|
||
18 years ago
|
errs = 0;
|
||
16 years ago
|
if (remote->pushurl_nr) {
|
||
|
url = remote->pushurl;
|
||
|
url_nr = remote->pushurl_nr;
|
||
|
} else {
|
||
|
url = remote->url;
|
||
|
url_nr = remote->url_nr;
|
||
|
}
|
||
15 years ago
|
if (url_nr) {
|
||
|
for (i = 0; i < url_nr; i++) {
|
||
|
struct transport *transport =
|
||
|
transport_get(remote, url[i]);
|
||
|
if (push_with_options(transport, flags))
|
||
|
errs++;
|
||
16 years ago
|
}
|
||
15 years ago
|
} else {
|
||
|
struct transport *transport =
|
||
|
transport_get(remote, NULL);
|
||
|
|
||
|
if (push_with_options(transport, flags))
|
||
|
errs++;
|
||
19 years ago
|
}
|
||
18 years ago
|
return !!errs;
|
||
19 years ago
|
}
|
||
|
|
||
19 years ago
|
int cmd_push(int argc, const char **argv, const char *prefix)
|
||
19 years ago
|
{
|
||
18 years ago
|
int flags = 0;
|
||
17 years ago
|
int tags = 0;
|
||
17 years ago
|
int rc;
|
||
18 years ago
|
const char *repo = NULL; /* default repository */
|
||
17 years ago
|
struct option options[] = {
|
||
15 years ago
|
OPT__VERBOSITY(&verbosity),
|
||
17 years ago
|
OPT_STRING( 0 , "repo", &repo, "repository", "repository"),
|
||
17 years ago
|
OPT_BIT( 0 , "all", &flags, "push all refs", TRANSPORT_PUSH_ALL),
|
||
|
OPT_BIT( 0 , "mirror", &flags, "mirror all refs",
|
||
|
(TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE)),
|
||
15 years ago
|
OPT_BOOLEAN( 0, "delete", &deleterefs, "delete refs"),
|
||
15 years ago
|
OPT_BOOLEAN( 0 , "tags", &tags, "push tags (can't be used with --all or --mirror)"),
|
||
16 years ago
|
OPT_BIT('n' , "dry-run", &flags, "dry run", TRANSPORT_PUSH_DRY_RUN),
|
||
16 years ago
|
OPT_BIT( 0, "porcelain", &flags, "machine-readable output", TRANSPORT_PUSH_PORCELAIN),
|
||
17 years ago
|
OPT_BIT('f', "force", &flags, "force updates", TRANSPORT_PUSH_FORCE),
|
||
17 years ago
|
OPT_BOOLEAN( 0 , "thin", &thin, "use thin pack"),
|
||
|
OPT_STRING( 0 , "receive-pack", &receivepack, "receive-pack", "receive pack program"),
|
||
|
OPT_STRING( 0 , "exec", &receivepack, "receive-pack", "receive pack program"),
|
||
15 years ago
|
OPT_BIT('u', "set-upstream", &flags, "set upstream for git pull/status",
|
||
|
TRANSPORT_PUSH_SET_UPSTREAM),
|
||
15 years ago
|
OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
|
||
17 years ago
|
OPT_END()
|
||
|
};
|
||
19 years ago
|
|
||
15 years ago
|
git_config(git_default_config, NULL);
|
||
16 years ago
|
argc = parse_options(argc, argv, prefix, options, push_usage, 0);
|
||
17 years ago
|
|
||
15 years ago
|
if (deleterefs && (tags || (flags & (TRANSPORT_PUSH_ALL | TRANSPORT_PUSH_MIRROR))))
|
||
|
die("--delete is incompatible with --all, --mirror and --tags");
|
||
|
if (deleterefs && argc < 2)
|
||
|
die("--delete doesn't make sense without any refs");
|
||
|
|
||
17 years ago
|
if (tags)
|
||
|
add_refspec("refs/tags/*");
|
||
|
|
||
|
if (argc > 0) {
|
||
|
repo = argv[0];
|
||
|
set_refspecs(argv + 1, argc - 1);
|
||
19 years ago
|
}
|
||
18 years ago
|
|
||
17 years ago
|
rc = do_push(repo, flags);
|
||
|
if (rc == -1)
|
||
17 years ago
|
usage_with_options(push_usage, options);
|
||
17 years ago
|
else
|
||
|
return rc;
|
||
19 years ago
|
}
|