Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 15:51:05 +08:00
|
|
|
#include "builtin.h"
|
2008-02-29 09:45:45 +08:00
|
|
|
#include "parse-options.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "remote.h"
|
2008-07-22 02:03:49 +08:00
|
|
|
#include "string-list.h"
|
2008-02-29 09:45:45 +08:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "refs.h"
|
|
|
|
|
|
|
|
static const char * const builtin_remote_usage[] = {
|
2008-11-17 19:15:49 +08:00
|
|
|
"git remote [-v | --verbose]",
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
"git remote add [-t <branch>] [-m <master>] [-f] [--mirror=<fetch|push>] <name> <url>",
|
2008-11-04 02:26:18 +08:00
|
|
|
"git remote rename <old> <new>",
|
2008-02-29 09:45:45 +08:00
|
|
|
"git remote rm <name>",
|
2009-11-21 07:43:13 +08:00
|
|
|
"git remote set-head <name> (-a | -d | <branch>)",
|
|
|
|
"git remote [-v | --verbose] show [-n] <name>",
|
2008-11-17 19:15:49 +08:00
|
|
|
"git remote prune [-n | --dry-run] <name>",
|
2010-10-09 01:31:17 +08:00
|
|
|
"git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]",
|
2010-05-20 02:38:50 +08:00
|
|
|
"git remote set-branches <name> [--add] <branch>...",
|
2010-01-19 01:18:02 +08:00
|
|
|
"git remote set-url <name> <newurl> [<oldurl>]",
|
|
|
|
"git remote set-url --add <name> <newurl>",
|
|
|
|
"git remote set-url --delete <name> <url>",
|
2008-02-29 09:45:45 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
static const char * const builtin_remote_add_usage[] = {
|
|
|
|
"git remote add [<options>] <name> <url>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const builtin_remote_rename_usage[] = {
|
|
|
|
"git remote rename <old> <new>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const builtin_remote_rm_usage[] = {
|
|
|
|
"git remote rm <name>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const builtin_remote_sethead_usage[] = {
|
|
|
|
"git remote set-head <name> (-a | -d | <branch>])",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-05-20 02:38:50 +08:00
|
|
|
static const char * const builtin_remote_setbranches_usage[] = {
|
|
|
|
"git remote set-branches <name> <branch>...",
|
|
|
|
"git remote set-branches --add <name> <branch>...",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
static const char * const builtin_remote_show_usage[] = {
|
|
|
|
"git remote show [<options>] <name>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const builtin_remote_prune_usage[] = {
|
|
|
|
"git remote prune [<options>] <name>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const builtin_remote_update_usage[] = {
|
|
|
|
"git remote update [<options>] [<group> | <remote>]...",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-01-19 01:18:02 +08:00
|
|
|
static const char * const builtin_remote_seturl_usage[] = {
|
|
|
|
"git remote set-url [--push] <name> <newurl> [<oldurl>]",
|
|
|
|
"git remote set-url --add <name> <newurl>",
|
|
|
|
"git remote set-url --delete <name> <url>",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-02-25 16:32:24 +08:00
|
|
|
#define GET_REF_STATES (1<<0)
|
|
|
|
#define GET_HEAD_NAMES (1<<1)
|
2009-02-25 16:32:28 +08:00
|
|
|
#define GET_PUSH_REF_STATES (1<<2)
|
2009-02-25 16:32:24 +08:00
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
static int verbose;
|
|
|
|
|
2008-04-09 23:15:51 +08:00
|
|
|
static int show_all(void);
|
2009-04-03 17:02:37 +08:00
|
|
|
static int prune_remote(const char *remote, int dry_run);
|
2008-04-09 23:15:51 +08:00
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
static inline int postfixcmp(const char *string, const char *postfix)
|
|
|
|
{
|
|
|
|
int len1 = strlen(string), len2 = strlen(postfix);
|
|
|
|
if (len1 < len2)
|
|
|
|
return 1;
|
|
|
|
return strcmp(string + len1 - len2, postfix);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fetch_remote(const char *name)
|
|
|
|
{
|
2008-11-18 19:04:02 +08:00
|
|
|
const char *argv[] = { "fetch", name, NULL, NULL };
|
|
|
|
if (verbose) {
|
|
|
|
argv[1] = "-v";
|
|
|
|
argv[2] = name;
|
|
|
|
}
|
2008-03-09 20:37:55 +08:00
|
|
|
printf("Updating %s\n", name);
|
2008-02-29 09:45:45 +08:00
|
|
|
if (run_command_v_opt(argv, RUN_GIT_CMD))
|
|
|
|
return error("Could not fetch %s", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:31:31 +08:00
|
|
|
enum {
|
|
|
|
TAGS_UNSET = 0,
|
|
|
|
TAGS_DEFAULT = 1,
|
|
|
|
TAGS_SET = 2
|
|
|
|
};
|
|
|
|
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
#define MIRROR_NONE 0
|
|
|
|
#define MIRROR_FETCH 1
|
|
|
|
#define MIRROR_PUSH 2
|
|
|
|
#define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
|
|
|
|
|
2010-05-20 02:38:50 +08:00
|
|
|
static int add_branch(const char *key, const char *branchname,
|
|
|
|
const char *remotename, int mirror, struct strbuf *tmp)
|
|
|
|
{
|
|
|
|
strbuf_reset(tmp);
|
|
|
|
strbuf_addch(tmp, '+');
|
|
|
|
if (mirror)
|
|
|
|
strbuf_addf(tmp, "refs/%s:refs/%s",
|
|
|
|
branchname, branchname);
|
|
|
|
else
|
|
|
|
strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
|
|
|
|
branchname, remotename, branchname);
|
|
|
|
return git_config_set_multivar(key, tmp->buf, "^$", 0);
|
|
|
|
}
|
|
|
|
|
2011-03-31 03:53:39 +08:00
|
|
|
static const char mirror_advice[] =
|
|
|
|
"--mirror is dangerous and deprecated; please\n"
|
|
|
|
"\t use --mirror=fetch or --mirror=push instead";
|
|
|
|
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
|
|
|
|
{
|
|
|
|
unsigned *mirror = opt->value;
|
|
|
|
if (not)
|
|
|
|
*mirror = MIRROR_NONE;
|
2011-03-31 03:53:39 +08:00
|
|
|
else if (!arg) {
|
|
|
|
warning("%s", mirror_advice);
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
*mirror = MIRROR_BOTH;
|
2011-03-31 03:53:39 +08:00
|
|
|
}
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
else if (!strcmp(arg, "fetch"))
|
|
|
|
*mirror = MIRROR_FETCH;
|
|
|
|
else if (!strcmp(arg, "push"))
|
|
|
|
*mirror = MIRROR_PUSH;
|
|
|
|
else
|
|
|
|
return error("unknown mirror argument: %s", arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
static int add(int argc, const char **argv)
|
|
|
|
{
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
int fetch = 0, fetch_tags = TAGS_DEFAULT;
|
|
|
|
unsigned mirror = MIRROR_NONE;
|
2010-07-05 03:46:19 +08:00
|
|
|
struct string_list track = STRING_LIST_INIT_NODUP;
|
2008-02-29 09:45:45 +08:00
|
|
|
const char *master = NULL;
|
|
|
|
struct remote *remote;
|
2008-10-10 03:12:12 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
|
2008-02-29 09:45:45 +08:00
|
|
|
const char *name, *url;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
|
2010-04-20 07:31:31 +08:00
|
|
|
OPT_SET_INT(0, "tags", &fetch_tags,
|
|
|
|
"import all tags and associated objects when fetching",
|
|
|
|
TAGS_SET),
|
|
|
|
OPT_SET_INT(0, NULL, &fetch_tags,
|
|
|
|
"or do not fetch any tag at all (--no-tags)", TAGS_UNSET),
|
2011-06-09 23:55:59 +08:00
|
|
|
OPT_STRING_LIST('t', "track", &track, "branch",
|
|
|
|
"branch(es) to track"),
|
2008-02-29 09:45:45 +08:00
|
|
|
OPT_STRING('m', "master", &master, "branch", "master branch"),
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
{ OPTION_CALLBACK, 0, "mirror", &mirror, "push|fetch",
|
|
|
|
"set up remote as a mirror to push to or fetch from",
|
|
|
|
PARSE_OPT_OPTARG, parse_mirror_opt },
|
2008-02-29 09:45:45 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
|
2009-05-24 02:53:12 +08:00
|
|
|
0);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
if (argc < 2)
|
2009-11-21 07:43:13 +08:00
|
|
|
usage_with_options(builtin_remote_add_usage, options);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
remote: disallow some nonsensical option combinations
It doesn't make sense to use "-m" on a mirror, since "-m"
sets up the HEAD symref in the remotes namespace, but with
mirror, we are by definition not using a remotes namespace.
Similarly, it does not make much sense to specify refspecs
with --mirror. For a mirror you plan to push to, those
refspecs will be ignored. For a mirror you are fetching
from, there is no point in mirroring, since the refspec
specifies everything you want to grab.
There is one case where "--mirror -t <X>" would be useful.
Because <X> is used as-is in the refspec, and because we
append it to to refs/, you could mirror a subset of the
hierarchy by doing:
git remote add --mirror -t 'tags/*'
But using anything besides a single branch as an argument to
"-t" is not documented and only happens to work, so closing
it off is not a serious regression.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:52:52 +08:00
|
|
|
if (mirror && master)
|
|
|
|
die("specifying a master branch makes no sense with --mirror");
|
2011-05-26 23:11:00 +08:00
|
|
|
if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
|
|
|
|
die("specifying branches to track makes sense only with fetch mirrors");
|
remote: disallow some nonsensical option combinations
It doesn't make sense to use "-m" on a mirror, since "-m"
sets up the HEAD symref in the remotes namespace, but with
mirror, we are by definition not using a remotes namespace.
Similarly, it does not make much sense to specify refspecs
with --mirror. For a mirror you plan to push to, those
refspecs will be ignored. For a mirror you are fetching
from, there is no point in mirroring, since the refspec
specifies everything you want to grab.
There is one case where "--mirror -t <X>" would be useful.
Because <X> is used as-is in the refspec, and because we
append it to to refs/, you could mirror a subset of the
hierarchy by doing:
git remote add --mirror -t 'tags/*'
But using anything besides a single branch as an argument to
"-t" is not documented and only happens to work, so closing
it off is not a serious regression.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:52:52 +08:00
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
name = argv[0];
|
|
|
|
url = argv[1];
|
|
|
|
|
|
|
|
remote = remote_get(name);
|
|
|
|
if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
|
|
|
|
remote->fetch_refspec_nr))
|
|
|
|
die("remote %s already exists.", name);
|
|
|
|
|
2008-04-13 17:56:54 +08:00
|
|
|
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
|
|
|
|
if (!valid_fetch_refspec(buf2.buf))
|
|
|
|
die("'%s' is not a valid remote name", name);
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
strbuf_addf(&buf, "remote.%s.url", name);
|
|
|
|
if (git_config_set(buf.buf, url))
|
|
|
|
return 1;
|
|
|
|
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
if (!mirror || mirror & MIRROR_FETCH) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.fetch", name);
|
|
|
|
if (track.nr == 0)
|
|
|
|
string_list_append(&track, "*");
|
|
|
|
for (i = 0; i < track.nr; i++) {
|
|
|
|
if (add_branch(buf.buf, track.items[i].string,
|
|
|
|
name, mirror, &buf2))
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
remote: separate the concept of push and fetch mirrors
git-remote currently has one option, "--mirror", which sets
up mirror configuration which can be used for either
fetching or pushing. It looks like this:
[remote "mirror"]
url = wherever
fetch = +refs/*:refs/*
mirror = true
However, a remote like this can be dangerous and confusing.
Specifically:
1. If you issue the wrong command, it can be devastating.
You are not likely to "push" when you meant to "fetch",
but "git remote update" will try to fetch it, even if
you intended the remote only for pushing. In either
case, the results can be quite destructive. An
unintended push will overwrite or delete remote refs,
and an unintended fetch can overwrite local branches.
2. The tracking setup code can produce confusing results.
The fetch refspec above means that "git checkout -b new
master" will consider refs/heads/master to come from
the remote "mirror", even if you only ever intend to
push to the mirror. It will set up the "new" branch to
track mirror's refs/heads/master.
3. The push code tries to opportunistically update
tracking branches. If you "git push mirror foo:bar",
it will see that we are updating mirror's
refs/heads/bar, which corresponds to our local
refs/heads/bar, and will update our local branch.
To solve this, we split the concept into "push mirrors" and
"fetch mirrors". Push mirrors set only remote.*.mirror,
solving (2) and (3), and making an accidental fetch write
only into FETCH_HEAD. Fetch mirrors set only the fetch
refspec, meaning an accidental push will not force-overwrite
or delete refs on the remote end.
The new syntax is "--mirror=<fetch|push>". For
compatibility, we keep "--mirror" as-is, setting up both
types simultaneously.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-31 03:53:19 +08:00
|
|
|
if (mirror & MIRROR_PUSH) {
|
2008-04-17 19:17:20 +08:00
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.mirror", name);
|
2008-08-03 03:38:56 +08:00
|
|
|
if (git_config_set(buf.buf, "true"))
|
2008-04-17 19:17:20 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-04-20 07:31:31 +08:00
|
|
|
if (fetch_tags != TAGS_DEFAULT) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.tagopt", name);
|
|
|
|
if (git_config_set(buf.buf,
|
|
|
|
fetch_tags == TAGS_SET ? "--tags" : "--no-tags"))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
if (fetch && fetch_remote(name))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (master) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
|
|
|
|
|
|
|
|
strbuf_reset(&buf2);
|
|
|
|
strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
|
|
|
|
|
|
|
|
if (create_symref(buf.buf, buf2.buf, "remote add"))
|
|
|
|
return error("Could not setup master '%s'", master);
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&buf2);
|
2008-07-22 02:03:49 +08:00
|
|
|
string_list_clear(&track, 0);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct branch_info {
|
2009-02-25 16:32:21 +08:00
|
|
|
char *remote_name;
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list merge;
|
2009-02-25 16:32:27 +08:00
|
|
|
int rebase;
|
2008-02-29 09:45:45 +08:00
|
|
|
};
|
|
|
|
|
2008-07-22 02:03:49 +08:00
|
|
|
static struct string_list branch_list;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2008-07-18 12:30:33 +08:00
|
|
|
static const char *abbrev_ref(const char *name, const char *prefix)
|
|
|
|
{
|
|
|
|
const char *abbrev = skip_prefix(name, prefix);
|
|
|
|
if (abbrev)
|
|
|
|
return abbrev;
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
|
|
|
|
|
2008-05-15 01:46:53 +08:00
|
|
|
static int config_read_branches(const char *key, const char *value, void *cb)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
|
|
|
if (!prefixcmp(key, "branch.")) {
|
2009-02-25 16:32:27 +08:00
|
|
|
const char *orig_key = key;
|
2008-02-29 09:45:45 +08:00
|
|
|
char *name;
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item;
|
2008-02-29 09:45:45 +08:00
|
|
|
struct branch_info *info;
|
2009-02-25 16:32:27 +08:00
|
|
|
enum { REMOTE, MERGE, REBASE } type;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
key += 7;
|
|
|
|
if (!postfixcmp(key, ".remote")) {
|
|
|
|
name = xstrndup(key, strlen(key) - 7);
|
|
|
|
type = REMOTE;
|
|
|
|
} else if (!postfixcmp(key, ".merge")) {
|
|
|
|
name = xstrndup(key, strlen(key) - 6);
|
|
|
|
type = MERGE;
|
2009-02-25 16:32:27 +08:00
|
|
|
} else if (!postfixcmp(key, ".rebase")) {
|
|
|
|
name = xstrndup(key, strlen(key) - 7);
|
|
|
|
type = REBASE;
|
2008-02-29 09:45:45 +08:00
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
2010-06-26 07:41:35 +08:00
|
|
|
item = string_list_insert(&branch_list, name);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
if (!item->util)
|
|
|
|
item->util = xcalloc(sizeof(struct branch_info), 1);
|
|
|
|
info = item->util;
|
|
|
|
if (type == REMOTE) {
|
2009-02-25 16:32:21 +08:00
|
|
|
if (info->remote_name)
|
2009-02-25 16:32:27 +08:00
|
|
|
warning("more than one %s", orig_key);
|
2009-02-25 16:32:21 +08:00
|
|
|
info->remote_name = xstrdup(value);
|
2009-02-25 16:32:27 +08:00
|
|
|
} else if (type == MERGE) {
|
2008-02-29 09:45:45 +08:00
|
|
|
char *space = strchr(value, ' ');
|
2008-07-18 12:30:33 +08:00
|
|
|
value = abbrev_branch(value);
|
2008-02-29 09:45:45 +08:00
|
|
|
while (space) {
|
|
|
|
char *merge;
|
|
|
|
merge = xstrndup(value, space - value);
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&info->merge, merge);
|
2008-07-18 12:30:33 +08:00
|
|
|
value = abbrev_branch(space + 1);
|
2008-02-29 09:45:45 +08:00
|
|
|
space = strchr(value, ' ');
|
|
|
|
}
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&info->merge, xstrdup(value));
|
2009-02-25 16:32:27 +08:00
|
|
|
} else
|
|
|
|
info->rebase = git_config_bool(orig_key, value);
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_branches(void)
|
|
|
|
{
|
|
|
|
if (branch_list.nr)
|
|
|
|
return;
|
2008-05-15 01:46:53 +08:00
|
|
|
git_config(config_read_branches, NULL);
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ref_states {
|
|
|
|
struct remote *remote;
|
2009-02-25 16:32:28 +08:00
|
|
|
struct string_list new, stale, tracked, heads, push;
|
2009-02-25 16:32:27 +08:00
|
|
|
int queried;
|
2008-02-29 09:45:45 +08:00
|
|
|
};
|
|
|
|
|
2009-02-25 16:32:21 +08:00
|
|
|
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
|
|
|
struct ref *fetch_map = NULL, **tail = &fetch_map;
|
2009-11-10 13:03:31 +08:00
|
|
|
struct ref *ref, *stale_refs;
|
2008-02-29 09:45:45 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < states->remote->fetch_refspec_nr; i++)
|
2009-02-25 16:32:21 +08:00
|
|
|
if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
|
2008-02-29 09:45:45 +08:00
|
|
|
die("Could not get fetch map for refspec %s",
|
|
|
|
states->remote->fetch_refspec[i]);
|
|
|
|
|
2009-12-01 07:57:27 +08:00
|
|
|
states->new.strdup_strings = 1;
|
|
|
|
states->tracked.strdup_strings = 1;
|
|
|
|
states->stale.strdup_strings = 1;
|
2008-02-29 09:45:45 +08:00
|
|
|
for (ref = fetch_map; ref; ref = ref->next) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&states->new, abbrev_branch(ref->name));
|
2009-02-25 16:32:20 +08:00
|
|
|
else
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&states->tracked, abbrev_branch(ref->name));
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
2009-11-10 13:03:31 +08:00
|
|
|
stale_refs = get_stale_heads(states->remote, fetch_map);
|
|
|
|
for (ref = stale_refs; ref; ref = ref->next) {
|
|
|
|
struct string_list_item *item =
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&states->stale, abbrev_branch(ref->name));
|
2009-11-10 13:03:31 +08:00
|
|
|
item->util = xstrdup(ref->name);
|
|
|
|
}
|
|
|
|
free_refs(stale_refs);
|
2008-02-29 09:45:45 +08:00
|
|
|
free_refs(fetch_map);
|
|
|
|
|
2009-02-25 16:32:23 +08:00
|
|
|
sort_string_list(&states->new);
|
|
|
|
sort_string_list(&states->tracked);
|
2008-07-22 02:03:49 +08:00
|
|
|
sort_string_list(&states->stale);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:28 +08:00
|
|
|
struct push_info {
|
|
|
|
char *dest;
|
|
|
|
int forced;
|
|
|
|
enum {
|
|
|
|
PUSH_STATUS_CREATE = 0,
|
|
|
|
PUSH_STATUS_DELETE,
|
|
|
|
PUSH_STATUS_UPTODATE,
|
|
|
|
PUSH_STATUS_FASTFORWARD,
|
|
|
|
PUSH_STATUS_OUTOFDATE,
|
2010-05-14 17:31:35 +08:00
|
|
|
PUSH_STATUS_NOTQUERIED
|
2009-02-25 16:32:28 +08:00
|
|
|
} status;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int get_push_ref_states(const struct ref *remote_refs,
|
|
|
|
struct ref_states *states)
|
|
|
|
{
|
|
|
|
struct remote *remote = states->remote;
|
2009-05-31 22:26:48 +08:00
|
|
|
struct ref *ref, *local_refs, *push_map;
|
2009-02-25 16:32:28 +08:00
|
|
|
if (remote->mirror)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
local_refs = get_local_heads();
|
2009-05-28 04:13:43 +08:00
|
|
|
push_map = copy_ref_list(remote_refs);
|
2009-02-25 16:32:28 +08:00
|
|
|
|
2009-05-31 22:26:48 +08:00
|
|
|
match_refs(local_refs, &push_map, remote->push_refspec_nr,
|
2009-02-25 16:32:28 +08:00
|
|
|
remote->push_refspec, MATCH_REFS_NONE);
|
|
|
|
|
|
|
|
states->push.strdup_strings = 1;
|
|
|
|
for (ref = push_map; ref; ref = ref->next) {
|
|
|
|
struct string_list_item *item;
|
|
|
|
struct push_info *info;
|
|
|
|
|
|
|
|
if (!ref->peer_ref)
|
|
|
|
continue;
|
|
|
|
hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
|
|
|
|
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(&states->push,
|
|
|
|
abbrev_branch(ref->peer_ref->name));
|
2009-02-25 16:32:28 +08:00
|
|
|
item->util = xcalloc(sizeof(struct push_info), 1);
|
|
|
|
info = item->util;
|
|
|
|
info->forced = ref->force;
|
|
|
|
info->dest = xstrdup(abbrev_branch(ref->name));
|
|
|
|
|
|
|
|
if (is_null_sha1(ref->new_sha1)) {
|
|
|
|
info->status = PUSH_STATUS_DELETE;
|
|
|
|
} else if (!hashcmp(ref->old_sha1, ref->new_sha1))
|
|
|
|
info->status = PUSH_STATUS_UPTODATE;
|
|
|
|
else if (is_null_sha1(ref->old_sha1))
|
|
|
|
info->status = PUSH_STATUS_CREATE;
|
|
|
|
else if (has_sha1_file(ref->old_sha1) &&
|
|
|
|
ref_newer(ref->new_sha1, ref->old_sha1))
|
|
|
|
info->status = PUSH_STATUS_FASTFORWARD;
|
|
|
|
else
|
|
|
|
info->status = PUSH_STATUS_OUTOFDATE;
|
|
|
|
}
|
|
|
|
free_refs(local_refs);
|
|
|
|
free_refs(push_map);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_push_ref_states_noquery(struct ref_states *states)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct remote *remote = states->remote;
|
|
|
|
struct string_list_item *item;
|
|
|
|
struct push_info *info;
|
|
|
|
|
|
|
|
if (remote->mirror)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
states->push.strdup_strings = 1;
|
|
|
|
if (!remote->push_refspec_nr) {
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(&states->push, "(matching)");
|
2009-02-25 16:32:28 +08:00
|
|
|
info = item->util = xcalloc(sizeof(struct push_info), 1);
|
|
|
|
info->status = PUSH_STATUS_NOTQUERIED;
|
|
|
|
info->dest = xstrdup(item->string);
|
|
|
|
}
|
|
|
|
for (i = 0; i < remote->push_refspec_nr; i++) {
|
|
|
|
struct refspec *spec = remote->push + i;
|
|
|
|
if (spec->matching)
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(&states->push, "(matching)");
|
2009-03-08 16:12:33 +08:00
|
|
|
else if (strlen(spec->src))
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(&states->push, spec->src);
|
2009-02-25 16:32:28 +08:00
|
|
|
else
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(&states->push, "(delete)");
|
2009-02-25 16:32:28 +08:00
|
|
|
|
|
|
|
info = item->util = xcalloc(sizeof(struct push_info), 1);
|
|
|
|
info->forced = spec->force;
|
|
|
|
info->status = PUSH_STATUS_NOTQUERIED;
|
2009-03-08 16:12:33 +08:00
|
|
|
info->dest = xstrdup(spec->dst ? spec->dst : item->string);
|
2009-02-25 16:32:28 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:24 +08:00
|
|
|
static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
|
|
|
|
{
|
|
|
|
struct ref *ref, *matches;
|
|
|
|
struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
|
|
|
|
struct refspec refspec;
|
|
|
|
|
|
|
|
refspec.force = 0;
|
|
|
|
refspec.pattern = 1;
|
2009-03-08 16:12:33 +08:00
|
|
|
refspec.src = refspec.dst = "refs/heads/*";
|
2009-02-25 16:32:24 +08:00
|
|
|
states->heads.strdup_strings = 1;
|
|
|
|
get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
|
|
|
|
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
|
|
|
|
fetch_map, 1);
|
2009-09-01 13:35:10 +08:00
|
|
|
for (ref = matches; ref; ref = ref->next)
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&states->heads, abbrev_branch(ref->name));
|
2009-02-25 16:32:24 +08:00
|
|
|
|
|
|
|
free_refs(fetch_map);
|
|
|
|
free_refs(matches);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 12:28:04 +08:00
|
|
|
struct known_remote {
|
|
|
|
struct known_remote *next;
|
|
|
|
struct remote *remote;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct known_remotes {
|
|
|
|
struct remote *to_delete;
|
|
|
|
struct known_remote *list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int add_known_remote(struct remote *remote, void *cb_data)
|
|
|
|
{
|
|
|
|
struct known_remotes *all = cb_data;
|
|
|
|
struct known_remote *r;
|
|
|
|
|
|
|
|
if (!strcmp(all->to_delete->name, remote->name))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = xmalloc(sizeof(*r));
|
|
|
|
r->remote = remote;
|
|
|
|
r->next = all->list;
|
|
|
|
all->list = r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
struct branches_for_remote {
|
2008-06-01 12:28:04 +08:00
|
|
|
struct remote *remote;
|
2009-02-05 00:06:07 +08:00
|
|
|
struct string_list *branches, *skipped;
|
2008-06-01 12:28:04 +08:00
|
|
|
struct known_remotes *keep;
|
2008-02-29 09:45:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int add_branch_for_removal(const char *refname,
|
|
|
|
const unsigned char *sha1, int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct branches_for_remote *branches = cb_data;
|
2008-06-01 12:28:04 +08:00
|
|
|
struct refspec refspec;
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item;
|
2008-06-01 12:28:04 +08:00
|
|
|
struct known_remote *kr;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2008-06-01 12:28:04 +08:00
|
|
|
memset(&refspec, 0, sizeof(refspec));
|
|
|
|
refspec.dst = (char *)refname;
|
|
|
|
if (remote_find_tracking(branches->remote, &refspec))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* don't delete a branch if another remote also uses it */
|
|
|
|
for (kr = branches->keep->list; kr; kr = kr->next) {
|
|
|
|
memset(&refspec, 0, sizeof(refspec));
|
|
|
|
refspec.dst = (char *)refname;
|
|
|
|
if (!remote_find_tracking(kr->remote, &refspec))
|
|
|
|
return 0;
|
|
|
|
}
|
2008-03-09 06:40:42 +08:00
|
|
|
|
2010-11-02 23:31:25 +08:00
|
|
|
/* don't delete non-remote-tracking refs */
|
2009-02-05 00:06:07 +08:00
|
|
|
if (prefixcmp(refname, "refs/remotes")) {
|
|
|
|
/* advise user how to delete local branches */
|
|
|
|
if (!prefixcmp(refname, "refs/heads/"))
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(branches->skipped,
|
|
|
|
abbrev_branch(refname));
|
2009-02-05 00:06:07 +08:00
|
|
|
/* silently skip over other non-remote refs */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 12:28:04 +08:00
|
|
|
/* make sure that symrefs are deleted */
|
|
|
|
if (flags & REF_ISSYMREF)
|
2008-11-11 05:07:52 +08:00
|
|
|
return unlink(git_path("%s", refname));
|
2008-03-09 06:40:42 +08:00
|
|
|
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(branches->branches, refname);
|
2008-06-01 12:28:04 +08:00
|
|
|
item->util = xmalloc(20);
|
|
|
|
hashcpy(item->util, sha1);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 02:26:18 +08:00
|
|
|
struct rename_info {
|
|
|
|
const char *old;
|
|
|
|
const char *new;
|
|
|
|
struct string_list *remote_branches;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int read_remote_branches(const char *refname,
|
|
|
|
const unsigned char *sha1, int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct rename_info *rename = cb_data;
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
struct string_list_item *item;
|
|
|
|
int flag;
|
|
|
|
unsigned char orig_sha1[20];
|
|
|
|
const char *symref;
|
|
|
|
|
2011-09-02 08:50:34 +08:00
|
|
|
strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
|
2009-09-01 13:35:10 +08:00
|
|
|
if (!prefixcmp(refname, buf.buf)) {
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(rename->remote_branches, xstrdup(refname));
|
2008-11-04 02:26:18 +08:00
|
|
|
symref = resolve_ref(refname, orig_sha1, 1, &flag);
|
|
|
|
if (flag & REF_ISSYMREF)
|
|
|
|
item->util = xstrdup(symref);
|
|
|
|
else
|
|
|
|
item->util = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-11 04:43:01 +08:00
|
|
|
static int migrate_file(struct remote *remote)
|
|
|
|
{
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
int i;
|
|
|
|
char *path = NULL;
|
|
|
|
|
|
|
|
strbuf_addf(&buf, "remote.%s.url", remote->name);
|
|
|
|
for (i = 0; i < remote->url_nr; i++)
|
|
|
|
if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
|
|
|
|
return error("Could not append '%s' to '%s'",
|
|
|
|
remote->url[i], buf.buf);
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.push", remote->name);
|
|
|
|
for (i = 0; i < remote->push_refspec_nr; i++)
|
|
|
|
if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
|
|
|
|
return error("Could not append '%s' to '%s'",
|
|
|
|
remote->push_refspec[i], buf.buf);
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.fetch", remote->name);
|
|
|
|
for (i = 0; i < remote->fetch_refspec_nr; i++)
|
|
|
|
if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
|
|
|
|
return error("Could not append '%s' to '%s'",
|
|
|
|
remote->fetch_refspec[i], buf.buf);
|
|
|
|
if (remote->origin == REMOTE_REMOTES)
|
|
|
|
path = git_path("remotes/%s", remote->name);
|
|
|
|
else if (remote->origin == REMOTE_BRANCHES)
|
|
|
|
path = git_path("branches/%s", remote->name);
|
2009-04-30 05:22:56 +08:00
|
|
|
if (path)
|
|
|
|
unlink_or_warn(path);
|
2008-11-11 04:43:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-04 02:26:18 +08:00
|
|
|
static int mv(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct remote *oldremote, *newremote;
|
2011-09-02 08:50:33 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
|
|
|
|
old_remote_context = STRBUF_INIT;
|
2010-07-05 03:46:19 +08:00
|
|
|
struct string_list remote_branches = STRING_LIST_INIT_NODUP;
|
2008-11-04 02:26:18 +08:00
|
|
|
struct rename_info rename;
|
2011-09-11 03:39:23 +08:00
|
|
|
int i, refspec_updated = 0;
|
2008-11-04 02:26:18 +08:00
|
|
|
|
|
|
|
if (argc != 3)
|
2009-11-21 07:43:13 +08:00
|
|
|
usage_with_options(builtin_remote_rename_usage, options);
|
2008-11-04 02:26:18 +08:00
|
|
|
|
|
|
|
rename.old = argv[1];
|
|
|
|
rename.new = argv[2];
|
|
|
|
rename.remote_branches = &remote_branches;
|
|
|
|
|
|
|
|
oldremote = remote_get(rename.old);
|
|
|
|
if (!oldremote)
|
|
|
|
die("No such remote: %s", rename.old);
|
|
|
|
|
2008-11-11 04:43:01 +08:00
|
|
|
if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
|
|
|
|
return migrate_file(oldremote);
|
|
|
|
|
2008-11-04 02:26:18 +08:00
|
|
|
newremote = remote_get(rename.new);
|
|
|
|
if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
|
|
|
|
die("remote %s already exists.", rename.new);
|
|
|
|
|
|
|
|
strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
|
|
|
|
if (!valid_fetch_refspec(buf.buf))
|
|
|
|
die("'%s' is not a valid remote name", rename.new);
|
|
|
|
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s", rename.old);
|
|
|
|
strbuf_addf(&buf2, "remote.%s", rename.new);
|
|
|
|
if (git_config_rename_section(buf.buf, buf2.buf) < 1)
|
|
|
|
return error("Could not rename config section '%s' to '%s'",
|
|
|
|
buf.buf, buf2.buf);
|
|
|
|
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.fetch", rename.new);
|
|
|
|
if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
|
|
|
|
return error("Could not remove config section '%s'", buf.buf);
|
2011-09-02 08:50:33 +08:00
|
|
|
strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
|
2008-11-04 02:26:18 +08:00
|
|
|
for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
strbuf_reset(&buf2);
|
|
|
|
strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
|
2011-09-02 08:50:33 +08:00
|
|
|
ptr = strstr(buf2.buf, old_remote_context.buf);
|
2011-09-11 03:39:23 +08:00
|
|
|
if (ptr) {
|
|
|
|
refspec_updated = 1;
|
2011-09-02 08:50:33 +08:00
|
|
|
strbuf_splice(&buf2,
|
|
|
|
ptr-buf2.buf + strlen(":refs/remotes/"),
|
|
|
|
strlen(rename.old), rename.new,
|
|
|
|
strlen(rename.new));
|
2011-09-11 03:39:23 +08:00
|
|
|
} else
|
2011-09-03 23:26:59 +08:00
|
|
|
warning("Not updating non-default fetch respec\n"
|
|
|
|
"\t%s\n"
|
|
|
|
"\tPlease update the configuration manually if necessary.",
|
|
|
|
buf2.buf);
|
|
|
|
|
2008-11-04 02:26:18 +08:00
|
|
|
if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
|
|
|
|
return error("Could not append '%s'", buf.buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
read_branches();
|
|
|
|
for (i = 0; i < branch_list.nr; i++) {
|
|
|
|
struct string_list_item *item = branch_list.items + i;
|
|
|
|
struct branch_info *info = item->util;
|
2009-02-25 16:32:21 +08:00
|
|
|
if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
|
2008-11-04 02:26:18 +08:00
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "branch.%s.remote", item->string);
|
|
|
|
if (git_config_set(buf.buf, rename.new)) {
|
|
|
|
return error("Could not set '%s'", buf.buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-11 03:39:23 +08:00
|
|
|
if (!refspec_updated)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-04 02:26:18 +08:00
|
|
|
/*
|
|
|
|
* First remove symrefs, then rename the rest, finally create
|
|
|
|
* the new symrefs.
|
|
|
|
*/
|
|
|
|
for_each_ref(read_remote_branches, &rename);
|
|
|
|
for (i = 0; i < remote_branches.nr; i++) {
|
|
|
|
struct string_list_item *item = remote_branches.items + i;
|
|
|
|
int flag = 0;
|
|
|
|
unsigned char sha1[20];
|
|
|
|
|
2009-03-08 04:02:10 +08:00
|
|
|
resolve_ref(item->string, sha1, 1, &flag);
|
2008-11-04 02:26:18 +08:00
|
|
|
if (!(flag & REF_ISSYMREF))
|
|
|
|
continue;
|
|
|
|
if (delete_ref(item->string, NULL, REF_NODEREF))
|
|
|
|
die("deleting '%s' failed", item->string);
|
|
|
|
}
|
|
|
|
for (i = 0; i < remote_branches.nr; i++) {
|
|
|
|
struct string_list_item *item = remote_branches.items + i;
|
|
|
|
|
|
|
|
if (item->util)
|
|
|
|
continue;
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addstr(&buf, item->string);
|
|
|
|
strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
|
|
|
|
rename.new, strlen(rename.new));
|
|
|
|
strbuf_reset(&buf2);
|
|
|
|
strbuf_addf(&buf2, "remote: renamed %s to %s",
|
|
|
|
item->string, buf.buf);
|
|
|
|
if (rename_ref(item->string, buf.buf, buf2.buf))
|
|
|
|
die("renaming '%s' failed", item->string);
|
|
|
|
}
|
|
|
|
for (i = 0; i < remote_branches.nr; i++) {
|
|
|
|
struct string_list_item *item = remote_branches.items + i;
|
|
|
|
|
|
|
|
if (!item->util)
|
|
|
|
continue;
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addstr(&buf, item->string);
|
|
|
|
strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
|
|
|
|
rename.new, strlen(rename.new));
|
|
|
|
strbuf_reset(&buf2);
|
|
|
|
strbuf_addstr(&buf2, item->util);
|
|
|
|
strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
|
|
|
|
rename.new, strlen(rename.new));
|
|
|
|
strbuf_reset(&buf3);
|
|
|
|
strbuf_addf(&buf3, "remote: renamed %s to %s",
|
|
|
|
item->string, buf.buf);
|
|
|
|
if (create_symref(buf.buf, buf2.buf, buf3.buf))
|
|
|
|
die("creating '%s' failed", buf.buf);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-22 02:03:49 +08:00
|
|
|
static int remove_branches(struct string_list *branches)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
|
|
|
int i, result = 0;
|
|
|
|
for (i = 0; i < branches->nr; i++) {
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item = branches->items + i;
|
|
|
|
const char *refname = item->string;
|
2008-02-29 09:45:45 +08:00
|
|
|
unsigned char *sha1 = item->util;
|
|
|
|
|
2008-10-26 10:33:56 +08:00
|
|
|
if (delete_ref(refname, sha1, 0))
|
2008-02-29 09:45:45 +08:00
|
|
|
result |= error("Could not remove branch %s", refname);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rm(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct remote *remote;
|
2008-10-10 03:12:12 +08:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2008-06-01 12:28:04 +08:00
|
|
|
struct known_remotes known_remotes = { NULL, NULL };
|
2010-07-05 03:46:19 +08:00
|
|
|
struct string_list branches = STRING_LIST_INIT_DUP;
|
|
|
|
struct string_list skipped = STRING_LIST_INIT_DUP;
|
2010-05-14 17:31:33 +08:00
|
|
|
struct branches_for_remote cb_data;
|
2009-02-04 01:51:12 +08:00
|
|
|
int i, result;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2010-05-14 17:31:33 +08:00
|
|
|
memset(&cb_data, 0, sizeof(cb_data));
|
|
|
|
cb_data.branches = &branches;
|
|
|
|
cb_data.skipped = &skipped;
|
|
|
|
cb_data.keep = &known_remotes;
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
if (argc != 2)
|
2009-11-21 07:43:13 +08:00
|
|
|
usage_with_options(builtin_remote_rm_usage, options);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
remote = remote_get(argv[1]);
|
|
|
|
if (!remote)
|
|
|
|
die("No such remote: %s", argv[1]);
|
|
|
|
|
2008-06-01 12:28:04 +08:00
|
|
|
known_remotes.to_delete = remote;
|
|
|
|
for_each_remote(add_known_remote, &known_remotes);
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
strbuf_addf(&buf, "remote.%s", remote->name);
|
|
|
|
if (git_config_rename_section(buf.buf, NULL) < 1)
|
|
|
|
return error("Could not remove config section '%s'", buf.buf);
|
|
|
|
|
|
|
|
read_branches();
|
|
|
|
for (i = 0; i < branch_list.nr; i++) {
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item = branch_list.items + i;
|
2008-02-29 09:45:45 +08:00
|
|
|
struct branch_info *info = item->util;
|
2009-02-25 16:32:21 +08:00
|
|
|
if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
|
2008-02-29 09:45:45 +08:00
|
|
|
const char *keys[] = { "remote", "merge", NULL }, **k;
|
|
|
|
for (k = keys; *k; k++) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "branch.%s.%s",
|
2008-07-22 02:03:49 +08:00
|
|
|
item->string, *k);
|
2008-02-29 09:45:45 +08:00
|
|
|
if (git_config_set(buf.buf, NULL)) {
|
|
|
|
strbuf_release(&buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We cannot just pass a function to for_each_ref() which deletes
|
|
|
|
* the branches one by one, since for_each_ref() relies on cached
|
|
|
|
* refs, which are invalidated when deleting a branch.
|
|
|
|
*/
|
2008-06-01 12:28:04 +08:00
|
|
|
cb_data.remote = remote;
|
2009-02-04 01:51:12 +08:00
|
|
|
result = for_each_ref(add_branch_for_removal, &cb_data);
|
2008-02-29 09:45:45 +08:00
|
|
|
strbuf_release(&buf);
|
|
|
|
|
2009-02-04 01:51:12 +08:00
|
|
|
if (!result)
|
|
|
|
result = remove_branches(&branches);
|
2008-07-22 02:03:49 +08:00
|
|
|
string_list_clear(&branches, 1);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-02-05 00:06:07 +08:00
|
|
|
if (skipped.nr) {
|
|
|
|
fprintf(stderr, skipped.nr == 1 ?
|
2010-11-02 23:31:25 +08:00
|
|
|
"Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
|
2009-02-05 00:06:07 +08:00
|
|
|
"to delete it, use:\n" :
|
2010-11-02 23:31:25 +08:00
|
|
|
"Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
|
2009-02-05 00:06:07 +08:00
|
|
|
"to delete them, use:\n");
|
|
|
|
for (i = 0; i < skipped.nr; i++)
|
|
|
|
fprintf(stderr, " git branch -d %s\n",
|
|
|
|
skipped.items[i].string);
|
|
|
|
}
|
|
|
|
string_list_clear(&skipped, 0);
|
|
|
|
|
2009-02-04 01:51:12 +08:00
|
|
|
return result;
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static void clear_push_info(void *util, const char *string)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
2009-02-25 16:32:28 +08:00
|
|
|
struct push_info *info = util;
|
|
|
|
free(info->dest);
|
|
|
|
free(info);
|
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-02-25 16:32:19 +08:00
|
|
|
static void free_remote_ref_states(struct ref_states *states)
|
|
|
|
{
|
|
|
|
string_list_clear(&states->new, 0);
|
2009-12-01 07:57:27 +08:00
|
|
|
string_list_clear(&states->stale, 1);
|
2009-02-25 16:32:19 +08:00
|
|
|
string_list_clear(&states->tracked, 0);
|
2009-02-25 16:32:24 +08:00
|
|
|
string_list_clear(&states->heads, 0);
|
2009-02-25 16:32:28 +08:00
|
|
|
string_list_clear_func(&states->push, clear_push_info);
|
2009-02-25 16:32:19 +08:00
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-02-25 16:32:22 +08:00
|
|
|
static int append_ref_to_tracked_list(const char *refname,
|
|
|
|
const unsigned char *sha1, int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct ref_states *states = cb_data;
|
|
|
|
struct refspec refspec;
|
|
|
|
|
2009-02-25 16:32:23 +08:00
|
|
|
if (flags & REF_ISSYMREF)
|
|
|
|
return 0;
|
|
|
|
|
2009-02-25 16:32:22 +08:00
|
|
|
memset(&refspec, 0, sizeof(refspec));
|
|
|
|
refspec.dst = (char *)refname;
|
|
|
|
if (!remote_find_tracking(states->remote, &refspec))
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(&states->tracked, abbrev_branch(refspec.src));
|
2009-02-25 16:32:22 +08:00
|
|
|
|
|
|
|
return 0;
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
2008-06-10 22:51:21 +08:00
|
|
|
static int get_remote_ref_states(const char *name,
|
|
|
|
struct ref_states *states,
|
|
|
|
int query)
|
|
|
|
{
|
|
|
|
struct transport *transport;
|
2009-02-25 16:32:21 +08:00
|
|
|
const struct ref *remote_refs;
|
2008-06-10 22:51:21 +08:00
|
|
|
|
|
|
|
states->remote = remote_get(name);
|
|
|
|
if (!states->remote)
|
|
|
|
return error("No such remote: %s", name);
|
|
|
|
|
|
|
|
read_branches();
|
|
|
|
|
|
|
|
if (query) {
|
2009-06-26 05:21:35 +08:00
|
|
|
transport = transport_get(states->remote, states->remote->url_nr > 0 ?
|
2008-06-10 22:51:21 +08:00
|
|
|
states->remote->url[0] : NULL);
|
2009-02-25 16:32:21 +08:00
|
|
|
remote_refs = transport_get_remote_refs(transport);
|
2008-06-10 22:51:21 +08:00
|
|
|
transport_disconnect(transport);
|
|
|
|
|
2009-02-25 16:32:27 +08:00
|
|
|
states->queried = 1;
|
2009-02-25 16:32:24 +08:00
|
|
|
if (query & GET_REF_STATES)
|
|
|
|
get_ref_states(remote_refs, states);
|
|
|
|
if (query & GET_HEAD_NAMES)
|
|
|
|
get_head_names(remote_refs, states);
|
2009-02-25 16:32:28 +08:00
|
|
|
if (query & GET_PUSH_REF_STATES)
|
|
|
|
get_push_ref_states(remote_refs, states);
|
2009-02-25 16:32:23 +08:00
|
|
|
} else {
|
2009-02-25 16:32:22 +08:00
|
|
|
for_each_ref(append_ref_to_tracked_list, states);
|
2009-02-25 16:32:23 +08:00
|
|
|
sort_string_list(&states->tracked);
|
2009-02-25 16:32:28 +08:00
|
|
|
get_push_ref_states_noquery(states);
|
2008-06-10 22:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:27 +08:00
|
|
|
struct show_info {
|
|
|
|
struct string_list *list;
|
|
|
|
struct ref_states *states;
|
2009-02-25 16:32:28 +08:00
|
|
|
int width, width2;
|
2009-02-25 16:32:27 +08:00
|
|
|
int any_rebase;
|
|
|
|
};
|
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
|
2008-06-11 06:54:49 +08:00
|
|
|
{
|
2009-02-25 16:32:27 +08:00
|
|
|
struct show_info *info = cb_data;
|
|
|
|
int n = strlen(item->string);
|
|
|
|
if (n > info->width)
|
|
|
|
info->width = n;
|
2010-06-26 07:41:35 +08:00
|
|
|
string_list_insert(info->list, item->string);
|
2009-02-25 16:32:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-06-11 06:54:49 +08:00
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int show_remote_info_item(struct string_list_item *item, void *cb_data)
|
2009-02-25 16:32:27 +08:00
|
|
|
{
|
|
|
|
struct show_info *info = cb_data;
|
|
|
|
struct ref_states *states = info->states;
|
|
|
|
const char *name = item->string;
|
|
|
|
|
|
|
|
if (states->queried) {
|
|
|
|
const char *fmt = "%s";
|
|
|
|
const char *arg = "";
|
|
|
|
if (string_list_has_string(&states->new, name)) {
|
|
|
|
fmt = " new (next fetch will store in remotes/%s)";
|
|
|
|
arg = states->remote->name;
|
|
|
|
} else if (string_list_has_string(&states->tracked, name))
|
|
|
|
arg = " tracked";
|
|
|
|
else if (string_list_has_string(&states->stale, name))
|
|
|
|
arg = " stale (use 'git remote prune' to remove)";
|
|
|
|
else
|
|
|
|
arg = " ???";
|
|
|
|
printf(" %-*s", info->width, name);
|
|
|
|
printf(fmt, arg);
|
|
|
|
printf("\n");
|
|
|
|
} else
|
|
|
|
printf(" %s\n", name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
|
2009-02-25 16:32:27 +08:00
|
|
|
{
|
|
|
|
struct show_info *show_info = cb_data;
|
|
|
|
struct ref_states *states = show_info->states;
|
|
|
|
struct branch_info *branch_info = branch_item->util;
|
|
|
|
struct string_list_item *item;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!branch_info->merge.nr || !branch_info->remote_name ||
|
|
|
|
strcmp(states->remote->name, branch_info->remote_name))
|
|
|
|
return 0;
|
|
|
|
if ((n = strlen(branch_item->string)) > show_info->width)
|
|
|
|
show_info->width = n;
|
|
|
|
if (branch_info->rebase)
|
|
|
|
show_info->any_rebase = 1;
|
|
|
|
|
2010-06-26 07:41:35 +08:00
|
|
|
item = string_list_insert(show_info->list, branch_item->string);
|
2009-02-25 16:32:27 +08:00
|
|
|
item->util = branch_info;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int show_local_info_item(struct string_list_item *item, void *cb_data)
|
2009-02-25 16:32:27 +08:00
|
|
|
{
|
|
|
|
struct show_info *show_info = cb_data;
|
|
|
|
struct branch_info *branch_info = item->util;
|
|
|
|
struct string_list *merge = &branch_info->merge;
|
|
|
|
const char *also;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (branch_info->rebase && branch_info->merge.nr > 1) {
|
|
|
|
error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
|
|
|
|
item->string);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" %-*s ", show_info->width, item->string);
|
|
|
|
if (branch_info->rebase) {
|
|
|
|
printf("rebases onto remote %s\n", merge->items[0].string);
|
|
|
|
return 0;
|
|
|
|
} else if (show_info->any_rebase) {
|
|
|
|
printf(" merges with remote %s\n", merge->items[0].string);
|
|
|
|
also = " and with remote";
|
|
|
|
} else {
|
|
|
|
printf("merges with remote %s\n", merge->items[0].string);
|
|
|
|
also = " and with remote";
|
|
|
|
}
|
|
|
|
for (i = 1; i < merge->nr; i++)
|
|
|
|
printf(" %-*s %s %s\n", show_info->width, "", also,
|
|
|
|
merge->items[i].string);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-11 06:54:49 +08:00
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
|
2009-02-25 16:32:28 +08:00
|
|
|
{
|
|
|
|
struct show_info *show_info = cb_data;
|
|
|
|
struct push_info *push_info = push_item->util;
|
|
|
|
struct string_list_item *item;
|
|
|
|
int n;
|
|
|
|
if ((n = strlen(push_item->string)) > show_info->width)
|
|
|
|
show_info->width = n;
|
|
|
|
if ((n = strlen(push_info->dest)) > show_info->width2)
|
|
|
|
show_info->width2 = n;
|
2010-06-26 07:41:38 +08:00
|
|
|
item = string_list_append(show_info->list, push_item->string);
|
2009-02-25 16:32:28 +08:00
|
|
|
item->util = push_item->util;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-22 16:59:20 +08:00
|
|
|
/*
|
|
|
|
* Sorting comparison for a string list that has push_info
|
|
|
|
* structs in its util field
|
|
|
|
*/
|
|
|
|
static int cmp_string_with_push(const void *va, const void *vb)
|
|
|
|
{
|
|
|
|
const struct string_list_item *a = va;
|
|
|
|
const struct string_list_item *b = vb;
|
|
|
|
const struct push_info *a_push = a->util;
|
|
|
|
const struct push_info *b_push = b->util;
|
|
|
|
int cmp = strcmp(a->string, b->string);
|
|
|
|
return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
|
|
|
|
}
|
|
|
|
|
2009-06-19 01:28:43 +08:00
|
|
|
static int show_push_info_item(struct string_list_item *item, void *cb_data)
|
2009-02-25 16:32:28 +08:00
|
|
|
{
|
|
|
|
struct show_info *show_info = cb_data;
|
|
|
|
struct push_info *push_info = item->util;
|
|
|
|
char *src = item->string, *status = NULL;
|
|
|
|
|
|
|
|
switch (push_info->status) {
|
|
|
|
case PUSH_STATUS_CREATE:
|
|
|
|
status = "create";
|
|
|
|
break;
|
|
|
|
case PUSH_STATUS_DELETE:
|
|
|
|
status = "delete";
|
|
|
|
src = "(none)";
|
|
|
|
break;
|
|
|
|
case PUSH_STATUS_UPTODATE:
|
|
|
|
status = "up to date";
|
|
|
|
break;
|
|
|
|
case PUSH_STATUS_FASTFORWARD:
|
2009-10-24 16:31:32 +08:00
|
|
|
status = "fast-forwardable";
|
2009-02-25 16:32:28 +08:00
|
|
|
break;
|
|
|
|
case PUSH_STATUS_OUTOFDATE:
|
|
|
|
status = "local out of date";
|
|
|
|
break;
|
|
|
|
case PUSH_STATUS_NOTQUERIED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status)
|
|
|
|
printf(" %-*s %s to %-*s (%s)\n", show_info->width, src,
|
|
|
|
push_info->forced ? "forces" : "pushes",
|
|
|
|
show_info->width2, push_info->dest, status);
|
|
|
|
else
|
|
|
|
printf(" %-*s %s to %s\n", show_info->width, src,
|
|
|
|
push_info->forced ? "forces" : "pushes",
|
|
|
|
push_info->dest);
|
2008-06-11 06:54:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-10 22:51:21 +08:00
|
|
|
static int show(int argc, const char **argv)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
2009-02-25 16:32:24 +08:00
|
|
|
int no_query = 0, result = 0, query_flag = 0;
|
2008-02-29 09:45:45 +08:00
|
|
|
struct option options[] = {
|
2008-06-10 22:51:08 +08:00
|
|
|
OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
|
2008-02-29 09:45:45 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct ref_states states;
|
2010-07-05 03:46:19 +08:00
|
|
|
struct string_list info_list = STRING_LIST_INIT_NODUP;
|
2009-02-25 16:32:27 +08:00
|
|
|
struct show_info info;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
|
2009-05-24 02:53:12 +08:00
|
|
|
0);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2008-06-10 22:51:21 +08:00
|
|
|
if (argc < 1)
|
|
|
|
return show_all();
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-02-25 16:32:24 +08:00
|
|
|
if (!no_query)
|
2009-02-25 16:32:28 +08:00
|
|
|
query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
|
2009-02-25 16:32:24 +08:00
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
memset(&states, 0, sizeof(states));
|
2009-02-25 16:32:27 +08:00
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
info.states = &states;
|
|
|
|
info.list = &info_list;
|
2008-02-29 09:45:45 +08:00
|
|
|
for (; argc; argc--, argv++) {
|
2008-06-10 22:51:08 +08:00
|
|
|
int i;
|
2009-06-14 00:29:10 +08:00
|
|
|
const char **url;
|
|
|
|
int url_nr;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-02-25 16:32:24 +08:00
|
|
|
get_remote_ref_states(*argv, &states, query_flag);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-06-06 23:16:30 +08:00
|
|
|
printf("* remote %s\n", *argv);
|
2009-06-14 00:29:10 +08:00
|
|
|
printf(" Fetch URL: %s\n", states.remote->url_nr > 0 ?
|
|
|
|
states.remote->url[0] : "(no URL)");
|
|
|
|
if (states.remote->pushurl_nr) {
|
|
|
|
url = states.remote->pushurl;
|
|
|
|
url_nr = states.remote->pushurl_nr;
|
|
|
|
} else {
|
|
|
|
url = states.remote->url;
|
|
|
|
url_nr = states.remote->url_nr;
|
|
|
|
}
|
2011-08-26 05:46:52 +08:00
|
|
|
for (i = 0; i < url_nr; i++)
|
2009-06-14 00:29:10 +08:00
|
|
|
printf(" Push URL: %s\n", url[i]);
|
|
|
|
if (!i)
|
|
|
|
printf(" Push URL: %s\n", "(no URL)");
|
2009-02-25 16:32:24 +08:00
|
|
|
if (no_query)
|
|
|
|
printf(" HEAD branch: (not queried)\n");
|
|
|
|
else if (!states.heads.nr)
|
|
|
|
printf(" HEAD branch: (unknown)\n");
|
|
|
|
else if (states.heads.nr == 1)
|
|
|
|
printf(" HEAD branch: %s\n", states.heads.items[0].string);
|
|
|
|
else {
|
|
|
|
printf(" HEAD branch (remote HEAD is ambiguous,"
|
|
|
|
" may be one of the following):\n");
|
|
|
|
for (i = 0; i < states.heads.nr; i++)
|
|
|
|
printf(" %s\n", states.heads.items[i].string);
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:27 +08:00
|
|
|
/* remote branch info */
|
|
|
|
info.width = 0;
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(&states.new, add_remote_to_show_info, &info);
|
|
|
|
for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
|
|
|
|
for_each_string_list(&states.stale, add_remote_to_show_info, &info);
|
2009-02-25 16:32:27 +08:00
|
|
|
if (info.list->nr)
|
|
|
|
printf(" Remote branch%s:%s\n",
|
|
|
|
info.list->nr > 1 ? "es" : "",
|
|
|
|
no_query ? " (status not queried)" : "");
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(info.list, show_remote_info_item, &info);
|
2009-02-25 16:32:27 +08:00
|
|
|
string_list_clear(info.list, 0);
|
|
|
|
|
|
|
|
/* git pull info */
|
|
|
|
info.width = 0;
|
|
|
|
info.any_rebase = 0;
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(&branch_list, add_local_to_show_info, &info);
|
2009-02-25 16:32:27 +08:00
|
|
|
if (info.list->nr)
|
|
|
|
printf(" Local branch%s configured for 'git pull':\n",
|
|
|
|
info.list->nr > 1 ? "es" : "");
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(info.list, show_local_info_item, &info);
|
2009-02-25 16:32:27 +08:00
|
|
|
string_list_clear(info.list, 0);
|
|
|
|
|
|
|
|
/* git push info */
|
2009-02-25 16:32:28 +08:00
|
|
|
if (states.remote->mirror)
|
|
|
|
printf(" Local refs will be mirrored by 'git push'\n");
|
|
|
|
|
|
|
|
info.width = info.width2 = 0;
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(&states.push, add_push_to_show_info, &info);
|
2009-03-22 16:59:20 +08:00
|
|
|
qsort(info.list->items, info.list->nr,
|
|
|
|
sizeof(*info.list->items), cmp_string_with_push);
|
2009-02-25 16:32:28 +08:00
|
|
|
if (info.list->nr)
|
|
|
|
printf(" Local ref%s configured for 'git push'%s:\n",
|
|
|
|
info.list->nr > 1 ? "s" : "",
|
|
|
|
no_query ? " (status not queried)" : "");
|
2010-06-26 07:41:34 +08:00
|
|
|
for_each_string_list(info.list, show_push_info_item, &info);
|
2009-02-25 16:32:28 +08:00
|
|
|
string_list_clear(info.list, 0);
|
2008-06-10 22:51:21 +08:00
|
|
|
|
2009-02-25 16:32:19 +08:00
|
|
|
free_remote_ref_states(&states);
|
2008-06-10 22:51:21 +08:00
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2008-06-10 22:51:21 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:25 +08:00
|
|
|
static int set_head(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int i, opt_a = 0, opt_d = 0, result = 0;
|
|
|
|
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
|
|
|
|
char *head_name = NULL;
|
|
|
|
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_BOOLEAN('a', "auto", &opt_a,
|
|
|
|
"set refs/remotes/<name>/HEAD according to remote"),
|
|
|
|
OPT_BOOLEAN('d', "delete", &opt_d,
|
|
|
|
"delete refs/remotes/<name>/HEAD"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
2009-11-21 07:43:13 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
|
2009-05-24 02:53:12 +08:00
|
|
|
0);
|
2009-02-25 16:32:25 +08:00
|
|
|
if (argc)
|
|
|
|
strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
|
|
|
|
|
|
|
|
if (!opt_a && !opt_d && argc == 2) {
|
|
|
|
head_name = xstrdup(argv[1]);
|
|
|
|
} else if (opt_a && !opt_d && argc == 1) {
|
|
|
|
struct ref_states states;
|
|
|
|
memset(&states, 0, sizeof(states));
|
|
|
|
get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
|
|
|
|
if (!states.heads.nr)
|
|
|
|
result |= error("Cannot determine remote HEAD");
|
|
|
|
else if (states.heads.nr > 1) {
|
|
|
|
result |= error("Multiple remote HEAD branches. "
|
|
|
|
"Please choose one explicitly with:");
|
|
|
|
for (i = 0; i < states.heads.nr; i++)
|
|
|
|
fprintf(stderr, " git remote set-head %s %s\n",
|
|
|
|
argv[0], states.heads.items[i].string);
|
|
|
|
} else
|
|
|
|
head_name = xstrdup(states.heads.items[0].string);
|
|
|
|
free_remote_ref_states(&states);
|
|
|
|
} else if (opt_d && !opt_a && argc == 1) {
|
|
|
|
if (delete_ref(buf.buf, NULL, REF_NODEREF))
|
|
|
|
result |= error("Could not delete %s", buf.buf);
|
|
|
|
} else
|
2009-11-21 07:43:13 +08:00
|
|
|
usage_with_options(builtin_remote_sethead_usage, options);
|
2009-02-25 16:32:25 +08:00
|
|
|
|
|
|
|
if (head_name) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
|
|
|
|
/* make sure it's valid */
|
|
|
|
if (!resolve_ref(buf2.buf, sha1, 1, NULL))
|
|
|
|
result |= error("Not a valid ref: %s", buf2.buf);
|
|
|
|
else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
|
|
|
|
result |= error("Could not setup %s", buf.buf);
|
|
|
|
if (opt_a)
|
|
|
|
printf("%s/HEAD set to %s\n", argv[0], head_name);
|
|
|
|
free(head_name);
|
2008-06-10 22:51:21 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 16:32:25 +08:00
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&buf2);
|
2008-06-10 22:51:21 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int prune(int argc, const char **argv)
|
|
|
|
{
|
2008-06-10 22:51:35 +08:00
|
|
|
int dry_run = 0, result = 0;
|
2008-06-10 22:51:21 +08:00
|
|
|
struct option options[] = {
|
2010-11-09 01:58:51 +08:00
|
|
|
OPT__DRY_RUN(&dry_run, "dry run"),
|
2008-06-10 22:51:21 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
|
2009-05-24 02:53:12 +08:00
|
|
|
0);
|
2008-06-10 22:51:21 +08:00
|
|
|
|
|
|
|
if (argc < 1)
|
2009-11-21 07:43:13 +08:00
|
|
|
usage_with_options(builtin_remote_prune_usage, options);
|
2008-06-10 22:51:21 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
for (; argc; argc--, argv++)
|
|
|
|
result |= prune_remote(*argv, dry_run);
|
2009-02-09 15:27:10 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
return result;
|
|
|
|
}
|
2008-06-10 22:51:35 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
static int prune_remote(const char *remote, int dry_run)
|
|
|
|
{
|
|
|
|
int result = 0, i;
|
|
|
|
struct ref_states states;
|
|
|
|
const char *dangling_msg = dry_run
|
|
|
|
? " %s will become dangling!\n"
|
|
|
|
: " %s has become dangling!\n";
|
2008-06-10 22:51:21 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
memset(&states, 0, sizeof(states));
|
|
|
|
get_remote_ref_states(remote, &states, GET_REF_STATES);
|
|
|
|
|
|
|
|
if (states.stale.nr) {
|
|
|
|
printf("Pruning %s\n", remote);
|
|
|
|
printf("URL: %s\n",
|
|
|
|
states.remote->url_nr
|
|
|
|
? states.remote->url[0]
|
|
|
|
: "(no URL)");
|
|
|
|
}
|
2008-06-10 22:51:35 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
for (i = 0; i < states.stale.nr; i++) {
|
|
|
|
const char *refname = states.stale.items[i].util;
|
2008-06-10 22:51:35 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
if (!dry_run)
|
|
|
|
result |= delete_ref(refname, NULL, 0);
|
2008-06-10 22:51:21 +08:00
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
|
|
|
|
abbrev_ref(refname, "refs/remotes/"));
|
2009-11-10 13:03:32 +08:00
|
|
|
warn_dangling_symref(stdout, dangling_msg, refname);
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
2009-04-03 17:02:37 +08:00
|
|
|
free_remote_ref_states(&states);
|
2008-02-29 09:45:45 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
static int get_remote_default(const char *key, const char *value, void *priv)
|
2008-02-29 09:45:45 +08:00
|
|
|
{
|
2009-11-10 16:21:32 +08:00
|
|
|
if (strcmp(key, "remotes.default") == 0) {
|
|
|
|
int *found = priv;
|
|
|
|
*found = 1;
|
2008-03-04 19:23:53 +08:00
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update(int argc, const char **argv)
|
|
|
|
{
|
2009-11-10 16:21:32 +08:00
|
|
|
int i, prune = 0;
|
2009-04-03 17:03:44 +08:00
|
|
|
struct option options[] = {
|
|
|
|
OPT_BOOLEAN('p', "prune", &prune,
|
2009-04-18 02:13:29 +08:00
|
|
|
"prune remotes after fetching"),
|
2009-04-03 17:03:44 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
2009-11-10 16:21:32 +08:00
|
|
|
const char **fetch_argv;
|
|
|
|
int fetch_argc = 0;
|
|
|
|
int default_defined = 0;
|
|
|
|
|
|
|
|
fetch_argv = xmalloc(sizeof(char *) * (argc+5));
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-11-21 07:43:13 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
|
2009-04-03 17:03:44 +08:00
|
|
|
PARSE_OPT_KEEP_ARGV0);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
fetch_argv[fetch_argc++] = "fetch";
|
2008-03-04 19:23:53 +08:00
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
if (prune)
|
|
|
|
fetch_argv[fetch_argc++] = "--prune";
|
|
|
|
if (verbose)
|
|
|
|
fetch_argv[fetch_argc++] = "-v";
|
2009-12-31 17:43:17 +08:00
|
|
|
fetch_argv[fetch_argc++] = "--multiple";
|
|
|
|
if (argc < 2)
|
2009-11-10 16:21:32 +08:00
|
|
|
fetch_argv[fetch_argc++] = "default";
|
2009-12-31 17:43:17 +08:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
fetch_argv[fetch_argc++] = argv[i];
|
2008-03-04 19:23:53 +08:00
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
|
|
|
|
git_config(get_remote_default, &default_defined);
|
|
|
|
if (!default_defined)
|
|
|
|
fetch_argv[fetch_argc-1] = "--all";
|
2009-04-03 17:03:44 +08:00
|
|
|
}
|
2008-03-04 19:23:53 +08:00
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
fetch_argv[fetch_argc] = NULL;
|
2008-03-04 19:23:53 +08:00
|
|
|
|
2009-11-10 16:21:32 +08:00
|
|
|
return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:38:50 +08:00
|
|
|
static int remove_all_fetch_refspecs(const char *remote, const char *key)
|
|
|
|
{
|
|
|
|
return git_config_set_multivar(key, NULL, NULL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_branches(struct remote *remote, const char **branches,
|
|
|
|
const char *key)
|
|
|
|
{
|
|
|
|
const char *remotename = remote->name;
|
|
|
|
int mirror = remote->mirror;
|
|
|
|
struct strbuf refspec = STRBUF_INIT;
|
|
|
|
|
|
|
|
for (; *branches; branches++)
|
|
|
|
if (add_branch(key, *branches, remotename, mirror, &refspec)) {
|
|
|
|
strbuf_release(&refspec);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&refspec);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_remote_branches(const char *remotename, const char **branches,
|
|
|
|
int add_mode)
|
|
|
|
{
|
|
|
|
struct strbuf key = STRBUF_INIT;
|
|
|
|
struct remote *remote;
|
|
|
|
|
|
|
|
strbuf_addf(&key, "remote.%s.fetch", remotename);
|
|
|
|
|
|
|
|
if (!remote_is_configured(remotename))
|
|
|
|
die("No such remote '%s'", remotename);
|
|
|
|
remote = remote_get(remotename);
|
|
|
|
|
|
|
|
if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
|
|
|
|
strbuf_release(&key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (add_branches(remote, branches, key.buf)) {
|
|
|
|
strbuf_release(&key);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&key);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_branches(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int add_mode = 0;
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_BOOLEAN('\0', "add", &add_mode, "add branch"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, NULL, options,
|
|
|
|
builtin_remote_setbranches_usage, 0);
|
|
|
|
if (argc == 0) {
|
|
|
|
error("no remote specified");
|
|
|
|
usage_with_options(builtin_remote_seturl_usage, options);
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
return set_remote_branches(argv[0], argv + 1, add_mode);
|
|
|
|
}
|
|
|
|
|
2010-01-19 01:18:02 +08:00
|
|
|
static int set_url(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int i, push_mode = 0, add_mode = 0, delete_mode = 0;
|
|
|
|
int matches = 0, negative_matches = 0;
|
|
|
|
const char *remotename = NULL;
|
|
|
|
const char *newurl = NULL;
|
|
|
|
const char *oldurl = NULL;
|
|
|
|
struct remote *remote;
|
|
|
|
regex_t old_regex;
|
|
|
|
const char **urlset;
|
|
|
|
int urlset_nr;
|
|
|
|
struct strbuf name_buf = STRBUF_INIT;
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_BOOLEAN('\0', "push", &push_mode,
|
|
|
|
"manipulate push URLs"),
|
|
|
|
OPT_BOOLEAN('\0', "add", &add_mode,
|
|
|
|
"add URL"),
|
|
|
|
OPT_BOOLEAN('\0', "delete", &delete_mode,
|
|
|
|
"delete URLs"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
|
|
|
|
PARSE_OPT_KEEP_ARGV0);
|
|
|
|
|
|
|
|
if (add_mode && delete_mode)
|
|
|
|
die("--add --delete doesn't make sense");
|
|
|
|
|
|
|
|
if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
|
|
|
|
usage_with_options(builtin_remote_seturl_usage, options);
|
|
|
|
|
|
|
|
remotename = argv[1];
|
|
|
|
newurl = argv[2];
|
|
|
|
if (argc > 3)
|
|
|
|
oldurl = argv[3];
|
|
|
|
|
|
|
|
if (delete_mode)
|
|
|
|
oldurl = newurl;
|
|
|
|
|
|
|
|
if (!remote_is_configured(remotename))
|
|
|
|
die("No such remote '%s'", remotename);
|
|
|
|
remote = remote_get(remotename);
|
|
|
|
|
|
|
|
if (push_mode) {
|
|
|
|
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
|
|
|
|
urlset = remote->pushurl;
|
|
|
|
urlset_nr = remote->pushurl_nr;
|
|
|
|
} else {
|
|
|
|
strbuf_addf(&name_buf, "remote.%s.url", remotename);
|
|
|
|
urlset = remote->url;
|
|
|
|
urlset_nr = remote->url_nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Special cases that add new entry. */
|
|
|
|
if ((!oldurl && !delete_mode) || add_mode) {
|
|
|
|
if (add_mode)
|
|
|
|
git_config_set_multivar(name_buf.buf, newurl,
|
|
|
|
"^$", 0);
|
|
|
|
else
|
|
|
|
git_config_set(name_buf.buf, newurl);
|
|
|
|
strbuf_release(&name_buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Old URL specified. Demand that one matches. */
|
|
|
|
if (regcomp(&old_regex, oldurl, REG_EXTENDED))
|
|
|
|
die("Invalid old URL pattern: %s", oldurl);
|
|
|
|
|
|
|
|
for (i = 0; i < urlset_nr; i++)
|
|
|
|
if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
|
|
|
|
matches++;
|
|
|
|
else
|
|
|
|
negative_matches++;
|
|
|
|
if (!delete_mode && !matches)
|
|
|
|
die("No such URL found: %s", oldurl);
|
|
|
|
if (delete_mode && !negative_matches && !push_mode)
|
|
|
|
die("Will not delete all non-push URLs");
|
|
|
|
|
|
|
|
regfree(&old_regex);
|
|
|
|
|
|
|
|
if (!delete_mode)
|
|
|
|
git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
|
|
|
|
else
|
|
|
|
git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:45:45 +08:00
|
|
|
static int get_one_entry(struct remote *remote, void *priv)
|
|
|
|
{
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list *list = priv;
|
2009-06-23 06:27:44 +08:00
|
|
|
struct strbuf url_buf = STRBUF_INIT;
|
2009-06-14 00:29:11 +08:00
|
|
|
const char **url;
|
|
|
|
int i, url_nr;
|
2008-02-29 09:45:45 +08:00
|
|
|
|
2008-09-22 16:57:51 +08:00
|
|
|
if (remote->url_nr > 0) {
|
2009-06-23 06:27:44 +08:00
|
|
|
strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(list, remote->name)->util =
|
2009-06-23 06:27:44 +08:00
|
|
|
strbuf_detach(&url_buf, NULL);
|
2008-09-22 16:57:51 +08:00
|
|
|
} else
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(list, remote->name)->util = NULL;
|
2009-06-14 00:29:11 +08:00
|
|
|
if (remote->pushurl_nr) {
|
|
|
|
url = remote->pushurl;
|
|
|
|
url_nr = remote->pushurl_nr;
|
|
|
|
} else {
|
|
|
|
url = remote->url;
|
|
|
|
url_nr = remote->url_nr;
|
|
|
|
}
|
|
|
|
for (i = 0; i < url_nr; i++)
|
|
|
|
{
|
2009-06-23 06:27:44 +08:00
|
|
|
strbuf_addf(&url_buf, "%s (push)", url[i]);
|
2010-06-26 07:41:38 +08:00
|
|
|
string_list_append(list, remote->name)->util =
|
2009-06-23 06:27:44 +08:00
|
|
|
strbuf_detach(&url_buf, NULL);
|
2009-06-14 00:29:11 +08:00
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_all(void)
|
|
|
|
{
|
2010-07-05 03:46:19 +08:00
|
|
|
struct string_list list = STRING_LIST_INIT_NODUP;
|
2009-06-14 00:29:11 +08:00
|
|
|
int result;
|
|
|
|
|
|
|
|
list.strdup_strings = 1;
|
|
|
|
result = for_each_remote(get_one_entry, &list);
|
2008-02-29 09:45:45 +08:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
int i;
|
|
|
|
|
2008-07-22 02:03:49 +08:00
|
|
|
sort_string_list(&list);
|
2008-02-29 09:45:45 +08:00
|
|
|
for (i = 0; i < list.nr; i++) {
|
2008-07-22 02:03:49 +08:00
|
|
|
struct string_list_item *item = list.items + i;
|
2008-09-22 16:57:51 +08:00
|
|
|
if (verbose)
|
|
|
|
printf("%s\t%s\n", item->string,
|
|
|
|
item->util ? (const char *)item->util : "");
|
|
|
|
else {
|
|
|
|
if (i && !strcmp((item - 1)->string, item->string))
|
|
|
|
continue;
|
|
|
|
printf("%s\n", item->string);
|
|
|
|
}
|
2008-02-29 09:45:45 +08:00
|
|
|
}
|
|
|
|
}
|
2009-06-14 00:29:11 +08:00
|
|
|
string_list_clear(&list, 1);
|
2008-02-29 09:45:45 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_remote(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
2010-11-09 01:56:39 +08:00
|
|
|
OPT__VERBOSE(&verbose, "be verbose; must be placed before a subcommand"),
|
2008-02-29 09:45:45 +08:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
int result;
|
|
|
|
|
2009-05-24 02:53:12 +08:00
|
|
|
argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
|
2008-02-29 09:45:45 +08:00
|
|
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
result = show_all();
|
|
|
|
else if (!strcmp(argv[0], "add"))
|
|
|
|
result = add(argc, argv);
|
2008-11-04 02:26:18 +08:00
|
|
|
else if (!strcmp(argv[0], "rename"))
|
|
|
|
result = mv(argc, argv);
|
2008-02-29 09:45:45 +08:00
|
|
|
else if (!strcmp(argv[0], "rm"))
|
|
|
|
result = rm(argc, argv);
|
2009-02-25 16:32:25 +08:00
|
|
|
else if (!strcmp(argv[0], "set-head"))
|
|
|
|
result = set_head(argc, argv);
|
2010-05-20 02:38:50 +08:00
|
|
|
else if (!strcmp(argv[0], "set-branches"))
|
|
|
|
result = set_branches(argc, argv);
|
2010-01-19 01:18:02 +08:00
|
|
|
else if (!strcmp(argv[0], "set-url"))
|
|
|
|
result = set_url(argc, argv);
|
2008-02-29 09:45:45 +08:00
|
|
|
else if (!strcmp(argv[0], "show"))
|
2008-06-10 22:51:21 +08:00
|
|
|
result = show(argc, argv);
|
2008-02-29 09:45:45 +08:00
|
|
|
else if (!strcmp(argv[0], "prune"))
|
2008-06-10 22:51:21 +08:00
|
|
|
result = prune(argc, argv);
|
2008-02-29 09:45:45 +08:00
|
|
|
else if (!strcmp(argv[0], "update"))
|
|
|
|
result = update(argc, argv);
|
|
|
|
else {
|
|
|
|
error("Unknown subcommand: %s", argv[0]);
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result ? 1 : 0;
|
|
|
|
}
|