git-add --intent-to-add (-N)
[gitweb.git] / builtin-remote.c
index 99a34dfe86a79df353d3c094cb2ebf71fdca1cc0..01945a8651c9a42f19fba850036ed2a1339675f1 100644 (file)
@@ -2,7 +2,7 @@
 #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"
@@ -29,19 +29,13 @@ static inline int postfixcmp(const char *string, const char *postfix)
        return strcmp(string + len1 - len2, postfix);
 }
 
-static inline const char *skip_prefix(const char *name, const char *prefix)
-{
-       return !name ? "" :
-               prefixcmp(name, prefix) ?  name : name + strlen(prefix);
-}
-
 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;
 }
 
@@ -57,7 +51,7 @@ static int fetch_remote(const char *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;
@@ -102,18 +96,18 @@ static int add(int argc, const char **argv)
        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;
        }
@@ -121,7 +115,7 @@ static int add(int argc, const char **argv)
        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;
        }
 
@@ -141,23 +135,32 @@ static int add(int argc, const char **argv)
 
        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;
 
@@ -171,7 +174,7 @@ static int config_read_branches(const char *key, const char *value, void *cb)
                } 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);
@@ -182,15 +185,15 @@ static int config_read_branches(const char *key, const char *value, void *cb)
                        info->remote = xstrdup(value);
                } else {
                        char *space = strchr(value, ' ');
-                       value = skip_prefix(value, "refs/heads/");
+                       value = abbrev_branch(value);
                        while (space) {
                                char *merge;
                                merge = xstrndup(value, space - value);
-                               path_list_append(merge, &info->merge);
-                               value = skip_prefix(space + 1, "refs/heads/");
+                               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;
@@ -201,13 +204,12 @@ static void read_branches(void)
        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 strbuf remote_prefix;
-       struct path_list new, stale, tracked;
+       struct string_list new, stale, tracked;
 };
 
 static int handle_one_branch(const char *refname,
@@ -219,16 +221,16 @@ 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 = skip_prefix(refspec.src, "refs/heads/");
+               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;
@@ -244,9 +246,9 @@ static int get_ref_states(const struct ref *ref, struct ref_states *states)
                        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;
 
@@ -257,50 +259,85 @@ static int get_ref_states(const struct ref *ref, struct ref_states *states)
                        if (hashcmp(sha1, ref->new_sha1))
                                util = &states;
                }
-               path_list_append(skip_prefix(ref->name, "refs/heads/"),
-                               target)->util = util;
+               string_list_append(abbrev_branch(ref->name), target)->util = util;
        }
        free_refs(fetch_map);
 
-       strbuf_addf(&states->remote_prefix,
-               "refs/remotes/%s/", states->remote->name);
        for_each_ref(handle_one_branch, states);
-       sort_path_list(&states->stale);
+       sort_string_list(&states->stale);
+
+       return 0;
+}
+
+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;
 }
 
 struct branches_for_remote {
-       const char *prefix;
-       struct path_list *branches;
+       struct remote *remote;
+       struct string_list *branches;
+       struct known_remotes *keep;
 };
 
 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;
+       struct refspec refspec;
+       struct string_list_item *item;
+       struct known_remote *kr;
 
-       if (!prefixcmp(refname, branches->prefix)) {
-               struct path_list_item *item;
+       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;
+       }
 
-               /* make sure that symrefs are deleted */
-               if (flags & REF_ISSYMREF)
-                       return unlink(git_path(refname));
+       /* make sure that symrefs are deleted */
+       if (flags & REF_ISSYMREF)
+               return unlink(git_path(refname));
 
-               item = path_list_append(refname, branches->branches);
-               item->util = xmalloc(20);
-               hashcpy(item->util, sha1);
-       }
+       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)
+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))
@@ -316,8 +353,9 @@ static int rm(int argc, const char **argv)
        };
        struct remote *remote;
        struct strbuf buf;
-       struct path_list branches = { NULL, 0, 0, 1 };
-       struct branches_for_remote cb_data = { NULL, &branches };
+       struct known_remotes known_remotes = { NULL, NULL };
+       struct string_list branches = { NULL, 0, 0, 1 };
+       struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
        int i;
 
        if (argc != 2)
@@ -327,6 +365,9 @@ static int rm(int argc, const char **argv)
        if (!remote)
                die("No such remote: %s", argv[1]);
 
+       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)
@@ -334,14 +375,14 @@ static int rm(int argc, const char **argv)
 
        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;
@@ -355,20 +396,18 @@ static int rm(int argc, const char **argv)
         * the branches one by one, since for_each_ref() relies on cached
         * refs, which are invalidated when deleting a branch.
         */
-       strbuf_reset(&buf);
-       strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
-       cb_data.prefix = buf.buf;
+       cb_data.remote = remote;
        i = for_each_ref(add_branch_for_removal, &cb_data);
        strbuf_release(&buf);
 
        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)
 {
        int i;
 
@@ -378,80 +417,77 @@ static void show_list(const char *title, struct path_list *list)
        printf(title, list->nr > 1 ? "es" : "");
        printf("\n    ");
        for (i = 0; i < list->nr; i++)
-               printf("%s%s", i ? " " : "", list->items[i].path);
+               printf("%s%s", i ? " " : "", list->items[i].string);
        printf("\n");
 }
 
-static int show_or_prune(int argc, const char **argv, int prune)
+static int get_remote_ref_states(const char *name,
+                                struct ref_states *states,
+                                int query)
 {
-       int dry_run = 0, result = 0;
+       struct transport *transport;
+       const struct ref *ref;
+
+       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);
+               ref = transport_get_remote_refs(transport);
+               transport_disconnect(transport);
+
+               get_ref_states(ref, states);
+       }
+
+       return 0;
+}
+
+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;
+
+       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;
+}
+
+static int show(int argc, const char **argv)
+{
+       int no_query = 0, result = 0;
        struct option options[] = {
                OPT_GROUP("show specific options"),
-               OPT__DRY_RUN(&dry_run),
+               OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
                OPT_END()
        };
        struct ref_states states;
 
        argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 
-       if (argc < 1) {
-               if (!prune)
-                       return show_all();
-               usage_with_options(builtin_remote_usage, options);
-       }
+       if (argc < 1)
+               return show_all();
 
        memset(&states, 0, sizeof(states));
        for (; argc; argc--, argv++) {
-               struct transport *transport;
-               const struct ref *ref;
                struct strbuf buf;
-               int i, got_states;
-
-               states.remote = remote_get(*argv);
-               if (!states.remote)
-                       return error("No such remote: %s", *argv);
-               transport = transport_get(NULL, states.remote->url_nr > 0 ?
-                       states.remote->url[0] : NULL);
-               ref = transport_get_remote_refs(transport);
-               transport_disconnect(transport);
-
-               read_branches();
-               got_states = get_ref_states(ref, &states);
-               if (got_states)
-                       result = error("Error getting local info for '%s'",
-                                       states.remote->name);
-
-               if (prune) {
-                       struct strbuf buf;
-                       int prefix_len;
+               int i;
 
-                       strbuf_init(&buf, 0);
-                       if (states.remote->fetch_refspec_nr == 1 &&
-                                       states.remote->fetch->pattern &&
-                                       !strcmp(states.remote->fetch->src,
-                                               states.remote->fetch->dst))
-                               /* handle --mirror remote */
-                               strbuf_addstr(&buf, "refs/heads/");
-                       else
-                               strbuf_addf(&buf, "refs/remotes/%s/", *argv);
-                       prefix_len = buf.len;
-
-                       for (i = 0; i < states.stale.nr; i++) {
-                               strbuf_setlen(&buf, prefix_len);
-                               strbuf_addstr(&buf, states.stale.items[i].path);
-                               result |= delete_ref(buf.buf, NULL);
-                       }
-
-                       strbuf_release(&buf);
-                       goto cleanup_states;
-               }
+               get_remote_ref_states(*argv, &states, !no_query);
 
                printf("* remote %s\n  URL: %s\n", *argv,
                        states.remote->url_nr > 0 ?
                                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;
 
@@ -460,23 +496,25 @@ static int show_or_prune(int argc, const char **argv, int prune)
                        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 (got_states)
-                       continue;
-               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("  Stale tracking branch%s (use 'git remote prune')",
-                               &states.stale);
-               show_list("  Tracked remote branch%s",
-                               &states.tracked);
+               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("  Stale tracking branch%s (use 'git remote "
+                               "prune')", &states.stale);
+               }
+
+               if (no_query)
+                       for_each_ref(append_ref_to_tracked_list, &states);
+               show_list("  Tracked remote branch%s", &states.tracked);
 
                if (states.remote->push_refspec_nr) {
                        printf("  Local branch%s pushed with 'git push'\n   ",
@@ -485,17 +523,65 @@ static int show_or_prune(int argc, const char **argv, int prune)
                        for (i = 0; i < states.remote->push_refspec_nr; i++) {
                                struct refspec *spec = states.remote->push + i;
                                printf(" %s%s%s%s", spec->force ? "+" : "",
-                                       skip_prefix(spec->src, "refs/heads/"),
-                                       spec->dst ? ":" : "",
-                                       skip_prefix(spec->dst, "refs/heads/"));
+                                      abbrev_branch(spec->src),
+                                      spec->dst ? ":" : "",
+                                      spec->dst ? abbrev_branch(spec->dst) : "");
                        }
                        printf("\n");
                }
-cleanup_states:
+
+               /* NEEDSWORK: free remote */
+               string_list_clear(&states.new, 0);
+               string_list_clear(&states.stale, 0);
+               string_list_clear(&states.tracked, 0);
+       }
+
+       return result;
+}
+
+static int prune(int argc, const char **argv)
+{
+       int dry_run = 0, result = 0;
+       struct option options[] = {
+               OPT_GROUP("prune specific options"),
+               OPT__DRY_RUN(&dry_run),
+               OPT_END()
+       };
+       struct ref_states states;
+
+       argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
+
+       if (argc < 1)
+               usage_with_options(builtin_remote_usage, options);
+
+       memset(&states, 0, sizeof(states));
+       for (; argc; argc--, argv++) {
+               int i;
+
+               get_remote_ref_states(*argv, &states, 1);
+
+               if (states.stale.nr) {
+                       printf("Pruning %s\n", *argv);
+                       printf("URL: %s\n",
+                              states.remote->url_nr
+                              ? states.remote->url[0]
+                              : "(no URL)");
+               }
+
+               for (i = 0; i < states.stale.nr; i++) {
+                       const char *refname = states.stale.items[i].util;
+
+                       if (!dry_run)
+                               result |= delete_ref(refname, NULL);
+
+                       printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
+                              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;
@@ -503,15 +589,15 @@ static int show_or_prune(int argc, const char **argv, int prune)
 
 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(xstrdup(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)
@@ -522,7 +608,7 @@ 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");
@@ -535,7 +621,7 @@ static int get_remote_group(const char *key, const char *value, void *cb)
 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) {
@@ -553,20 +639,20 @@ static int update(int argc, const char **argv)
                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 ?
+       string_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);
@@ -576,16 +662,16 @@ static int get_one_entry(struct remote *remote, void *priv)
 
 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,
+                       struct string_list_item *item = list.items + i;
+                       printf("%s%s%s\n", item->string,
                                verbose ? "\t" : "",
                                verbose && item->util ?
                                        (const char *)item->util : "");
@@ -612,9 +698,9 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
        else if (!strcmp(argv[0], "rm"))
                result = rm(argc, argv);
        else if (!strcmp(argv[0], "show"))
-               result = show_or_prune(argc, argv, 0);
+               result = show(argc, argv);
        else if (!strcmp(argv[0], "prune"))
-               result = show_or_prune(argc, argv, 1);
+               result = prune(argc, argv);
        else if (!strcmp(argv[0], "update"))
                result = update(argc, argv);
        else {