Merge branch 'fg/remote-prune'
* fg/remote-prune: add tests for remote groups git remote update: Fallback to remote if group does not exist remote: New function remote_is_configured() git remote update: Report error for non-existing groups git remote update: New option --prune builtin-remote.c: Split out prune_remote as a separate function.
This commit is contained in:
@ -16,7 +16,7 @@ SYNOPSIS
|
|||||||
'git remote set-head' <name> [-a | -d | <branch>]
|
'git remote set-head' <name> [-a | -d | <branch>]
|
||||||
'git remote show' [-n] <name>
|
'git remote show' [-n] <name>
|
||||||
'git remote prune' [-n | --dry-run] <name>
|
'git remote prune' [-n | --dry-run] <name>
|
||||||
'git remote update' [group]
|
'git remote update' [-p | --prune] [group | remote]...
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
@ -125,6 +125,8 @@ the configuration parameter remotes.default will get used; if
|
|||||||
remotes.default is not defined, all remotes which do not have the
|
remotes.default is not defined, all remotes which do not have the
|
||||||
configuration parameter remote.<name>.skipDefaultUpdate set to true will
|
configuration parameter remote.<name>.skipDefaultUpdate set to true will
|
||||||
be updated. (See linkgit:git-config[1]).
|
be updated. (See linkgit:git-config[1]).
|
||||||
|
+
|
||||||
|
With `--prune` option, prune all the remotes that are updated.
|
||||||
|
|
||||||
|
|
||||||
DISCUSSION
|
DISCUSSION
|
||||||
|
@ -15,7 +15,7 @@ static const char * const builtin_remote_usage[] = {
|
|||||||
"git remote set-head <name> [-a | -d | <branch>]",
|
"git remote set-head <name> [-a | -d | <branch>]",
|
||||||
"git remote show [-n] <name>",
|
"git remote show [-n] <name>",
|
||||||
"git remote prune [-n | --dry-run] <name>",
|
"git remote prune [-n | --dry-run] <name>",
|
||||||
"git remote [-v | --verbose] update [group]",
|
"git remote [-v | --verbose] update [-p | --prune] [group]",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,6 +26,7 @@ static const char * const builtin_remote_usage[] = {
|
|||||||
static int verbose;
|
static int verbose;
|
||||||
|
|
||||||
static int show_all(void);
|
static int show_all(void);
|
||||||
|
static int prune_remote(const char *remote, int dry_run);
|
||||||
|
|
||||||
static inline int postfixcmp(const char *string, const char *postfix)
|
static inline int postfixcmp(const char *string, const char *postfix)
|
||||||
{
|
{
|
||||||
@ -1128,46 +1129,49 @@ static int prune(int argc, const char **argv)
|
|||||||
OPT__DRY_RUN(&dry_run),
|
OPT__DRY_RUN(&dry_run),
|
||||||
OPT_END()
|
OPT_END()
|
||||||
};
|
};
|
||||||
struct ref_states states;
|
|
||||||
const char *dangling_msg;
|
|
||||||
|
|
||||||
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)
|
||||||
usage_with_options(builtin_remote_usage, options);
|
usage_with_options(builtin_remote_usage, options);
|
||||||
|
|
||||||
dangling_msg = (dry_run
|
for (; argc; argc--, argv++)
|
||||||
? " %s will become dangling!\n"
|
result |= prune_remote(*argv, dry_run);
|
||||||
: " %s has become dangling!\n");
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int prune_remote(const char *remote, int dry_run)
|
||||||
|
{
|
||||||
|
int result = 0, i;
|
||||||
|
struct ref_states states;
|
||||||
|
const char *dangling_msg = dry_run
|
||||||
|
? " %s will become dangling!\n"
|
||||||
|
: " %s has become dangling!\n";
|
||||||
|
|
||||||
memset(&states, 0, sizeof(states));
|
memset(&states, 0, sizeof(states));
|
||||||
for (; argc; argc--, argv++) {
|
get_remote_ref_states(remote, &states, GET_REF_STATES);
|
||||||
int i;
|
|
||||||
|
|
||||||
get_remote_ref_states(*argv, &states, GET_REF_STATES);
|
if (states.stale.nr) {
|
||||||
|
printf("Pruning %s\n", remote);
|
||||||
if (states.stale.nr) {
|
printf("URL: %s\n",
|
||||||
printf("Pruning %s\n", *argv);
|
states.remote->url_nr
|
||||||
printf("URL: %s\n",
|
? states.remote->url[0]
|
||||||
states.remote->url_nr
|
: "(no URL)");
|
||||||
? 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, 0);
|
|
||||||
|
|
||||||
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
|
|
||||||
abbrev_ref(refname, "refs/remotes/"));
|
|
||||||
warn_dangling_symref(dangling_msg, refname);
|
|
||||||
}
|
|
||||||
|
|
||||||
free_remote_ref_states(&states);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < states.stale.nr; i++) {
|
||||||
|
const char *refname = states.stale.items[i].util;
|
||||||
|
|
||||||
|
if (!dry_run)
|
||||||
|
result |= delete_ref(refname, NULL, 0);
|
||||||
|
|
||||||
|
printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
|
||||||
|
abbrev_ref(refname, "refs/remotes/"));
|
||||||
|
warn_dangling_symref(dangling_msg, refname);
|
||||||
|
}
|
||||||
|
|
||||||
|
free_remote_ref_states(&states);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1184,16 +1188,18 @@ struct remote_group {
|
|||||||
struct string_list *list;
|
struct string_list *list;
|
||||||
} remote_group;
|
} remote_group;
|
||||||
|
|
||||||
static int get_remote_group(const char *key, const char *value, void *cb)
|
static int get_remote_group(const char *key, const char *value, void *num_hits)
|
||||||
{
|
{
|
||||||
if (!prefixcmp(key, "remotes.") &&
|
if (!prefixcmp(key, "remotes.") &&
|
||||||
!strcmp(key + 8, remote_group.name)) {
|
!strcmp(key + 8, remote_group.name)) {
|
||||||
/* split list by white space */
|
/* split list by white space */
|
||||||
int space = strcspn(value, " \t\n");
|
int space = strcspn(value, " \t\n");
|
||||||
while (*value) {
|
while (*value) {
|
||||||
if (space > 1)
|
if (space > 1) {
|
||||||
string_list_append(xstrndup(value, space),
|
string_list_append(xstrndup(value, space),
|
||||||
remote_group.list);
|
remote_group.list);
|
||||||
|
++*((int *)num_hits);
|
||||||
|
}
|
||||||
value += space + (value[space] != '\0');
|
value += space + (value[space] != '\0');
|
||||||
space = strcspn(value, " \t\n");
|
space = strcspn(value, " \t\n");
|
||||||
}
|
}
|
||||||
@ -1204,10 +1210,18 @@ static int get_remote_group(const char *key, const char *value, void *cb)
|
|||||||
|
|
||||||
static int update(int argc, const char **argv)
|
static int update(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
int i, result = 0;
|
int i, result = 0, prune = 0;
|
||||||
struct string_list list = { NULL, 0, 0, 0 };
|
struct string_list list = { NULL, 0, 0, 0 };
|
||||||
static const char *default_argv[] = { NULL, "default", NULL };
|
static const char *default_argv[] = { NULL, "default", NULL };
|
||||||
|
struct option options[] = {
|
||||||
|
OPT_GROUP("update specific options"),
|
||||||
|
OPT_BOOLEAN('p', "prune", &prune,
|
||||||
|
"prune remotes after fecthing"),
|
||||||
|
OPT_END()
|
||||||
|
};
|
||||||
|
|
||||||
|
argc = parse_options(argc, argv, options, builtin_remote_usage,
|
||||||
|
PARSE_OPT_KEEP_ARGV0);
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
argc = 2;
|
argc = 2;
|
||||||
argv = default_argv;
|
argv = default_argv;
|
||||||
@ -1215,15 +1229,28 @@ static int update(int argc, const char **argv)
|
|||||||
|
|
||||||
remote_group.list = &list;
|
remote_group.list = &list;
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
|
int groups_found = 0;
|
||||||
remote_group.name = argv[i];
|
remote_group.name = argv[i];
|
||||||
result = git_config(get_remote_group, NULL);
|
result = git_config(get_remote_group, &groups_found);
|
||||||
|
if (!groups_found && (i != 1 || strcmp(argv[1], "default"))) {
|
||||||
|
struct remote *remote;
|
||||||
|
if (!remote_is_configured(argv[i]))
|
||||||
|
die("No such remote or remote group: %s",
|
||||||
|
argv[i]);
|
||||||
|
remote = remote_get(argv[i]);
|
||||||
|
string_list_append(remote->name, remote_group.list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
|
if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
|
||||||
result = for_each_remote(get_one_remote_for_update, &list);
|
result = for_each_remote(get_one_remote_for_update, &list);
|
||||||
|
|
||||||
for (i = 0; i < list.nr; i++)
|
for (i = 0; i < list.nr; i++) {
|
||||||
result |= fetch_remote(list.items[i].string);
|
int err = fetch_remote(list.items[i].string);
|
||||||
|
result |= err;
|
||||||
|
if (!err && prune)
|
||||||
|
result |= prune_remote(list.items[i].string, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/* all names were strdup()ed or strndup()ed */
|
/* all names were strdup()ed or strndup()ed */
|
||||||
list.strdup_strings = 1;
|
list.strdup_strings = 1;
|
||||||
|
11
remote.c
11
remote.c
@ -667,6 +667,17 @@ struct remote *remote_get(const char *name)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int remote_is_configured(const char *name)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
read_config();
|
||||||
|
|
||||||
|
for (i = 0; i < remotes_nr; i++)
|
||||||
|
if (!strcmp(name, remotes[i]->name))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int for_each_remote(each_remote_fn fn, void *priv)
|
int for_each_remote(each_remote_fn fn, void *priv)
|
||||||
{
|
{
|
||||||
int i, result = 0;
|
int i, result = 0;
|
||||||
|
1
remote.h
1
remote.h
@ -45,6 +45,7 @@ struct remote {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct remote *remote_get(const char *name);
|
struct remote *remote_get(const char *name);
|
||||||
|
int remote_is_configured(const char *name);
|
||||||
|
|
||||||
typedef int each_remote_fn(struct remote *remote, void *priv);
|
typedef int each_remote_fn(struct remote *remote, void *priv);
|
||||||
int for_each_remote(each_remote_fn fn, void *priv);
|
int for_each_remote(each_remote_fn fn, void *priv);
|
||||||
|
81
t/t5506-remote-groups.sh
Executable file
81
t/t5506-remote-groups.sh
Executable file
@ -0,0 +1,81 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
test_description='git remote group handling'
|
||||||
|
. ./test-lib.sh
|
||||||
|
|
||||||
|
mark() {
|
||||||
|
echo "$1" >mark
|
||||||
|
}
|
||||||
|
|
||||||
|
update_repo() {
|
||||||
|
(cd $1 &&
|
||||||
|
echo content >>file &&
|
||||||
|
git add file &&
|
||||||
|
git commit -F ../mark)
|
||||||
|
}
|
||||||
|
|
||||||
|
update_repos() {
|
||||||
|
update_repo one $1 &&
|
||||||
|
update_repo two $1
|
||||||
|
}
|
||||||
|
|
||||||
|
repo_fetched() {
|
||||||
|
if test "`git log -1 --pretty=format:%s $1 --`" = "`cat mark`"; then
|
||||||
|
echo >&2 "repo was fetched: $1"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
echo >&2 "repo was not fetched: $1"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
test_expect_success 'setup' '
|
||||||
|
mkdir one && (cd one && git init) &&
|
||||||
|
mkdir two && (cd two && git init) &&
|
||||||
|
git remote add -m master one one &&
|
||||||
|
git remote add -m master two two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'no group updates all' '
|
||||||
|
mark update-all &&
|
||||||
|
update_repos &&
|
||||||
|
git remote update &&
|
||||||
|
repo_fetched one &&
|
||||||
|
repo_fetched two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'nonexistant group produces error' '
|
||||||
|
mark nonexistant &&
|
||||||
|
update_repos &&
|
||||||
|
test_must_fail git remote update nonexistant &&
|
||||||
|
! repo_fetched one &&
|
||||||
|
! repo_fetched two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'updating group updates all members' '
|
||||||
|
mark group-all &&
|
||||||
|
update_repos &&
|
||||||
|
git config --add remotes.all one &&
|
||||||
|
git config --add remotes.all two &&
|
||||||
|
git remote update all &&
|
||||||
|
repo_fetched one &&
|
||||||
|
repo_fetched two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'updating group does not update non-members' '
|
||||||
|
mark group-some &&
|
||||||
|
update_repos &&
|
||||||
|
git config --add remotes.some one &&
|
||||||
|
git remote update some &&
|
||||||
|
repo_fetched one &&
|
||||||
|
! repo_fetched two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'updating remote name updates that remote' '
|
||||||
|
mark remote-name &&
|
||||||
|
update_repos &&
|
||||||
|
git remote update one &&
|
||||||
|
repo_fetched one &&
|
||||||
|
! repo_fetched two
|
||||||
|
'
|
||||||
|
|
||||||
|
test_done
|
Reference in New Issue
Block a user