Moving (renaming) submodules, recipe/script
Today I had to move my first submodule, and I discovered that Git's support for this is pretty limited. There have been a few patch series attempting to address this [1,2], but none of them seems to have pushed through into master (although I can't put my finger on a reason for why). There are also some SO postings discussing this [3,4]. It would be nice if git mv worked out of the box on submodules. Failing that, there could be a git submodule mv command that casts the appropriate spell. Failing that, there could be a recipe in Documentation/git-submodule.txt. Here's the best I could come up with for a git-submodule-mv.sh:
#!/bin/sh
# usage: git-submodule-mv.sh OLD NEW
OLD=$(realpath --relative-to . "$1")
NEW=$(realpath --relative-to . "$2")
SHA=$(git ls-files -s "$OLD" | sed 's|^[0-9]* \([0-9a-f]*\) .*|\1|')
NAME=$(git config -f .gitmodules --get-regexp 'submodule\..*\.path' "$OLD" |
sed -e 's|^submodule.||' -e "s|.path $OLD\$||")
GITDIR=$(realpath --relative-to "$NEW" .git/modules/"$NAME")
git config -f .gitmodules submodule."$NAME".path "$NEW"
git config -f .git/modules/"$NAME"/config core.worktree "../../../$NEW"
git rm --cached "$OLD"
mv "$OLD" "$NEW"
echo "gitdir: $GITDIR" > "$NEW/.git"
git update-index --add --cacheinfo 160000 "$SHA" "$NEW"This only works from the repository root directory, and I'm sure makes a number of poor assumptions (e.g. old-style submodules that don't use gitdir links are not supported). It does work for some simple test cases. The tricky parts (e.g. path -> name conversion) are already worked out more robustly git-submodule.sh, so adding a new cmd_mv shouldn't be very difficult.
Could something like this live somewhere in Git, or are we waiting for a more integrated solution?
Cheers,
Trevor
[1]: http://thread.gmane.org/gmane.comp.version-control.git/88720
[2]: http://thread.gmane.org/gmane.comp.version-control.git/143250
[4]: http://stackoverflow.com/questions/4323558/moving-submodules-with-git
[3]: http://stackoverflow.com/questions/4604486/how-do-i-move-an-existing-git-submodule-within-a-git-repository
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
(just cc-ing Jens and Peter, who might be interested)
Quoted 43 lines · W. Trevor King
Today I had to move my first submodule, and I discovered that Git's support for this is pretty limited. There have been a few patch series attempting to address this [1,2], but none of them seems to have pushed through into master (although I can't put my finger on a reason for why). There are also some SO postings discussing this [3,4]. It would be nice if
git mvworked out of the box on submodules. Failing that, there could be agit submodule mvcommand that casts the appropriate spell. Failing that, there could be a recipe inDocumentation/git-submodule.txt. Here's the best I could come up with for agit-submodule-mv.sh:#!/bin/sh # usage: git-submodule-mv.sh OLD NEW OLD=$(realpath --relative-to . "$1") NEW=$(realpath --relative-to . "$2") SHA=$(git ls-files -s "$OLD" | sed 's|^[0-9]* \([0-9a-f]*\) .*|\1|') NAME=$(git config -f .gitmodules --get-regexp 'submodule\..*\.path' "$OLD" | sed -e 's|^submodule.||' -e "s|.path $OLD\$||") GITDIR=$(realpath --relative-to "$NEW" .git/modules/"$NAME") git config -f .gitmodules submodule."$NAME".path "$NEW" git config -f .git/modules/"$NAME"/config core.worktree "../../../$NEW" git rm --cached "$OLD" mv "$OLD" "$NEW" echo "gitdir: $GITDIR" > "$NEW/.git" git update-index --add --cacheinfo 160000 "$SHA" "$NEW"This only works from the repository root directory, and I'm sure makes a number of poor assumptions (e.g. old-style submodules that don't use
gitdirlinks are not supported). It does work for some simple test cases. The tricky parts (e.g. path -> name conversion) are already worked out more robustlygit-submodule.sh, so adding a newcmd_mvshouldn't be very difficult.Could something like this live somewhere in Git, or are we waiting for a more integrated solution?
Cheers,
Trevor[1]: http://thread.gmane.org/gmane.comp.version-control.git/88720
[2]: http://thread.gmane.org/gmane.comp.version-control.git/143250
[4]: http://stackoverflow.com/questions/4323558/moving-submodules-with-git
[3]: http://stackoverflow.com/questions/4604486/how-do-i-move-an-existing-git-submodule-within-a-git-repository
Am 07.01.2013 02:39, schrieb Jonathan Nieder:
Quoted 1 line
(just cc-ing Jens and Peter, who might be interested)
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit. Please see
https://github.com/jlehmann/git-submod-enhancements/commits/mv-submodules
for the current state of this series.
Quoted 50 lines
Quoted 36 lines · W. Trevor King
Today I had to move my first submodule, and I discovered that Git's support for this is pretty limited. There have been a few patch series attempting to address this [1,2], but none of them seems to have pushed through into master (although I can't put my finger on a reason for why). There are also some SO postings discussing this [3,4]. It would be nice if
git mvworked out of the box on submodules. Failing that, there could be agit submodule mvcommand that casts the appropriate spell. Failing that, there could be a recipe inDocumentation/git-submodule.txt. Here's the best I could come up with for agit-submodule-mv.sh:#!/bin/sh # usage: git-submodule-mv.sh OLD NEW OLD=$(realpath --relative-to . "$1") NEW=$(realpath --relative-to . "$2") SHA=$(git ls-files -s "$OLD" | sed 's|^[0-9]* \([0-9a-f]*\) .*|\1|') NAME=$(git config -f .gitmodules --get-regexp 'submodule\..*\.path' "$OLD" | sed -e 's|^submodule.||' -e "s|.path $OLD\$||") GITDIR=$(realpath --relative-to "$NEW" .git/modules/"$NAME") git config -f .gitmodules submodule."$NAME".path "$NEW" git config -f .git/modules/"$NAME"/config core.worktree "../../../$NEW" git rm --cached "$OLD" mv "$OLD" "$NEW" echo "gitdir: $GITDIR" > "$NEW/.git" git update-index --add --cacheinfo 160000 "$SHA" "$NEW"This only works from the repository root directory, and I'm sure makes a number of poor assumptions (e.g. old-style submodules that don't use
gitdirlinks are not supported). It does work for some simple test cases. The tricky parts (e.g. path -> name conversion) are already worked out more robustlygit-submodule.sh, so adding a newcmd_mvshouldn't be very difficult.Could something like this live somewhere in Git, or are we waiting for a more integrated solution?
Quoted 7 lines
Cheers,
Trevor[1]: http://thread.gmane.org/gmane.comp.version-control.git/88720
[2]: http://thread.gmane.org/gmane.comp.version-control.git/143250
[4]: http://stackoverflow.com/questions/4323558/moving-submodules-with-git
[3]: http://stackoverflow.com/questions/4604486/how-do-i-move-an-existing-git-submodule-within-a-git-repository--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 5 lines · Jens Lehmann
Am 07.01.2013 02:39, schrieb Jonathan Nieder:
Quoted 1 line
(just cc-ing Jens and Peter, who might be interested)
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit.
Thanks for a heads-up.
As a couple of recent "What's cooking" message has stated, I'll shortly kick jl/submodule-deinit topic out of 'next' back to 'pu', so please make an update a replacement, not an incremental. If I recall the discussion correctly, I think we agreed that deinit should clear the slate, which means the submodule working tree should be removed and made into an empty directory without ".git" in it, and the last round we saw on the list didn't do that.
Thanks.
Am 07.01.2013 08:44, schrieb Junio C Hamano:
Quoted 10 lines
Quoted 5 lines · Jens Lehmann
Am 07.01.2013 02:39, schrieb Jonathan Nieder:
Quoted 1 line
(just cc-ing Jens and Peter, who might be interested)
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit.
Thanks for a heads-up.
Quoted 7 lines
As a couple of recent "What's cooking" message has stated, I'll shortly kick jl/submodule-deinit topic out of 'next' back to 'pu', so please make an update a replacement, not an incremental. If I recall the discussion correctly, I think we agreed that deinit should clear the slate, which means the submodule working tree should be removed and made into an empty directory without ".git" in it, and the last round we saw on the list didn't do that.
Right, and me thinks that would warrant a --force option for deinit to do that even if the submodule contains local changes (which would make deinit fail otherwise). Additionally Michael and Marc spoke up that they would rather have a --all option to deinit all initialized submodules and "git submodule deinit" without any arguments should just produce a usage message. As I saw no voices against it that'll be part of the next iteration too.
Quoted 3 lines · Jens Lehmann
Right, and me thinks that would warrant a --force option for deinit to do that even if the submodule contains local changes (which would make deinit fail otherwise).
Probably.
Quoted 5 lines
Additionally Michael and Marc spoke up
that they would rather have a --all option to deinit all initialized submodules and "git submodule deinit" without any arguments should just produce a usage message. As I saw no voices against it that'll be part of the next iteration too.
Yeah, I forgot about that possible surprise of deiniting everything under the sun by default.
I am not sure if "--all" is a good way forward, though.
Can you defeat it with "git submodule deinit ./--all" or something to limit the target only to one submodule whose unfortunate location is named as such? If you have such a support, I have this suspicion that you already get a short and explicit way to say "everything under the current directory" with "git submodule deinit ." for free.
Quoted 5 lines · Jens Lehmann
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit. Please see
https://github.com/jlehmann/git-submod-enhancements/commits/mv-submodules
for the current state of this series.
Ah, that will be much better :).
It looks like my script tries to do all the right things though, so if anyone needs to move a submodule before Jens' branch lands, you might want to give it a try.
Cheers,
Trevor
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
Quoted 8 lines · Jens Lehmann
Am 07.01.2013 02:39, schrieb Jonathan Nieder:
Quoted 1 line
(just cc-ing Jens and Peter, who might be interested)
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit. Please see
https://github.com/jlehmann/git-submod-enhancements/commits/mv-submodules
for the current state of this series.
Thinking about this a bit more, I'm not clear on how out-of-tree updates (i.e. worktree in .git/modules/*/config) propogated during branch checkouts (merges, rebases, etc.). I just got a broken rebase trying to move a trivial patch back before the submodule move, and Git was confused about what had happened to the submodules. Here's a simple script that illustrates the problem:
#!/bin/sh
rm -rf a b c
mkdir a
(cd a
git init
echo a > README
git add README
git commit -am 'a'
)
git clone a b
(cd b
git submodule add ../a submod-1
git commit -am 'add submodule at submod-1'
)
git clone b c
(cd c
git submodule update --init
)
(cd b
git-submodule-mv.sh submod-1 submod-2
git commit -am 'move submodule from submod-1 to submod-2'
)
(cd c
git pull
ls -d .git/modules/*
cat .git/modules/submod-1/config
ls -a submod*
)
The end result is that c gets the .gitmodules path updates and new gitlinked directory from the submodule move in b (using my git-submodule-mv.sh posted earlier in this thread), but submod-1 is left lying around (because Git doesn't know that it can remove submod-1/.git and submod-1/README). The Git directory for the submodule stays in .git/modules/submod-1/ (good), but the worktree in .git/modules/submod-1/config still points to ../../../submod-1 (bad).
This means that submodule moves are possible, but anyone trying to share them between several repositories (or trying to rebase across the move within their own repository) is in for a world of suffering ;). I'm not sure how this should be addressed, but I didn't see anything handling it in Jens' new series.
Thanks,
Trevor
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
Quoted 3 lines · W. Trevor King
Thinking about this a bit more, I'm not clear on how out-of-tree updates (i.e. worktree in .git/modules/*/config) propogated during branch checkouts (merges, rebases, etc.).
Actually, I don't understand why storing worktree in .git/modules/*/config is useful at all…. This may be related to my lack of clarity on the "why can't we have multiple working directories checked out at the same time" issue:
Quoted 7 lines · W. Trevor King
Quoted 6 lines · Jens Lehmann
Am 22.10.2012 14:37, schrieb W. Trevor King:
Quoted 5 lines
but cloning a remote repository (vs. checking out a local branch) seems to be baked into the submodule implementation. Should I be thinking about generalizing
git-submodule.sh, or am I looking under the wrong rock? My ideal syntax would be something like
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
$ git submodule add -b c --local dir-for-c/But then we'd have to be able to have two (or more) work trees using the same git directory, which current submodule code can't.
And that's the problem I'm trying to solve ;).
Can someone with a better feeling for why this won't work. Is is just that there's only one .git/HEAD?
Cheers,
Trevor
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
Am 08.01.2013 15:32, schrieb W. Trevor King:
Quoted 46 lines · Jens Lehmann
Quoted 8 lines
Am 07.01.2013 02:39, schrieb Jonathan Nieder:
Quoted 1 line
(just cc-ing Jens and Peter, who might be interested)
I´m currently working on teaching mv to move submodules and intend to send those patches to the list after finishing submodule deinit. Please see
https://github.com/jlehmann/git-submod-enhancements/commits/mv-submodules
for the current state of this series.Thinking about this a bit more, I'm not clear on how out-of-tree updates (i.e. worktree in .git/modules/*/config) propogated during branch checkouts (merges, rebases, etc.). I just got a broken rebase trying to move a trivial patch back before the submodule move, and Git was confused about what had happened to the submodules. Here's a simple script that illustrates the problem:
#!/bin/sh
rm -rf a b c
mkdir a
(cd a
git init
echo a > README
git add README
git commit -am 'a'
)
git clone a b
(cd b
git submodule add ../a submod-1
git commit -am 'add submodule at submod-1'
)
git clone b c
(cd c
git submodule update --init
)
(cd b
git-submodule-mv.shsubmod-1 submod-2
git commit -am 'move submodule from submod-1 to submod-2'
)
(cd c
git pull
ls -d .git/modules/*
cat .git/modules/submod-1/config
ls -a submod*
)
Quoted 5 lines
The end result is that
cgets the.gitmodulespath updates and new gitlinked directory from the submodule move inb(using mygit-submodule-mv.shposted earlier in this thread), butsubmod-1is left lying around (because Git doesn't know that it can remove submod-1/.git and submod-1/README).
That's just what current git does with removed submodules (but my recursive submodule update series will handle that just fine).
Quoted 3 lines
The Git directory for the
submodule stays in .git/modules/submod-1/ (good), but the worktree in .git/modules/submod-1/config still points to ../../../submod-1 (bad).
You'll not only have to update the gitfile but also the core.worktree setting in the repo. Sorry I missed that when you posted your script.
Quoted 5 lines
This means that submodule moves are possible, but anyone trying to share them between several repositories (or trying to rebase across the move within their own repository) is in for a world of suffering ;). I'm not sure how this should be addressed, but I didn't see anything handling it in Jens' new series.
If you adjust core.worktree properly you'll just have the old submodule work tree lying around (just like you do after you rm'd it) and everything apart from that should just work.
As I mentioned that will be fixed by recursive submodule checkout. I'll see if I can polish my preliminary branch so that interested people can play around with it if anyone is interested.
Quoted 7 lines · Jens Lehmann
Am 08.01.2013 15:32, schrieb W. Trevor King:
Quoted 3 lines
The Git directory for the
submodule stays in .git/modules/submod-1/ (good), but the worktree in .git/modules/submod-1/config still points to ../../../submod-1 (bad).You'll not only have to update the gitfile but also the core.worktree setting in the repo. Sorry I missed that when you posted your script.
My git-submodule-mv.sh script does update core.worktree. The problem is that git checkout, git merge, etc. do not.
Quoted 6 lines
Quoted 5 lines
This means that submodule moves are possible, but anyone trying to share them between several repositories (or trying to rebase across the move within their own repository) is in for a world of suffering ;). I'm not sure how this should be addressed, but I didn't see anything handling it in Jens' new series.
Quoted 7 lines
If you adjust core.worktree properly you'll just have the old submodule work tree lying around (just like you do after you rm'd it) and everything apart from that should just work.
As I mentioned that will be fixed by recursive submodule checkout. I'll see if I can polish my preliminary branch so that interested people can play around with it if anyone is interested.
Sounds like a fix will be in here. I'll definitely help put the branch through its paces ;).
Trevor
Signature · 2 lines
This email may be signed or encrypted with GnuPG (http://www.gnupg.org). For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy