git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Plumbing to rename a ref?
@ 2014-05-23 10:11 Sergei Organov
  2014-05-23 10:50 ` John Keeping
  0 siblings, 1 reply; 8+ messages in thread
From: Sergei Organov @ 2014-05-23 10:11 UTC (permalink / raw)
  To: git

Hello,

After convertion of a project from CVS to git, I'd like to rename some
references in the created git repository (before it's published, so no
problems here). Is there a plumbing that would do:

git rename-ref <old_name> <new_name>

for me?

For reference, the (ugly) solution I currrently use is:

# Renamve branches/tags for brevity.
#
# e.g.: version-3-5-branch -> v3.5-branch
#

sed_cmd='sed "s/version-/v/g" | sed "s/\([0-9]\)-\([0-9]\)/\1.\2/g" | sed "s/\([0-9]\)-\([0-9]\)/\1.\2/g"'

if [ -f "packed-refs" ]; then
    rm -rf "packed-refs.new"
    cat "packed-refs" | eval "$sed_cmd" > "packed-refs.new" && mv "packed-refs.new" "packed-refs"
fi

git for-each-ref --format="%(refname)" |
while read -r; do
    ref="$REPLY"
    if [ -f "$ref" ]; then
        new_ref=`echo "$ref" | eval "$sed_cmd"`
        if [ "$ref" != "$new_ref" ]; then
            echo "$ref -> $new_ref"
            mv "$ref" "$new_ref"
        fi
    fi
done

-- Sergey.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 10:11 Plumbing to rename a ref? Sergei Organov
@ 2014-05-23 10:50 ` John Keeping
  2014-05-23 11:35   ` Sergei Organov
  2014-05-23 17:10   ` Junio C Hamano
  0 siblings, 2 replies; 8+ messages in thread
From: John Keeping @ 2014-05-23 10:50 UTC (permalink / raw)
  To: Sergei Organov; +Cc: git

On Fri, May 23, 2014 at 02:11:55PM +0400, Sergei Organov wrote:
> Hello,
> 
> After convertion of a project from CVS to git, I'd like to rename some
> references in the created git repository (before it's published, so no
> problems here). Is there a plumbing that would do:
> 
> git rename-ref <old_name> <new_name>
> 
> for me?

I think the best you can get is two invocations of `git update-ref`:

	git update-ref <new_name> <old_name> &&
	git update-ref -d <old_name>

Although if you're scripting it the `--stdin` mode may be easier:

	git update-ref --stdin <<-\EOF
	create <new_name> <old_name>
	delete <old_name>
	EOF

Note that "<new_name>" must be a fully-qualified ref (that is, it must
start with "refs/", so "refs/heads/new_name" for a branch or
"refs/tags/new_name" for a tag).

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 10:50 ` John Keeping
@ 2014-05-23 11:35   ` Sergei Organov
  2014-05-23 12:35     ` Jeff King
  2014-05-23 17:10   ` Junio C Hamano
  1 sibling, 1 reply; 8+ messages in thread
From: Sergei Organov @ 2014-05-23 11:35 UTC (permalink / raw)
  To: John Keeping; +Cc: git

John Keeping <john@keeping.me.uk> writes:
> On Fri, May 23, 2014 at 02:11:55PM +0400, Sergei Organov wrote:
>> Hello,
>> 
>> After convertion of a project from CVS to git, I'd like to rename some
>> references in the created git repository (before it's published, so no
>> problems here). Is there a plumbing that would do:
>> 
>> git rename-ref <old_name> <new_name>
>> 
>> for me?
>
> I think the best you can get is two invocations of `git update-ref`:
>
> 	git update-ref <new_name> <old_name> &&
> 	git update-ref -d <old_name>

This should be good enough. Thanks a lot!

-- Sergey.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 11:35   ` Sergei Organov
@ 2014-05-23 12:35     ` Jeff King
  2014-05-23 17:11       ` Junio C Hamano
  0 siblings, 1 reply; 8+ messages in thread
From: Jeff King @ 2014-05-23 12:35 UTC (permalink / raw)
  To: Sergei Organov; +Cc: John Keeping, git

On Fri, May 23, 2014 at 03:35:54PM +0400, Sergei Organov wrote:

> John Keeping <john@keeping.me.uk> writes:
> > On Fri, May 23, 2014 at 02:11:55PM +0400, Sergei Organov wrote:
> >> Hello,
> >> 
> >> After convertion of a project from CVS to git, I'd like to rename some
> >> references in the created git repository (before it's published, so no
> >> problems here). Is there a plumbing that would do:
> >> 
> >> git rename-ref <old_name> <new_name>
> >> 
> >> for me?
> >
> > I think the best you can get is two invocations of `git update-ref`:
> >
> > 	git update-ref <new_name> <old_name> &&
> > 	git update-ref -d <old_name>
> 
> This should be good enough. Thanks a lot!

One thing that this misses (as does your original script) is the
reflogs. Doing "branch -m" to rename a branch will actually move the
reflogs, too, but there is otherwise no way to access that
functionality.

It does not seem unreasonable to teach "git update-ref" to do renames to
take advantage of this (it would be fairly simple; the logic is already
encapsulated internally in a rename_ref function).

-Peff

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 10:50 ` John Keeping
  2014-05-23 11:35   ` Sergei Organov
@ 2014-05-23 17:10   ` Junio C Hamano
  2014-05-23 17:14     ` John Keeping
  1 sibling, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2014-05-23 17:10 UTC (permalink / raw)
  To: John Keeping; +Cc: Sergei Organov, git

John Keeping <john@keeping.me.uk> writes:

> On Fri, May 23, 2014 at 02:11:55PM +0400, Sergei Organov wrote:
>> Hello,
>> 
>> After convertion of a project from CVS to git, I'd like to rename some
>> references in the created git repository (before it's published, so no
>> problems here). Is there a plumbing that would do:
>> 
>> git rename-ref <old_name> <new_name>
>> 
>> for me?
>
> I think the best you can get is two invocations of `git update-ref`:
>
> 	git update-ref <new_name> <old_name> &&
> 	git update-ref -d <old_name>
>
> Although if you're scripting it the `--stdin` mode may be easier:
>
> 	git update-ref --stdin <<-\EOF
> 	create <new_name> <old_name>
> 	delete <old_name>
> 	EOF
>
> Note that "<new_name>" must be a fully-qualified ref (that is, it must
> start with "refs/", so "refs/heads/new_name" for a branch or
> "refs/tags/new_name" for a tag).

Shouldn't <old_name> also be a full ref?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 12:35     ` Jeff King
@ 2014-05-23 17:11       ` Junio C Hamano
  2014-05-23 19:41         ` Jeff King
  0 siblings, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2014-05-23 17:11 UTC (permalink / raw)
  To: Jeff King; +Cc: Sergei Organov, John Keeping, git

Jeff King <peff@peff.net> writes:

> One thing that this misses (as does your original script) is the
> reflogs. Doing "branch -m" to rename a branch will actually move the
> reflogs, too, but there is otherwise no way to access that
> functionality.
>
> It does not seem unreasonable to teach "git update-ref" to do renames to
> take advantage of this (it would be fairly simple; the logic is already
> encapsulated internally in a rename_ref function).

Sounds sensible.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 17:10   ` Junio C Hamano
@ 2014-05-23 17:14     ` John Keeping
  0 siblings, 0 replies; 8+ messages in thread
From: John Keeping @ 2014-05-23 17:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Sergei Organov, git

On Fri, May 23, 2014 at 10:10:05AM -0700, Junio C Hamano wrote:
> John Keeping <john@keeping.me.uk> writes:
> 
> > On Fri, May 23, 2014 at 02:11:55PM +0400, Sergei Organov wrote:
> >> Hello,
> >> 
> >> After convertion of a project from CVS to git, I'd like to rename some
> >> references in the created git repository (before it's published, so no
> >> problems here). Is there a plumbing that would do:
> >> 
> >> git rename-ref <old_name> <new_name>
> >> 
> >> for me?
> >
> > I think the best you can get is two invocations of `git update-ref`:
> >
> > 	git update-ref <new_name> <old_name> &&
> > 	git update-ref -d <old_name>
> >
> > Although if you're scripting it the `--stdin` mode may be easier:
> >
> > 	git update-ref --stdin <<-\EOF
> > 	create <new_name> <old_name>
> > 	delete <old_name>
> > 	EOF
> >
> > Note that "<new_name>" must be a fully-qualified ref (that is, it must
> > start with "refs/", so "refs/heads/new_name" for a branch or
> > "refs/tags/new_name" for a tag).
> 
> Shouldn't <old_name> also be a full ref?

I tested this before sending the email, and it seemed to do the right
thing specifying only the branch name; so it probably /should/ be a full
ref, but it seems like it doesn't /need/ to be.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Plumbing to rename a ref?
  2014-05-23 17:11       ` Junio C Hamano
@ 2014-05-23 19:41         ` Jeff King
  0 siblings, 0 replies; 8+ messages in thread
From: Jeff King @ 2014-05-23 19:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Sergei Organov, John Keeping, git

On Fri, May 23, 2014 at 10:11:03AM -0700, Junio C Hamano wrote:

> Jeff King <peff@peff.net> writes:
> 
> > One thing that this misses (as does your original script) is the
> > reflogs. Doing "branch -m" to rename a branch will actually move the
> > reflogs, too, but there is otherwise no way to access that
> > functionality.
> >
> > It does not seem unreasonable to teach "git update-ref" to do renames to
> > take advantage of this (it would be fairly simple; the logic is already
> > encapsulated internally in a rename_ref function).
> 
> Sounds sensible.

It's not much code to do the basics, and a patch is below. There are a
few open ends, though:

  1. There's no documentation (looking for where to put it, I feel like
     the manpage could stand to be reorganized a bit).

  2. It doesn't do anything with the --no-deref flag. I'm not sure what
     specifying a symref for either argument would/should do.

     As a side note, I also notice that you can specify --no-no-deref.
     Probably that option should be using PARSE_OPT_NONEG and offering
     "--deref" and "--no-deref" as opposites.

  3. Should it overwrite, or fail if the destination exists? "git
     branch" has a "-f" option, but we don't here.

  4. It doesn't implement rename for --stdin mode. Not strictly
     necessary, but it would be nice to keep that in sync with the
     regular command-line options.

This isn't really my itch to scratch, so I'm not all that inclined to
work on it more[1]. But without addressing 1 and 2, I'm not sure it's
really fit for inclusion. Maybe somebody wants to try to pick it up and
build on top?

-Peff

[1] Mostly when I say things like "it would be fairly simple", I wonder
    if I am being accurate, so I sometimes try to back it up with code.
    In this case the basics _are_ simple, but there are definitely some
    details to be worked out. So I was half-right. ;)

-- >8 --
Subject: teach update-ref a "--rename" option

You can rename a branch with "branch -m", but there is no
plumbing mechanism to rename an arbitrary ref. You can
simulate it with:

  git update-ref newname oldname &&
  git update-ref -d oldname

but that drops the reflog from oldname. This patch instead
allows:

  git update-ref --rename oldname newname

which copies the reflog.

Unlike "git branch -m", it does not munge the branch config
(since this plumbing command is purely about refs), nor does
it allow a missing "oldname" to mean an implicit HEAD
(because it is plumbing, and we are better to make the
caller be explicit than risk a surprise).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/update-ref.c  | 23 ++++++++++++++++++++++-
 t/t1400-update-ref.sh | 23 ++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index 5c208bb..ac97894 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -8,6 +8,7 @@
 static const char * const git_update_ref_usage[] = {
 	N_("git update-ref [options] -d <refname> [<oldval>]"),
 	N_("git update-ref [options]    <refname> <newval> [<oldval>]"),
+	N_("git update-ref [options] --rename <refname> <refname>"),
 	N_("git update-ref [options] --stdin [-z]"),
 	NULL
 };
@@ -19,6 +20,18 @@ static const struct ref_update **updates;
 static char line_termination = '\n';
 static int update_flags;
 
+static int do_rename_ref(const char *from, const char *to)
+{
+	struct strbuf msg = STRBUF_INIT;
+	int ret;
+
+	strbuf_addf(&msg, "update-ref: renamed %s to %s", from, to);
+	ret = rename_ref(from, to, msg.buf);
+	strbuf_release(&msg);
+
+	return !!ret;
+}
+
 static struct ref_update *update_alloc(void)
 {
 	struct ref_update *update;
@@ -251,9 +264,11 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	const char *refname, *oldval, *msg = NULL;
 	unsigned char sha1[20], oldsha1[20];
 	int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
+	int rename = 0;
 	struct option options[] = {
 		OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
 		OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
+		OPT_BOOL( 0 , "rename", &rename, N_("rename the reference")),
 		OPT_BOOL( 0 , "no-deref", &no_deref,
 					N_("update <refname> not the one it points to")),
 		OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
@@ -268,7 +283,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 		die("Refusing to perform update with empty message.");
 
 	if (read_stdin) {
-		if (delete || no_deref || argc > 0)
+		if (delete || rename || no_deref || argc > 0)
 			usage_with_options(git_update_ref_usage, options);
 		if (end_null)
 			line_termination = '\0';
@@ -279,6 +294,12 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	if (end_null)
 		usage_with_options(git_update_ref_usage, options);
 
+	if (rename) {
+		if (delete || argc < 2 || argc > 2)
+			usage_with_options(git_update_ref_usage, options);
+		return do_rename_ref(argv[0], argv[1]);
+	}
+
 	if (delete) {
 		if (argc < 1 || argc > 2)
 			usage_with_options(git_update_ref_usage, options);
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
index e130c52..c4008e8 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -23,6 +23,7 @@ test_expect_success setup '
 m=refs/heads/master
 n_dir=refs/heads/gu
 n=$n_dir/fixes
+d=refs/heads/dst
 
 test_expect_success \
 	"create $m" \
@@ -255,8 +256,28 @@ test_expect_success \
 	 test '"$D"' = $(cat o) &&
 	 test "warning: Log .git/logs/'"$m unexpectedly ended on $ld"'." = "$(cat e)"'
 
+test_expect_success 'rename a ref' '
+	git rev-parse --verify $m >expect &&
 
-rm -f .git/$m .git/logs/$m expect
+	# set the date to match the reflog entries we created
+	# above, which do not follow test_tick; otherwise
+	# the we write an out-of-order entry into the reflog,
+	# which confuses the reflog parser
+	GIT_COMMITTER_DATE=$ld \
+	git update-ref --rename $m $d &&
+
+	test_must_fail git rev-parse --verify $m &&
+	git rev-parse --verify $d >o &&
+	test_cmp expect o
+'
+
+test_expect_success 'renames copy reflogs' '
+	echo "$C" >expect &&
+	git rev-parse --verify "$d@{2005-05-26 23:32:00}" >o &&
+	test_cmp expect o
+'
+
+rm -f .git/$m .git/logs/$m .git/$d .git/logs/$d expect
 
 test_expect_success \
     'creating initial files' \
-- 
2.0.0.rc1.436.g03cb729

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-05-23 19:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-23 10:11 Plumbing to rename a ref? Sergei Organov
2014-05-23 10:50 ` John Keeping
2014-05-23 11:35   ` Sergei Organov
2014-05-23 12:35     ` Jeff King
2014-05-23 17:11       ` Junio C Hamano
2014-05-23 19:41         ` Jeff King
2014-05-23 17:10   ` Junio C Hamano
2014-05-23 17:14     ` John Keeping

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).