2008-02-29 02:45:45 +01:00
|
|
|
#include "cache.h"
|
|
|
|
#include "parse-options.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "remote.h"
|
2008-07-21 20:03:49 +02:00
|
|
|
#include "string-list.h"
|
2008-02-29 02:45:45 +01:00
|
|
|
#include "strbuf.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "refs.h"
|
|
|
|
|
|
|
|
static const char * const builtin_remote_usage[] = {
|
2008-11-17 12:15:49 +01:00
|
|
|
"git remote [-v | --verbose]",
|
|
|
|
"git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
|
2008-11-03 19:26:18 +01:00
|
|
|
"git remote rename <old> <new>",
|
2008-02-29 02:45:45 +01:00
|
|
|
"git remote rm <name>",
|
2009-02-25 09:32:25 +01:00
|
|
|
"git remote set-head <name> [-a | -d | <branch>]",
|
2008-11-17 12:15:49 +01:00
|
|
|
"git remote show [-n] <name>",
|
|
|
|
"git remote prune [-n | --dry-run] <name>",
|
2008-11-18 12:04:02 +01:00
|
|
|
"git remote [-v | --verbose] update [group]",
|
2008-02-29 02:45:45 +01:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2009-02-25 09:32:24 +01:00
|
|
|
#define GET_REF_STATES (1<<0)
|
|
|
|
#define GET_HEAD_NAMES (1<<1)
|
|
|
|
|
2008-02-29 02:45:45 +01:00
|
|
|
static int verbose;
|
|
|
|
|
2008-04-09 17:15:51 +02:00
|
|
|
static int show_all(void);
|
|
|
|
|
2008-02-29 02:45:45 +01: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 opt_parse_track(const struct option *opt, const char *arg, int not)
|
|
|
|
{
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list *list = opt->value;
|
2008-02-29 02:45:45 +01:00
|
|
|
if (not)
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_clear(list, 0);
|
2008-02-29 02:45:45 +01:00
|
|
|
else
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_append(arg, list);
|
2008-02-29 02:45:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fetch_remote(const char *name)
|
|
|
|
{
|
2008-11-18 12:04:02 +01:00
|
|
|
const char *argv[] = { "fetch", name, NULL, NULL };
|
|
|
|
if (verbose) {
|
|
|
|
argv[1] = "-v";
|
|
|
|
argv[2] = name;
|
|
|
|
}
|
2008-03-09 13:37:55 +01:00
|
|
|
printf("Updating %s\n", name);
|
2008-02-29 02:45:45 +01:00
|
|
|
if (run_command_v_opt(argv, RUN_GIT_CMD))
|
|
|
|
return error("Could not fetch %s", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
int fetch = 0, mirror = 0;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list track = { NULL, 0, 0 };
|
2008-02-29 02:45:45 +01:00
|
|
|
const char *master = NULL;
|
|
|
|
struct remote *remote;
|
2008-10-09 21:12:12 +02:00
|
|
|
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
|
2008-02-29 02:45:45 +01:00
|
|
|
const char *name, *url;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_GROUP("add specific options"),
|
|
|
|
OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
|
|
|
|
OPT_CALLBACK('t', "track", &track, "branch",
|
|
|
|
"branch(es) to track", opt_parse_track),
|
|
|
|
OPT_STRING('m', "master", &master, "branch", "master branch"),
|
|
|
|
OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
|
|
|
|
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 11:56:54 +02: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 02:45:45 +01:00
|
|
|
strbuf_addf(&buf, "remote.%s.url", name);
|
|
|
|
if (git_config_set(buf.buf, url))
|
|
|
|
return 1;
|
|
|
|
|
2008-04-13 11:56:54 +02:00
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.fetch", name);
|
|
|
|
|
2008-02-29 02:45:45 +01:00
|
|
|
if (track.nr == 0)
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_append("*", &track);
|
2008-02-29 02:45:45 +01:00
|
|
|
for (i = 0; i < track.nr; i++) {
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item = track.items + i;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
strbuf_reset(&buf2);
|
2008-04-22 13:11:13 +02:00
|
|
|
strbuf_addch(&buf2, '+');
|
2008-02-29 02:45:45 +01:00
|
|
|
if (mirror)
|
|
|
|
strbuf_addf(&buf2, "refs/%s:refs/%s",
|
2008-07-21 20:03:49 +02:00
|
|
|
item->string, item->string);
|
2008-02-29 02:45:45 +01:00
|
|
|
else
|
|
|
|
strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
|
2008-07-21 20:03:49 +02:00
|
|
|
item->string, name, item->string);
|
2008-02-29 02:45:45 +01:00
|
|
|
if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-04-17 13:17:20 +02:00
|
|
|
if (mirror) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "remote.%s.mirror", name);
|
2008-08-02 21:38:56 +02:00
|
|
|
if (git_config_set(buf.buf, "true"))
|
2008-04-17 13:17:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-29 02:45:45 +01: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-21 20:03:49 +02:00
|
|
|
string_list_clear(&track, 0);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct branch_info {
|
2009-02-25 09:32:21 +01:00
|
|
|
char *remote_name;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list merge;
|
2008-02-29 02:45:45 +01:00
|
|
|
};
|
|
|
|
|
2008-07-21 20:03:49 +02:00
|
|
|
static struct string_list branch_list;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-07-18 06:30:33 +02: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-14 19:46:53 +02:00
|
|
|
static int config_read_branches(const char *key, const char *value, void *cb)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
|
|
|
if (!prefixcmp(key, "branch.")) {
|
|
|
|
char *name;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item;
|
2008-02-29 02:45:45 +01:00
|
|
|
struct branch_info *info;
|
|
|
|
enum { REMOTE, MERGE } type;
|
|
|
|
|
|
|
|
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;
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
2008-07-21 20:03:49 +02:00
|
|
|
item = string_list_insert(name, &branch_list);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
if (!item->util)
|
|
|
|
item->util = xcalloc(sizeof(struct branch_info), 1);
|
|
|
|
info = item->util;
|
|
|
|
if (type == REMOTE) {
|
2009-02-25 09:32:21 +01:00
|
|
|
if (info->remote_name)
|
2008-02-29 02:45:45 +01:00
|
|
|
warning("more than one branch.%s", key);
|
2009-02-25 09:32:21 +01:00
|
|
|
info->remote_name = xstrdup(value);
|
2008-02-29 02:45:45 +01:00
|
|
|
} else {
|
|
|
|
char *space = strchr(value, ' ');
|
2008-07-18 06:30:33 +02:00
|
|
|
value = abbrev_branch(value);
|
2008-02-29 02:45:45 +01:00
|
|
|
while (space) {
|
|
|
|
char *merge;
|
|
|
|
merge = xstrndup(value, space - value);
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_append(merge, &info->merge);
|
2008-07-18 06:30:33 +02:00
|
|
|
value = abbrev_branch(space + 1);
|
2008-02-29 02:45:45 +01:00
|
|
|
space = strchr(value, ' ');
|
|
|
|
}
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_append(xstrdup(value), &info->merge);
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_branches(void)
|
|
|
|
{
|
|
|
|
if (branch_list.nr)
|
|
|
|
return;
|
2008-05-14 19:46:53 +02:00
|
|
|
git_config(config_read_branches, NULL);
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ref_states {
|
|
|
|
struct remote *remote;
|
2009-02-25 09:32:24 +01:00
|
|
|
struct string_list new, stale, tracked, heads;
|
2008-02-29 02:45:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int handle_one_branch(const char *refname,
|
|
|
|
const unsigned char *sha1, int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct ref_states *states = cb_data;
|
|
|
|
struct refspec refspec;
|
|
|
|
|
|
|
|
memset(&refspec, 0, sizeof(refspec));
|
|
|
|
refspec.dst = (char *)refname;
|
|
|
|
if (!remote_find_tracking(states->remote, &refspec)) {
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item;
|
2008-07-18 06:30:33 +02:00
|
|
|
const char *name = abbrev_branch(refspec.src);
|
2008-03-19 01:27:42 +01:00
|
|
|
/* symbolic refs pointing nowhere were handled already */
|
|
|
|
if ((flags & REF_ISSYMREF) ||
|
2009-02-25 09:32:23 +01:00
|
|
|
string_list_has_string(&states->tracked, name) ||
|
|
|
|
string_list_has_string(&states->new, name))
|
2008-02-29 02:45:45 +01:00
|
|
|
return 0;
|
2008-07-21 20:03:49 +02:00
|
|
|
item = string_list_append(name, &states->stale);
|
2008-02-29 02:45:45 +01:00
|
|
|
item->util = xstrdup(refname);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:21 +01:00
|
|
|
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
|
|
|
struct ref *fetch_map = NULL, **tail = &fetch_map;
|
2009-02-25 09:32:21 +01:00
|
|
|
struct ref *ref;
|
2008-02-29 02:45:45 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < states->remote->fetch_refspec_nr; i++)
|
2009-02-25 09:32:21 +01:00
|
|
|
if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
|
2008-02-29 02:45:45 +01:00
|
|
|
die("Could not get fetch map for refspec %s",
|
|
|
|
states->remote->fetch_refspec[i]);
|
|
|
|
|
2008-07-21 20:03:49 +02:00
|
|
|
states->new.strdup_strings = states->tracked.strdup_strings = 1;
|
2008-02-29 02:45:45 +01:00
|
|
|
for (ref = fetch_map; ref; ref = ref->next) {
|
|
|
|
unsigned char sha1[20];
|
|
|
|
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
|
2009-02-25 09:32:20 +01:00
|
|
|
string_list_append(abbrev_branch(ref->name), &states->new);
|
|
|
|
else
|
|
|
|
string_list_append(abbrev_branch(ref->name), &states->tracked);
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
free_refs(fetch_map);
|
|
|
|
|
2009-02-25 09:32:23 +01:00
|
|
|
sort_string_list(&states->new);
|
|
|
|
sort_string_list(&states->tracked);
|
2008-02-29 02:45:45 +01:00
|
|
|
for_each_ref(handle_one_branch, states);
|
2008-07-21 20:03:49 +02:00
|
|
|
sort_string_list(&states->stale);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:24 +01: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;
|
|
|
|
refspec.src = refspec.dst = "refs/heads/";
|
|
|
|
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);
|
|
|
|
for(ref = matches; ref; ref = ref->next)
|
|
|
|
string_list_append(abbrev_branch(ref->name), &states->heads);
|
|
|
|
|
|
|
|
free_refs(fetch_map);
|
|
|
|
free_refs(matches);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 06:28:04 +02: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 02:45:45 +01:00
|
|
|
struct branches_for_remote {
|
2008-06-01 06:28:04 +02:00
|
|
|
struct remote *remote;
|
2009-02-04 17:06:07 +01:00
|
|
|
struct string_list *branches, *skipped;
|
2008-06-01 06:28:04 +02:00
|
|
|
struct known_remotes *keep;
|
2008-02-29 02:45:45 +01: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 06:28:04 +02:00
|
|
|
struct refspec refspec;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item;
|
2008-06-01 06:28:04 +02:00
|
|
|
struct known_remote *kr;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-06-01 06:28:04 +02: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-08 23:40:42 +01:00
|
|
|
|
2009-02-04 17:06:07 +01:00
|
|
|
/* don't delete non-remote refs */
|
|
|
|
if (prefixcmp(refname, "refs/remotes")) {
|
|
|
|
/* advise user how to delete local branches */
|
|
|
|
if (!prefixcmp(refname, "refs/heads/"))
|
|
|
|
string_list_append(abbrev_branch(refname),
|
|
|
|
branches->skipped);
|
|
|
|
/* silently skip over other non-remote refs */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-01 06:28:04 +02:00
|
|
|
/* make sure that symrefs are deleted */
|
|
|
|
if (flags & REF_ISSYMREF)
|
2008-11-10 22:07:52 +01:00
|
|
|
return unlink(git_path("%s", refname));
|
2008-03-08 23:40:42 +01:00
|
|
|
|
2008-07-21 20:03:49 +02:00
|
|
|
item = string_list_append(refname, branches->branches);
|
2008-06-01 06:28:04 +02:00
|
|
|
item->util = xmalloc(20);
|
|
|
|
hashcpy(item->util, sha1);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-03 19:26:18 +01: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;
|
|
|
|
|
|
|
|
strbuf_addf(&buf, "refs/remotes/%s", rename->old);
|
|
|
|
if(!prefixcmp(refname, buf.buf)) {
|
|
|
|
item = string_list_append(xstrdup(refname), rename->remote_branches);
|
|
|
|
symref = resolve_ref(refname, orig_sha1, 1, &flag);
|
|
|
|
if (flag & REF_ISSYMREF)
|
|
|
|
item->util = xstrdup(symref);
|
|
|
|
else
|
|
|
|
item->util = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-10 21:43:01 +01: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);
|
|
|
|
if (path && unlink(path))
|
|
|
|
warning("failed to remove '%s'", path);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-03 19:26:18 +01:00
|
|
|
static int mv(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct remote *oldremote, *newremote;
|
|
|
|
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
|
|
|
|
struct string_list remote_branches = { NULL, 0, 0, 0 };
|
|
|
|
struct rename_info rename;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (argc != 3)
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
|
|
|
|
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-10 21:43:01 +01:00
|
|
|
if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
|
|
|
|
return migrate_file(oldremote);
|
|
|
|
|
2008-11-03 19:26:18 +01: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);
|
|
|
|
for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
strbuf_reset(&buf2);
|
|
|
|
strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
|
|
|
|
ptr = strstr(buf2.buf, rename.old);
|
|
|
|
if (ptr)
|
|
|
|
strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
|
|
|
|
rename.new, strlen(rename.new));
|
|
|
|
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 09:32:21 +01:00
|
|
|
if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
|
2008-11-03 19:26:18 +01: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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];
|
|
|
|
const char *symref;
|
|
|
|
|
|
|
|
symref = resolve_ref(item->string, sha1, 1, &flag);
|
|
|
|
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-21 20:03:49 +02:00
|
|
|
static int remove_branches(struct string_list *branches)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
|
|
|
int i, result = 0;
|
|
|
|
for (i = 0; i < branches->nr; i++) {
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item = branches->items + i;
|
|
|
|
const char *refname = item->string;
|
2008-02-29 02:45:45 +01:00
|
|
|
unsigned char *sha1 = item->util;
|
|
|
|
|
2008-10-26 03:33:56 +01:00
|
|
|
if (delete_ref(refname, sha1, 0))
|
2008-02-29 02:45:45 +01: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-09 21:12:12 +02:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2008-06-01 06:28:04 +02:00
|
|
|
struct known_remotes known_remotes = { NULL, NULL };
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list branches = { NULL, 0, 0, 1 };
|
2009-02-04 17:06:07 +01:00
|
|
|
struct string_list skipped = { NULL, 0, 0, 1 };
|
|
|
|
struct branches_for_remote cb_data = {
|
|
|
|
NULL, &branches, &skipped, &known_remotes
|
|
|
|
};
|
2009-02-03 18:51:12 +01:00
|
|
|
int i, result;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
|
|
|
|
remote = remote_get(argv[1]);
|
|
|
|
if (!remote)
|
|
|
|
die("No such remote: %s", argv[1]);
|
|
|
|
|
2008-06-01 06:28:04 +02:00
|
|
|
known_remotes.to_delete = remote;
|
|
|
|
for_each_remote(add_known_remote, &known_remotes);
|
|
|
|
|
2008-02-29 02:45:45 +01: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-21 20:03:49 +02:00
|
|
|
struct string_list_item *item = branch_list.items + i;
|
2008-02-29 02:45:45 +01:00
|
|
|
struct branch_info *info = item->util;
|
2009-02-25 09:32:21 +01:00
|
|
|
if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
|
2008-02-29 02:45:45 +01:00
|
|
|
const char *keys[] = { "remote", "merge", NULL }, **k;
|
|
|
|
for (k = keys; *k; k++) {
|
|
|
|
strbuf_reset(&buf);
|
|
|
|
strbuf_addf(&buf, "branch.%s.%s",
|
2008-07-21 20:03:49 +02:00
|
|
|
item->string, *k);
|
2008-02-29 02:45:45 +01: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 06:28:04 +02:00
|
|
|
cb_data.remote = remote;
|
2009-02-03 18:51:12 +01:00
|
|
|
result = for_each_ref(add_branch_for_removal, &cb_data);
|
2008-02-29 02:45:45 +01:00
|
|
|
strbuf_release(&buf);
|
|
|
|
|
2009-02-03 18:51:12 +01:00
|
|
|
if (!result)
|
|
|
|
result = remove_branches(&branches);
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_clear(&branches, 1);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2009-02-04 17:06:07 +01:00
|
|
|
if (skipped.nr) {
|
|
|
|
fprintf(stderr, skipped.nr == 1 ?
|
|
|
|
"Note: A non-remote branch was not removed; "
|
|
|
|
"to delete it, use:\n" :
|
|
|
|
"Note: Non-remote branches were not removed; "
|
|
|
|
"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-03 18:51:12 +01:00
|
|
|
return result;
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 13:03:25 +02:00
|
|
|
static void show_list(const char *title, struct string_list *list,
|
|
|
|
const char *extra_arg)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!list->nr)
|
|
|
|
return;
|
|
|
|
|
2008-09-22 13:03:25 +02:00
|
|
|
printf(title, list->nr > 1 ? "es" : "", extra_arg);
|
2008-02-29 02:45:45 +01:00
|
|
|
printf("\n");
|
2008-10-22 09:39:47 +02:00
|
|
|
for (i = 0; i < list->nr; i++)
|
|
|
|
printf(" %s\n", list->items[i].string);
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:19 +01:00
|
|
|
static void free_remote_ref_states(struct ref_states *states)
|
|
|
|
{
|
|
|
|
string_list_clear(&states->new, 0);
|
|
|
|
string_list_clear(&states->stale, 0);
|
|
|
|
string_list_clear(&states->tracked, 0);
|
2009-02-25 09:32:24 +01:00
|
|
|
string_list_clear(&states->heads, 0);
|
2009-02-25 09:32:19 +01:00
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:22 +01: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 09:32:23 +01:00
|
|
|
if (flags & REF_ISSYMREF)
|
|
|
|
return 0;
|
|
|
|
|
2009-02-25 09:32:22 +01:00
|
|
|
memset(&refspec, 0, sizeof(refspec));
|
|
|
|
refspec.dst = (char *)refname;
|
|
|
|
if (!remote_find_tracking(states->remote, &refspec))
|
|
|
|
string_list_append(abbrev_branch(refspec.src), &states->tracked);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:51:21 +02:00
|
|
|
static int get_remote_ref_states(const char *name,
|
|
|
|
struct ref_states *states,
|
|
|
|
int query)
|
|
|
|
{
|
|
|
|
struct transport *transport;
|
2009-02-25 09:32:21 +01:00
|
|
|
const struct ref *remote_refs;
|
2008-06-10 16:51:21 +02:00
|
|
|
|
|
|
|
states->remote = remote_get(name);
|
|
|
|
if (!states->remote)
|
|
|
|
return error("No such remote: %s", name);
|
|
|
|
|
|
|
|
read_branches();
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
transport = transport_get(NULL, states->remote->url_nr > 0 ?
|
|
|
|
states->remote->url[0] : NULL);
|
2009-02-25 09:32:21 +01:00
|
|
|
remote_refs = transport_get_remote_refs(transport);
|
2008-06-10 16:51:21 +02:00
|
|
|
transport_disconnect(transport);
|
|
|
|
|
2009-02-25 09:32:24 +01: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 09:32:23 +01:00
|
|
|
} else {
|
2009-02-25 09:32:22 +01:00
|
|
|
for_each_ref(append_ref_to_tracked_list, states);
|
2009-02-25 09:32:23 +01:00
|
|
|
sort_string_list(&states->tracked);
|
|
|
|
}
|
2008-06-11 00:54:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:51:21 +02:00
|
|
|
static int show(int argc, const char **argv)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
2009-02-25 09:32:24 +01:00
|
|
|
int no_query = 0, result = 0, query_flag = 0;
|
2008-02-29 02:45:45 +01:00
|
|
|
struct option options[] = {
|
|
|
|
OPT_GROUP("show specific options"),
|
2008-06-10 16:51:08 +02:00
|
|
|
OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
|
2008-02-29 02:45:45 +01:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct ref_states states;
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
|
|
|
|
|
2008-06-10 16:51:21 +02:00
|
|
|
if (argc < 1)
|
|
|
|
return show_all();
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2009-02-25 09:32:24 +01:00
|
|
|
if (!no_query)
|
|
|
|
query_flag = (GET_REF_STATES | GET_HEAD_NAMES);
|
|
|
|
|
2008-02-29 02:45:45 +01:00
|
|
|
memset(&states, 0, sizeof(states));
|
|
|
|
for (; argc; argc--, argv++) {
|
2008-06-10 16:51:08 +02:00
|
|
|
int i;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2009-02-25 09:32:24 +01:00
|
|
|
get_remote_ref_states(*argv, &states, query_flag);
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
printf("* remote %s\n URL: %s\n", *argv,
|
|
|
|
states.remote->url_nr > 0 ?
|
|
|
|
states.remote->url[0] : "(no URL)");
|
2009-02-25 09:32:24 +01: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 02:45:45 +01:00
|
|
|
|
|
|
|
for (i = 0; i < branch_list.nr; i++) {
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *branch = branch_list.items + i;
|
2008-02-29 02:45:45 +01:00
|
|
|
struct branch_info *info = branch->util;
|
|
|
|
int j;
|
|
|
|
|
2009-02-25 09:32:21 +01:00
|
|
|
if (!info->merge.nr || strcmp(*argv, info->remote_name))
|
2008-02-29 02:45:45 +01:00
|
|
|
continue;
|
|
|
|
printf(" Remote branch%s merged with 'git pull' "
|
|
|
|
"while on branch %s\n ",
|
|
|
|
info->merge.nr > 1 ? "es" : "",
|
2008-07-21 20:03:49 +02:00
|
|
|
branch->string);
|
2008-02-29 02:45:45 +01:00
|
|
|
for (j = 0; j < info->merge.nr; j++)
|
2008-07-21 20:03:49 +02:00
|
|
|
printf(" %s", info->merge.items[j].string);
|
2008-02-29 02:45:45 +01:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:51:08 +02:00
|
|
|
if (!no_query) {
|
2008-09-22 13:03:25 +02:00
|
|
|
show_list(" New remote branch%s (next fetch "
|
|
|
|
"will store in remotes/%s)",
|
|
|
|
&states.new, states.remote->name);
|
2008-06-10 16:51:08 +02:00
|
|
|
show_list(" Stale tracking branch%s (use 'git remote "
|
2008-09-22 13:03:25 +02:00
|
|
|
"prune')", &states.stale, "");
|
2008-06-10 16:51:08 +02:00
|
|
|
}
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-09-22 13:03:25 +02:00
|
|
|
show_list(" Tracked remote branch%s", &states.tracked, "");
|
2008-06-11 00:54:49 +02:00
|
|
|
|
2008-02-29 02:45:45 +01:00
|
|
|
if (states.remote->push_refspec_nr) {
|
2008-10-22 09:39:47 +02:00
|
|
|
printf(" Local branch%s pushed with 'git push'\n",
|
2008-02-29 02:45:45 +01:00
|
|
|
states.remote->push_refspec_nr > 1 ?
|
|
|
|
"es" : "");
|
|
|
|
for (i = 0; i < states.remote->push_refspec_nr; i++) {
|
|
|
|
struct refspec *spec = states.remote->push + i;
|
2008-10-22 09:39:47 +02:00
|
|
|
printf(" %s%s%s%s\n",
|
|
|
|
spec->force ? "+" : "",
|
2008-07-18 06:30:33 +02:00
|
|
|
abbrev_branch(spec->src),
|
|
|
|
spec->dst ? ":" : "",
|
|
|
|
spec->dst ? abbrev_branch(spec->dst) : "");
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
}
|
2008-06-10 16:51:21 +02:00
|
|
|
|
2009-02-25 09:32:19 +01:00
|
|
|
free_remote_ref_states(&states);
|
2008-06-10 16:51:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:25 +01: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_GROUP("set-head specific 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()
|
|
|
|
};
|
|
|
|
argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
|
|
|
|
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
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&buf2);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-06-10 16:51:21 +02:00
|
|
|
static int prune(int argc, const char **argv)
|
|
|
|
{
|
2008-06-10 16:51:35 +02:00
|
|
|
int dry_run = 0, result = 0;
|
2008-06-10 16:51:21 +02:00
|
|
|
struct option options[] = {
|
|
|
|
OPT_GROUP("prune specific options"),
|
2008-06-10 16:51:35 +02:00
|
|
|
OPT__DRY_RUN(&dry_run),
|
2008-06-10 16:51:21 +02:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
struct ref_states states;
|
2009-02-09 08:27:10 +01:00
|
|
|
const char *dangling_msg;
|
2008-06-10 16:51:21 +02:00
|
|
|
|
|
|
|
argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage_with_options(builtin_remote_usage, options);
|
|
|
|
|
2009-02-09 08:27:10 +01:00
|
|
|
dangling_msg = (dry_run
|
|
|
|
? " %s will become dangling!\n"
|
|
|
|
: " %s has become dangling!\n");
|
|
|
|
|
2008-06-10 16:51:21 +02:00
|
|
|
memset(&states, 0, sizeof(states));
|
|
|
|
for (; argc; argc--, argv++) {
|
|
|
|
int i;
|
|
|
|
|
2009-02-25 09:32:24 +01:00
|
|
|
get_remote_ref_states(*argv, &states, GET_REF_STATES);
|
2008-06-10 16:51:35 +02:00
|
|
|
|
2008-06-12 08:43:25 +02:00
|
|
|
if (states.stale.nr) {
|
|
|
|
printf("Pruning %s\n", *argv);
|
2008-06-10 16:51:35 +02:00
|
|
|
printf("URL: %s\n",
|
|
|
|
states.remote->url_nr
|
|
|
|
? states.remote->url[0]
|
|
|
|
: "(no URL)");
|
2008-06-12 08:43:25 +02:00
|
|
|
}
|
2008-06-10 16:51:21 +02:00
|
|
|
|
|
|
|
for (i = 0; i < states.stale.nr; i++) {
|
|
|
|
const char *refname = states.stale.items[i].util;
|
2008-06-10 16:51:35 +02:00
|
|
|
|
|
|
|
if (!dry_run)
|
2008-10-26 03:33:56 +01:00
|
|
|
result |= delete_ref(refname, NULL, 0);
|
2008-06-10 16:51:35 +02:00
|
|
|
|
|
|
|
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
|
2008-07-18 06:30:33 +02:00
|
|
|
abbrev_ref(refname, "refs/remotes/"));
|
2009-02-09 08:27:10 +01:00
|
|
|
warn_dangling_symref(dangling_msg, refname);
|
2008-06-10 16:51:21 +02:00
|
|
|
}
|
|
|
|
|
2009-02-25 09:32:19 +01:00
|
|
|
free_remote_ref_states(&states);
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-03-04 12:23:53 +01:00
|
|
|
static int get_one_remote_for_update(struct remote *remote, void *priv)
|
2008-02-29 02:45:45 +01:00
|
|
|
{
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list *list = priv;
|
2008-02-29 02:45:45 +01:00
|
|
|
if (!remote->skip_default_update)
|
2008-11-17 16:54:07 +01:00
|
|
|
string_list_append(remote->name, list);
|
2008-03-04 12:23:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct remote_group {
|
|
|
|
const char *name;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list *list;
|
2008-03-04 12:23:53 +01:00
|
|
|
} remote_group;
|
|
|
|
|
2008-05-14 19:46:53 +02:00
|
|
|
static int get_remote_group(const char *key, const char *value, void *cb)
|
2008-03-04 12:23:53 +01:00
|
|
|
{
|
|
|
|
if (!prefixcmp(key, "remotes.") &&
|
|
|
|
!strcmp(key + 8, remote_group.name)) {
|
|
|
|
/* split list by white space */
|
|
|
|
int space = strcspn(value, " \t\n");
|
|
|
|
while (*value) {
|
|
|
|
if (space > 1)
|
2008-07-21 20:03:49 +02:00
|
|
|
string_list_append(xstrndup(value, space),
|
2008-03-04 12:23:53 +01:00
|
|
|
remote_group.list);
|
|
|
|
value += space + (value[space] != '\0');
|
|
|
|
space = strcspn(value, " \t\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-29 02:45:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int update(int argc, const char **argv)
|
|
|
|
{
|
2008-03-04 12:23:53 +01:00
|
|
|
int i, result = 0;
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list list = { NULL, 0, 0, 0 };
|
2008-03-04 12:23:53 +01:00
|
|
|
static const char *default_argv[] = { NULL, "default", NULL };
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-03-04 12:23:53 +01:00
|
|
|
if (argc < 2) {
|
|
|
|
argc = 2;
|
|
|
|
argv = default_argv;
|
|
|
|
}
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-03-04 12:23:53 +01:00
|
|
|
remote_group.list = &list;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
remote_group.name = argv[i];
|
2008-05-14 19:46:53 +02:00
|
|
|
result = git_config(get_remote_group, NULL);
|
2008-03-04 12:23:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
|
|
|
|
result = for_each_remote(get_one_remote_for_update, &list);
|
|
|
|
|
|
|
|
for (i = 0; i < list.nr; i++)
|
2008-07-21 20:03:49 +02:00
|
|
|
result |= fetch_remote(list.items[i].string);
|
2008-03-04 12:23:53 +01:00
|
|
|
|
|
|
|
/* all names were strdup()ed or strndup()ed */
|
2008-07-21 20:03:49 +02:00
|
|
|
list.strdup_strings = 1;
|
|
|
|
string_list_clear(&list, 0);
|
2008-03-04 12:23:53 +01:00
|
|
|
|
|
|
|
return result;
|
2008-02-29 02:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int get_one_entry(struct remote *remote, void *priv)
|
|
|
|
{
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list *list = priv;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
2008-09-22 10:57:51 +02:00
|
|
|
if (remote->url_nr > 0) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < remote->url_nr; i++)
|
|
|
|
string_list_append(remote->name, list)->util = (void *)remote->url[i];
|
|
|
|
} else
|
|
|
|
string_list_append(remote->name, list)->util = NULL;
|
2008-02-29 02:45:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_all(void)
|
|
|
|
{
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list list = { NULL, 0, 0 };
|
2008-02-29 02:45:45 +01:00
|
|
|
int result = for_each_remote(get_one_entry, &list);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
int i;
|
|
|
|
|
2008-07-21 20:03:49 +02:00
|
|
|
sort_string_list(&list);
|
2008-02-29 02:45:45 +01:00
|
|
|
for (i = 0; i < list.nr; i++) {
|
2008-07-21 20:03:49 +02:00
|
|
|
struct string_list_item *item = list.items + i;
|
2008-09-22 10:57:51 +02: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 02:45:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_remote(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
struct option options[] = {
|
|
|
|
OPT__VERBOSE(&verbose),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
int result;
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, options, builtin_remote_usage,
|
|
|
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
result = show_all();
|
|
|
|
else if (!strcmp(argv[0], "add"))
|
|
|
|
result = add(argc, argv);
|
2008-11-03 19:26:18 +01:00
|
|
|
else if (!strcmp(argv[0], "rename"))
|
|
|
|
result = mv(argc, argv);
|
2008-02-29 02:45:45 +01:00
|
|
|
else if (!strcmp(argv[0], "rm"))
|
|
|
|
result = rm(argc, argv);
|
2009-02-25 09:32:25 +01:00
|
|
|
else if (!strcmp(argv[0], "set-head"))
|
|
|
|
result = set_head(argc, argv);
|
2008-02-29 02:45:45 +01:00
|
|
|
else if (!strcmp(argv[0], "show"))
|
2008-06-10 16:51:21 +02:00
|
|
|
result = show(argc, argv);
|
2008-02-29 02:45:45 +01:00
|
|
|
else if (!strcmp(argv[0], "prune"))
|
2008-06-10 16:51:21 +02:00
|
|
|
result = prune(argc, argv);
|
2008-02-29 02:45:45 +01: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;
|
|
|
|
}
|