git-remote rename: migrate from remotes/ and branches/
Remote definition that came from $GIT_DIR/remotes/nick and $GIT_DIR/branches/nick are migrated to [remotes "nick"] section in the configuration file. Signed-off-by: Miklos Vajna <vmiklos@frugalware.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Miklos Vajna committed
Nov 10, 2008 at 21:43 UTC
1dd1239aa33ddd7f159cd183338ef6f71298e29a
2 files changed
+68
builtin-remote.c
+35
@@ -359,6 +359,38 @@ static int read_remote_branches(const char *refname,
359
return 0;
360
}
361
362
+static int migrate_file(struct remote *remote)
363
+{
364
+ struct strbuf buf = STRBUF_INIT;
365
+ int i;
366
+ char *path = NULL;
367
+
368
+ strbuf_addf(&buf, "remote.%s.url", remote->name);
369
+ for (i = 0; i < remote->url_nr; i++)
370
+ if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
371
+ return error("Could not append '%s' to '%s'",
372
+ remote->url[i], buf.buf);
373
+ strbuf_reset(&buf);
374
+ strbuf_addf(&buf, "remote.%s.push", remote->name);
375
+ for (i = 0; i < remote->push_refspec_nr; i++)
376
+ if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
377
+ return error("Could not append '%s' to '%s'",
378
+ remote->push_refspec[i], buf.buf);
379
+ strbuf_reset(&buf);
380
+ strbuf_addf(&buf, "remote.%s.fetch", remote->name);
381
+ for (i = 0; i < remote->fetch_refspec_nr; i++)
382
+ if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
383
+ return error("Could not append '%s' to '%s'",
384
+ remote->fetch_refspec[i], buf.buf);
385
+ if (remote->origin == REMOTE_REMOTES)
386
+ path = git_path("remotes/%s", remote->name);
387
+ else if (remote->origin == REMOTE_BRANCHES)
388
+ path = git_path("branches/%s", remote->name);
389
+ if (path && unlink(path))
390
+ warning("failed to remove '%s'", path);
391
+ return 0;
392
+}
393
+
394
static int mv(int argc, const char **argv)
395
{
396
struct option options[] = {
@@ -381,6 +413,9 @@ static int mv(int argc, const char **argv)
413
if (!oldremote)
414
die("No such remote: %s", rename.old);
415
416
+ if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
417
+ return migrate_file(oldremote);
418
+
419
newremote = remote_get(rename.new);
420
if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
421
die("remote %s already exists.", rename.new);
t/t5505-remote.sh
+33
@@ -343,4 +343,37 @@ test_expect_success 'rename a remote' '
343
test "$(git config branch.master.remote)" = "upstream")
344
345
'
346
+
347
+cat > remotes_origin << EOF
348
+URL: $(pwd)/one
349
+Push: refs/heads/master:refs/heads/upstream
350
+Pull: refs/heads/master:refs/heads/origin
351
+EOF
352
+
353
+test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
354
+ git clone one five &&
355
+ origin_url=$(pwd)/one &&
356
+ (cd five &&
357
+ git remote rm origin &&
358
+ mkdir -p .git/remotes &&
359
+ cat ../remotes_origin > .git/remotes/origin &&
360
+ git remote rename origin origin &&
361
+ ! test -f .git/remotes/origin &&
362
+ test "$(git config remote.origin.url)" = "$origin_url" &&
363
+ test "$(git config remote.origin.push)" = "refs/heads/master:refs/heads/upstream" &&
364
+ test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
365
+'
366
+
367
+test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
368
+ git clone one six &&
369
+ origin_url=$(pwd)/one &&
370
+ (cd six &&
371
+ git remote rm origin &&
372
+ echo "$origin_url" > .git/branches/origin &&
373
+ git remote rename origin origin &&
374
+ ! test -f .git/branches/origin &&
375
+ test "$(git config remote.origin.url)" = "$origin_url" &&
376
+ test "$(git config remote.origin.fetch)" = "refs/heads/master:refs/heads/origin")
377
+'
378
+
379
test_done