#include "parse-options.h"
#include "transport.h"
#include "remote.h"
-#include "path-list.h"
+#include "string-list.h"
#include "strbuf.h"
#include "run-command.h"
#include "refs.h"
static const char * const builtin_remote_usage[] = {
- "git remote",
- "git remote add <name> <url>",
+ "git remote [-v | --verbose]",
+ "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
+ "git remote rename <old> <new>",
"git remote rm <name>",
- "git remote show <name>",
- "git remote prune <name>",
- "git remote update [group]",
+ "git remote show [-n] <name>",
+ "git remote prune [-n | --dry-run] <name>",
+ "git remote [-v | --verbose] update [group]",
NULL
};
static int opt_parse_track(const struct option *opt, const char *arg, int not)
{
- struct path_list *list = opt->value;
+ struct string_list *list = opt->value;
if (not)
- path_list_clear(list, 0);
+ string_list_clear(list, 0);
else
- path_list_append(arg, list);
+ string_list_append(arg, list);
return 0;
}
static int fetch_remote(const char *name)
{
- const char *argv[] = { "fetch", name, NULL };
+ const char *argv[] = { "fetch", name, NULL, NULL };
+ if (verbose) {
+ argv[1] = "-v";
+ argv[2] = name;
+ }
printf("Updating %s\n", name);
if (run_command_v_opt(argv, RUN_GIT_CMD))
return error("Could not fetch %s", name);
static int add(int argc, const char **argv)
{
int fetch = 0, mirror = 0;
- struct path_list track = { NULL, 0, 0 };
+ struct string_list track = { NULL, 0, 0 };
const char *master = NULL;
struct remote *remote;
- struct strbuf buf, buf2;
+ struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
const char *name, *url;
int i;
remote->fetch_refspec_nr))
die("remote %s already exists.", name);
- strbuf_init(&buf, 0);
- strbuf_init(&buf2, 0);
-
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);
strbuf_addf(&buf, "remote.%s.fetch", name);
if (track.nr == 0)
- path_list_append("*", &track);
+ string_list_append("*", &track);
for (i = 0; i < track.nr; i++) {
- struct path_list_item *item = track.items + i;
+ struct string_list_item *item = track.items + i;
strbuf_reset(&buf2);
strbuf_addch(&buf2, '+');
if (mirror)
strbuf_addf(&buf2, "refs/%s:refs/%s",
- item->path, item->path);
+ item->string, item->string);
else
strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
- item->path, name, item->path);
+ item->string, name, item->string);
if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
return 1;
}
if (mirror) {
strbuf_reset(&buf);
strbuf_addf(&buf, "remote.%s.mirror", name);
- if (git_config_set(buf.buf, "yes"))
+ if (git_config_set(buf.buf, "true"))
return 1;
}
strbuf_release(&buf);
strbuf_release(&buf2);
- path_list_clear(&track, 0);
+ string_list_clear(&track, 0);
return 0;
}
struct branch_info {
char *remote;
- struct path_list merge;
+ struct string_list merge;
};
-static struct path_list branch_list;
+static struct string_list branch_list;
+
+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/")
static int config_read_branches(const char *key, const char *value, void *cb)
{
if (!prefixcmp(key, "branch.")) {
char *name;
- struct path_list_item *item;
+ struct string_list_item *item;
struct branch_info *info;
enum { REMOTE, MERGE } type;
} else
return 0;
- item = path_list_insert(name, &branch_list);
+ item = string_list_insert(name, &branch_list);
if (!item->util)
item->util = xcalloc(sizeof(struct branch_info), 1);
info->remote = xstrdup(value);
} else {
char *space = strchr(value, ' ');
- const char *ptr = skip_prefix(value, "refs/heads/");
- if (ptr)
- value = ptr;
+ value = abbrev_branch(value);
while (space) {
char *merge;
merge = xstrndup(value, space - value);
- path_list_append(merge, &info->merge);
- ptr = skip_prefix(space + 1, "refs/heads/");
- if (ptr)
- value = ptr;
- else
- value = space + 1;
+ string_list_append(merge, &info->merge);
+ value = abbrev_branch(space + 1);
space = strchr(value, ' ');
}
- path_list_append(xstrdup(value), &info->merge);
+ string_list_append(xstrdup(value), &info->merge);
}
}
return 0;
if (branch_list.nr)
return;
git_config(config_read_branches, NULL);
- sort_path_list(&branch_list);
+ sort_string_list(&branch_list);
}
struct ref_states {
struct remote *remote;
- struct path_list new, stale, tracked;
+ struct string_list new, stale, tracked;
};
static int handle_one_branch(const char *refname,
memset(&refspec, 0, sizeof(refspec));
refspec.dst = (char *)refname;
if (!remote_find_tracking(states->remote, &refspec)) {
- struct path_list_item *item;
- const char *name, *ptr;
- ptr = skip_prefix(refspec.src, "refs/heads/");
- if (ptr)
- name = ptr;
- else
- name = refspec.src;
+ struct string_list_item *item;
+ const char *name = abbrev_branch(refspec.src);
/* symbolic refs pointing nowhere were handled already */
if ((flags & REF_ISSYMREF) ||
- unsorted_path_list_has_path(&states->tracked,
+ unsorted_string_list_has_string(&states->tracked,
name) ||
- unsorted_path_list_has_path(&states->new,
+ unsorted_string_list_has_string(&states->new,
name))
return 0;
- item = path_list_append(name, &states->stale);
+ item = string_list_append(name, &states->stale);
item->util = xstrdup(refname);
}
return 0;
die("Could not get fetch map for refspec %s",
states->remote->fetch_refspec[i]);
- states->new.strdup_paths = states->tracked.strdup_paths = 1;
+ states->new.strdup_strings = states->tracked.strdup_strings = 1;
for (ref = fetch_map; ref; ref = ref->next) {
- struct path_list *target = &states->tracked;
+ struct string_list *target = &states->tracked;
unsigned char sha1[20];
void *util = NULL;
- const char *ptr;
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
target = &states->new;
if (hashcmp(sha1, ref->new_sha1))
util = &states;
}
- ptr = skip_prefix(ref->name, "refs/heads/");
- if (!ptr)
- ptr = ref->name;
- path_list_append(ptr, target)->util = util;
+ string_list_append(abbrev_branch(ref->name), target)->util = util;
}
free_refs(fetch_map);
for_each_ref(handle_one_branch, states);
- sort_path_list(&states->stale);
+ sort_string_list(&states->stale);
return 0;
}
struct branches_for_remote {
struct remote *remote;
- struct path_list *branches;
+ struct string_list *branches;
struct known_remotes *keep;
};
{
struct branches_for_remote *branches = cb_data;
struct refspec refspec;
- struct path_list_item *item;
+ struct string_list_item *item;
struct known_remote *kr;
memset(&refspec, 0, sizeof(refspec));
/* make sure that symrefs are deleted */
if (flags & REF_ISSYMREF)
- return unlink(git_path(refname));
+ return unlink(git_path("%s", refname));
- item = path_list_append(refname, branches->branches);
+ item = string_list_append(refname, branches->branches);
item->util = xmalloc(20);
hashcpy(item->util, sha1);
return 0;
}
-static int remove_branches(struct path_list *branches)
+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;
+}
+
+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;
+}
+
+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);
+
+ if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
+ return migrate_file(oldremote);
+
+ 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;
+ if (info->remote && !strcmp(info->remote, rename.old)) {
+ 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;
+}
+
+static int remove_branches(struct string_list *branches)
{
int i, result = 0;
for (i = 0; i < branches->nr; i++) {
- struct path_list_item *item = branches->items + i;
- const char *refname = item->path;
+ struct string_list_item *item = branches->items + i;
+ const char *refname = item->string;
unsigned char *sha1 = item->util;
- if (delete_ref(refname, sha1))
+ if (delete_ref(refname, sha1, 0))
result |= error("Could not remove branch %s", refname);
}
return result;
OPT_END()
};
struct remote *remote;
- struct strbuf buf;
+ struct strbuf buf = STRBUF_INIT;
struct known_remotes known_remotes = { NULL, NULL };
- struct path_list branches = { NULL, 0, 0, 1 };
+ struct string_list branches = { NULL, 0, 0, 1 };
struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
int i;
known_remotes.to_delete = remote;
for_each_remote(add_known_remote, &known_remotes);
- strbuf_init(&buf, 0);
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++) {
- struct path_list_item *item = branch_list.items + i;
+ struct string_list_item *item = branch_list.items + i;
struct branch_info *info = item->util;
if (info->remote && !strcmp(info->remote, remote->name)) {
const char *keys[] = { "remote", "merge", NULL }, **k;
for (k = keys; *k; k++) {
strbuf_reset(&buf);
strbuf_addf(&buf, "branch.%s.%s",
- item->path, *k);
+ item->string, *k);
if (git_config_set(buf.buf, NULL)) {
strbuf_release(&buf);
return -1;
if (!i)
i = remove_branches(&branches);
- path_list_clear(&branches, 1);
+ string_list_clear(&branches, 1);
return i;
}
-static void show_list(const char *title, struct path_list *list)
+static void show_list(const char *title, struct string_list *list,
+ const char *extra_arg)
{
int i;
if (!list->nr)
return;
- printf(title, list->nr > 1 ? "es" : "");
- printf("\n ");
- for (i = 0; i < list->nr; i++)
- printf("%s%s", i ? " " : "", list->items[i].path);
+ printf(title, list->nr > 1 ? "es" : "", extra_arg);
printf("\n");
+ for (i = 0; i < list->nr; i++)
+ printf(" %s\n", list->items[i].string);
}
static int get_remote_ref_states(const char *name,
memset(&refspec, 0, sizeof(refspec));
refspec.dst = (char *)refname;
- if (!remote_find_tracking(states->remote, &refspec)) {
- path_list_append(skip_prefix(refspec.src, "refs/heads/"),
- &states->tracked);
- }
+ if (!remote_find_tracking(states->remote, &refspec))
+ string_list_append(abbrev_branch(refspec.src), &states->tracked);
return 0;
}
memset(&states, 0, sizeof(states));
for (; argc; argc--, argv++) {
- struct strbuf buf;
int i;
get_remote_ref_states(*argv, &states, !no_query);
states.remote->url[0] : "(no URL)");
for (i = 0; i < branch_list.nr; i++) {
- struct path_list_item *branch = branch_list.items + i;
+ struct string_list_item *branch = branch_list.items + i;
struct branch_info *info = branch->util;
int j;
printf(" Remote branch%s merged with 'git pull' "
"while on branch %s\n ",
info->merge.nr > 1 ? "es" : "",
- branch->path);
+ branch->string);
for (j = 0; j < info->merge.nr; j++)
- printf(" %s", info->merge.items[j].path);
+ printf(" %s", info->merge.items[j].string);
printf("\n");
}
if (!no_query) {
- strbuf_init(&buf, 0);
- strbuf_addf(&buf, " New remote branch%%s (next fetch "
- "will store in remotes/%s)", states.remote->name);
- show_list(buf.buf, &states.new);
- strbuf_release(&buf);
+ show_list(" New remote branch%s (next fetch "
+ "will store in remotes/%s)",
+ &states.new, states.remote->name);
show_list(" Stale tracking branch%s (use 'git remote "
- "prune')", &states.stale);
+ "prune')", &states.stale, "");
}
if (no_query)
for_each_ref(append_ref_to_tracked_list, &states);
- show_list(" Tracked remote branch%s", &states.tracked);
+ show_list(" Tracked remote branch%s", &states.tracked, "");
if (states.remote->push_refspec_nr) {
- printf(" Local branch%s pushed with 'git push'\n ",
+ printf(" Local branch%s pushed with 'git push'\n",
states.remote->push_refspec_nr > 1 ?
"es" : "");
for (i = 0; i < states.remote->push_refspec_nr; i++) {
struct refspec *spec = states.remote->push + i;
- const char *p = "", *q = "";
- if (spec->src)
- p = skip_prefix(spec->src, "refs/heads/");
- if (spec->dst)
- q = skip_prefix(spec->dst, "refs/heads/");
- printf(" %s%s%s%s", spec->force ? "+" : "",
- p ? p : spec->src,
- spec->dst ? ":" : "",
- q ? q : spec->dst);
+ printf(" %s%s%s%s\n",
+ spec->force ? "+" : "",
+ abbrev_branch(spec->src),
+ spec->dst ? ":" : "",
+ spec->dst ? abbrev_branch(spec->dst) : "");
}
- printf("\n");
}
/* NEEDSWORK: free remote */
- path_list_clear(&states.new, 0);
- path_list_clear(&states.stale, 0);
- path_list_clear(&states.tracked, 0);
+ string_list_clear(&states.new, 0);
+ string_list_clear(&states.stale, 0);
+ string_list_clear(&states.tracked, 0);
}
return result;
const char *refname = states.stale.items[i].util;
if (!dry_run)
- result |= delete_ref(refname, NULL);
+ result |= delete_ref(refname, NULL, 0);
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
- skip_prefix(refname, "refs/remotes/"));
+ abbrev_ref(refname, "refs/remotes/"));
}
/* NEEDSWORK: free remote */
- path_list_clear(&states.new, 0);
- path_list_clear(&states.stale, 0);
- path_list_clear(&states.tracked, 0);
+ string_list_clear(&states.new, 0);
+ string_list_clear(&states.stale, 0);
+ string_list_clear(&states.tracked, 0);
}
return result;
static int get_one_remote_for_update(struct remote *remote, void *priv)
{
- struct path_list *list = priv;
+ struct string_list *list = priv;
if (!remote->skip_default_update)
- path_list_append(xstrdup(remote->name), list);
+ string_list_append(remote->name, list);
return 0;
}
struct remote_group {
const char *name;
- struct path_list *list;
+ struct string_list *list;
} remote_group;
static int get_remote_group(const char *key, const char *value, void *cb)
int space = strcspn(value, " \t\n");
while (*value) {
if (space > 1)
- path_list_append(xstrndup(value, space),
+ string_list_append(xstrndup(value, space),
remote_group.list);
value += space + (value[space] != '\0');
space = strcspn(value, " \t\n");
static int update(int argc, const char **argv)
{
int i, result = 0;
- struct path_list list = { NULL, 0, 0, 0 };
+ struct string_list list = { NULL, 0, 0, 0 };
static const char *default_argv[] = { NULL, "default", NULL };
if (argc < 2) {
result = for_each_remote(get_one_remote_for_update, &list);
for (i = 0; i < list.nr; i++)
- result |= fetch_remote(list.items[i].path);
+ result |= fetch_remote(list.items[i].string);
/* all names were strdup()ed or strndup()ed */
- list.strdup_paths = 1;
- path_list_clear(&list, 0);
+ list.strdup_strings = 1;
+ string_list_clear(&list, 0);
return result;
}
static int get_one_entry(struct remote *remote, void *priv)
{
- struct path_list *list = priv;
+ struct string_list *list = priv;
- path_list_append(remote->name, list)->util = remote->url_nr ?
- (void *)remote->url[0] : NULL;
- if (remote->url_nr > 1)
- warning("Remote %s has more than one URL", remote->name);
+ 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;
return 0;
}
static int show_all(void)
{
- struct path_list list = { NULL, 0, 0 };
+ struct string_list list = { NULL, 0, 0 };
int result = for_each_remote(get_one_entry, &list);
if (!result) {
int i;
- sort_path_list(&list);
+ sort_string_list(&list);
for (i = 0; i < list.nr; i++) {
- struct path_list_item *item = list.items + i;
- printf("%s%s%s\n", item->path,
- verbose ? "\t" : "",
- verbose && item->util ?
- (const char *)item->util : "");
+ struct string_list_item *item = list.items + i;
+ 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);
+ }
}
}
return result;
result = show_all();
else if (!strcmp(argv[0], "add"))
result = add(argc, argv);
+ else if (!strcmp(argv[0], "rename"))
+ result = mv(argc, argv);
else if (!strcmp(argv[0], "rm"))
result = rm(argc, argv);
else if (!strcmp(argv[0], "show"))