Plumbing to rename a ref?
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
fidone
-- Sergey.
Quoted 9 lines · Sergei Organov
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).
Quoted 7 lines · John Keeping
Quoted 6 lines · Sergei Organov
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:
Quoted 8 lines
Quoted 3 lines
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.
Quoted 10 lines · Sergei Organov
Quoted 9 lines · John Keeping
Quoted 8 lines · Sergei Organov
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>
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
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
Quoted 8 lines · Jeff King
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_reffunction).
Sounds sensible.
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Jeff King
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.
Quoted 5 lines
Quoted 3 lines
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_reffunction).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).
--- builtin/update-ref.c | 23 ++++++++++++++++++++++- t/t1400-update-ref.sh | 23 ++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-)
builtin/update-ref.c +22 -1
@@ -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);
t/t1400-update-ref.sh +22 -1
@@ -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' \
Signature · 1 line
2.0.0.rc1.436.g03cb729
Quoted 18 lines · John Keeping
Quoted 9 lines · Sergei Organov
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
--stdinmode may be easier:
Quoted 8 lines
git update-ref --stdin <<-\EOF create <new_name> <old_name> delete <old_name> EOFNote 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?
Quoted 25 lines · Junio C Hamano
Quoted 23 lines · John Keeping
Quoted 9 lines · Sergei Organov
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
--stdinmode may be easier:git update-ref --stdin <<-\EOF create <new_name> <old_name> delete <old_name> EOF
Quoted 5 lines
Quoted 3 lines
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.