Merge branch 'om/remote-fix'

* om/remote-fix:
  "remote prune": be quiet when there is nothing to prune
  remote show: list tracked remote branches with -n
  remote prune: print the list of pruned branches
  builtin-remote: split show_or_prune() in two separate functions
  remote show: fix the -n option
This commit is contained in:
Junio C Hamano 2008-06-12 22:55:44 -07:00
commit dc92cc20f8
3 changed files with 153 additions and 51 deletions

View File

@ -12,8 +12,8 @@ SYNOPSIS
'git-remote' [-v | --verbose] 'git-remote' [-v | --verbose]
'git-remote' add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url> 'git-remote' add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>
'git-remote' rm <name> 'git-remote' rm <name>
'git-remote' show <name> 'git-remote' show [-n] <name>
'git-remote' prune <name> 'git-remote' prune [-n | --dry-run] <name>
'git-remote' update [group] 'git-remote' update [group]
DESCRIPTION DESCRIPTION
@ -80,9 +80,8 @@ These stale branches have already been removed from the remote repository
referenced by <name>, but are still locally available in referenced by <name>, but are still locally available in
"remotes/<name>". "remotes/<name>".
+ +
With `-n` option, the remote heads are not confirmed first with `git With `--dry-run` option, report what branches will be pruned, but do no
ls-remote <name>`; cached information is used instead. Use with actually prune them.
caution.
'update':: 'update'::

View File

@ -419,52 +419,68 @@ static void show_list(const char *title, struct path_list *list)
printf("\n"); 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)) {
path_list_append(skip_prefix(refspec.src, "refs/heads/"),
&states->tracked);
}
return 0;
}
static int show(int argc, const char **argv)
{
int no_query = 0, result = 0;
struct option options[] = { struct option options[] = {
OPT_GROUP("show specific options"), OPT_GROUP("show specific options"),
OPT__DRY_RUN(&dry_run), OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
OPT_END() OPT_END()
}; };
struct ref_states states; struct ref_states states;
argc = parse_options(argc, argv, options, builtin_remote_usage, 0); argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
if (argc < 1) { if (argc < 1)
if (!prune) return show_all();
return show_all();
usage_with_options(builtin_remote_usage, options);
}
memset(&states, 0, sizeof(states)); memset(&states, 0, sizeof(states));
for (; argc; argc--, argv++) { for (; argc; argc--, argv++) {
struct transport *transport;
const struct ref *ref;
struct strbuf buf; struct strbuf buf;
int i, got_states; int i;
states.remote = remote_get(*argv); get_remote_ref_states(*argv, &states, !no_query);
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) {
for (i = 0; i < states.stale.nr; i++) {
const char *refname = states.stale.items[i].util;
result |= delete_ref(refname, NULL);
}
goto cleanup_states;
}
printf("* remote %s\n URL: %s\n", *argv, printf("* remote %s\n URL: %s\n", *argv,
states.remote->url_nr > 0 ? states.remote->url_nr > 0 ?
@ -486,17 +502,19 @@ static int show_or_prune(int argc, const char **argv, int prune)
printf("\n"); printf("\n");
} }
if (got_states) if (!no_query) {
continue; strbuf_init(&buf, 0);
strbuf_init(&buf, 0); strbuf_addf(&buf, " New remote branch%%s (next fetch "
strbuf_addf(&buf, " New remote branch%%s (next fetch will " "will store in remotes/%s)", states.remote->name);
"store in remotes/%s)", states.remote->name); show_list(buf.buf, &states.new);
show_list(buf.buf, &states.new); strbuf_release(&buf);
strbuf_release(&buf); show_list(" Stale tracking branch%s (use 'git remote "
show_list(" Stale tracking branch%s (use 'git remote prune')", "prune')", &states.stale);
&states.stale); }
show_list(" Tracked remote branch%s",
&states.tracked); 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) { if (states.remote->push_refspec_nr) {
printf(" Local branch%s pushed with 'git push'\n ", printf(" Local branch%s pushed with 'git push'\n ",
@ -511,7 +529,55 @@ static int show_or_prune(int argc, const char **argv, int prune)
} }
printf("\n"); printf("\n");
} }
cleanup_states:
/* NEEDSWORK: free remote */
path_list_clear(&states.new, 0);
path_list_clear(&states.stale, 0);
path_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",
skip_prefix(refname, "refs/remotes/"));
}
/* NEEDSWORK: free remote */ /* NEEDSWORK: free remote */
path_list_clear(&states.new, 0); path_list_clear(&states.new, 0);
path_list_clear(&states.stale, 0); path_list_clear(&states.stale, 0);
@ -632,9 +698,9 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
else if (!strcmp(argv[0], "rm")) else if (!strcmp(argv[0], "rm"))
result = rm(argc, argv); result = rm(argc, argv);
else if (!strcmp(argv[0], "show")) else if (!strcmp(argv[0], "show"))
result = show_or_prune(argc, argv, 0); result = show(argc, argv);
else if (!strcmp(argv[0], "prune")) else if (!strcmp(argv[0], "prune"))
result = show_or_prune(argc, argv, 1); result = prune(argc, argv);
else if (!strcmp(argv[0], "update")) else if (!strcmp(argv[0], "update"))
result = update(argc, argv); result = update(argc, argv);
else { else {

View File

@ -138,6 +138,25 @@ test_expect_success 'show' '
test_cmp expect output) test_cmp expect output)
' '
cat > test/expect << EOF
* remote origin
URL: $(pwd)/one/.git
Remote branch merged with 'git pull' while on branch master
master
Tracked remote branches
master side
Local branches pushed with 'git push'
master:upstream +refs/tags/lastbackup
EOF
test_expect_success 'show -n' '
(mv one one.unreachable &&
cd test &&
git remote show -n origin > output &&
mv ../one.unreachable ../one &&
test_cmp expect output)
'
test_expect_success 'prune' ' test_expect_success 'prune' '
(cd one && (cd one &&
git branch -m side side2) && git branch -m side side2) &&
@ -148,6 +167,24 @@ test_expect_success 'prune' '
! git rev-parse refs/remotes/origin/side) ! git rev-parse refs/remotes/origin/side)
' '
cat > test/expect << EOF
Pruning origin
URL: $(pwd)/one/.git
* [would prune] origin/side2
EOF
test_expect_success 'prune --dry-run' '
(cd one &&
git branch -m side2 side) &&
(cd test &&
git remote prune --dry-run origin > output &&
git rev-parse refs/remotes/origin/side2 &&
! git rev-parse refs/remotes/origin/side &&
(cd ../one &&
git branch -m side side2) &&
test_cmp expect output)
'
test_expect_success 'add --mirror && prune' ' test_expect_success 'add --mirror && prune' '
(mkdir mirror && (mkdir mirror &&
cd mirror && cd mirror &&