@matheusbsbs / matheus-bagliano / commits / 8db355964d
Fork 0

Re-implement 'git remote update' using 'git fetch'

In order not to duplicate functionality, re-implement 'git remote update' in terms of 'git fetch'. Signed-off-by: Björn Gustavsson <bgustavsson@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Björn Gustavsson committed Nov 10, 2009 at 09:21 UTC 8db355964d89c19eb262ffe38e57e5a610e1cc05
1 file changed +28 -60
builtin-remote.c
+28 -60
@@ -1173,88 +1173,56 @@ static int prune_remote(const char *remote, int dry_run)
1173 return result;
1174 }
1175
1176 -static int get_one_remote_for_update(struct remote *remote, void *priv)
1176 +static int get_remote_default(const char *key, const char *value, void *priv)
1177 {
1178 - struct string_list *list = priv;
1179 - if (!remote->skip_default_update)
1180 - string_list_append(remote->name, list);
1181 - return 0;
1182 -}
1183 -
1184 -static struct remote_group {
1185 - const char *name;
1186 - struct string_list *list;
1187 -} remote_group;
1188 -
1189 -static int get_remote_group(const char *key, const char *value, void *num_hits)
1190 -{
1191 - if (!prefixcmp(key, "remotes.") &&
1192 - !strcmp(key + 8, remote_group.name)) {
1193 - /* split list by white space */
1194 - int space = strcspn(value, " \t\n");
1195 - while (*value) {
1196 - if (space > 1) {
1197 - string_list_append(xstrndup(value, space),
1198 - remote_group.list);
1199 - ++*((int *)num_hits);
1200 - }
1201 - value += space + (value[space] != '\0');
1202 - space = strcspn(value, " \t\n");
1203 - }
1178 + if (strcmp(key, "remotes.default") == 0) {
1179 + int *found = priv;
1180 + *found = 1;
1181 }
1205 -
1182 return 0;
1183 }
1184
1185 static int update(int argc, const char **argv)
1186 {
1211 - int i, result = 0, prune = 0;
1212 - struct string_list list = { NULL, 0, 0, 0 };
1213 - static const char *default_argv[] = { NULL, "default", NULL };
1187 + int i, prune = 0;
1188 struct option options[] = {
1189 OPT_GROUP("update specific options"),
1190 OPT_BOOLEAN('p', "prune", &prune,
1191 "prune remotes after fetching"),
1192 OPT_END()
1193 };
1194 + const char **fetch_argv;
1195 + int fetch_argc = 0;
1196 + int default_defined = 0;
1197 +
1198 + fetch_argv = xmalloc(sizeof(char *) * (argc+5));
1199
1200 argc = parse_options(argc, argv, NULL, options, builtin_remote_usage,
1201 PARSE_OPT_KEEP_ARGV0);
1223 - if (argc < 2) {
1224 - argc = 2;
1225 - argv = default_argv;
1226 - }
1202
1228 - remote_group.list = &list;
1229 - for (i = 1; i < argc; i++) {
1230 - int groups_found = 0;
1231 - remote_group.name = argv[i];
1232 - result = git_config(get_remote_group, &groups_found);
1233 - if (!groups_found && (i != 1 || strcmp(argv[1], "default"))) {
1234 - struct remote *remote;
1235 - if (!remote_is_configured(argv[i]))
1236 - die("No such remote or remote group: %s",
1237 - argv[i]);
1238 - remote = remote_get(argv[i]);
1239 - string_list_append(remote->name, remote_group.list);
1240 - }
1241 - }
1203 + fetch_argv[fetch_argc++] = "fetch";
1204
1243 - if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
1244 - result = for_each_remote(get_one_remote_for_update, &list);
1205 + if (prune)
1206 + fetch_argv[fetch_argc++] = "--prune";
1207 + if (verbose)
1208 + fetch_argv[fetch_argc++] = "-v";
1209 + if (argc < 2) {
1210 + fetch_argv[fetch_argc++] = "default";
1211 + } else {
1212 + fetch_argv[fetch_argc++] = "--multiple";
1213 + for (i = 1; i < argc; i++)
1214 + fetch_argv[fetch_argc++] = argv[i];
1215 + }
1216
1246 - for (i = 0; i < list.nr; i++) {
1247 - int err = fetch_remote(list.items[i].string);
1248 - result |= err;
1249 - if (!err && prune)
1250 - result |= prune_remote(list.items[i].string, 0);
1217 + if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
1218 + git_config(get_remote_default, &default_defined);
1219 + if (!default_defined)
1220 + fetch_argv[fetch_argc-1] = "--all";
1221 }
1222
1253 - /* all names were strdup()ed or strndup()ed */
1254 - list.strdup_strings = 1;
1255 - string_list_clear(&list, 0);
1223 + fetch_argv[fetch_argc] = NULL;
1224
1257 - return result;
1225 + return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
1226 }
1227
1228 static int get_one_entry(struct remote *remote, void *priv)