jeremysears / scripts

Handy Scripts For Doing Things
MIT License
128 stars 36 forks source link

git-submodule-rewrite, actually always failing on git merge -s ours "not something we can merge" #25

Open devinrhode2 opened 4 years ago

devinrhode2 commented 4 years ago
➤ git-submodule-rewrite --verbose dev-scripts myco && echo "finished"                                                                             git:dev
    This script will convert your "dev-scripts" git submodule into
    a simple subdirectory in the parent repository while retaining all
    contents, file history and its own submodules.

    The script will:
      * delete the dev-scripts submodule configuration from .gitmodules and
        .git/config and commit it.
      * rewrite the entire history of the dev-scripts submodule so that all
        paths are prefixed by dev-scripts.
        This ensures that git log will correctly follow the original file
        history.
      * merge the submodule's tags into its parent repository and commit
        each tag merge individually.
        (only those tags are considered which are reachable from
         the tip of dev-scripts/myco)
      * merge the submodule into its parent repository and commit it.
      * reinstate any of the submodule's own submodules as part of the parent
        repository

    NOTE: This script might completely garble your repository, so PLEASE apply
    this only to a fresh clone of the repository where it does not matter if
    the repo is destroyed.  It would be wise to keep a backup clone of your
    repository, so that you can reconstitute it if need be.  You have been
    warned.  Use at your own risk.

Do you want to proceed? (y/n) y
+ git config -f .gitmodules --remove-section submodule.dev-scripts
+ git config -f .git/config --get submodule.dev-scripts.url
https://gitlab.biw-services.com/east-php/q360-fed/dev-scripts.git
+ git config -f .git/config --remove-section submodule.dev-scripts
+ rm -rf dev-scripts
+ git add -A .
+ git commit -m 'Remove submodule dev-scripts'
[dev 6746478b4] Remove submodule dev-scripts
 2 files changed, 12 deletions(-)
 delete mode 160000 dev-scripts
+ rm -rf .git/modules/dev-scripts
++ mktemp -d -t submodule-rewrite-XXXXXX
+ local tmpdir=/var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu
+ git clone -b myco https://gitlab.biw-services.com/east-php/q360-fed/dev-scripts.git /var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu
Cloning into '/var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu'...
remote: Enumerating objects: 348, done.
remote: Counting objects: 100% (348/348), done.
remote: Compressing objects: 100% (154/154), done.
remote: Total 348 (delta 215), reused 321 (delta 194)
Receiving objects: 100% (348/348), 111.64 KiB | 367.00 KiB/s, done.
Resolving deltas: 100% (215/215), done.
+ git fetch --tags
+ pushd /var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu
/var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu ~/repos/mhp
++ printf '\t'
+ local 'tab=   '
+ local 'filter=git ls-files -s | sed "s:   :   dev-scripts/:" | GIT_INDEX_FILE=${GIT_INDEX_FILE}.new git update-index --index-info && mv ${GIT_INDEX_FILE}.new ${GIT_INDEX_FILE} || true'
+ git filter-branch --index-filter 'git ls-files -s | sed "s:   :   dev-scripts/:" | GIT_INDEX_FILE=${GIT_INDEX_FILE}.new git update-index --index-info && mv ${GIT_INDEX_FILE}.new ${GIT_INDEX_FILE} || true' HEAD
WARNING: git-filter-branch has a glut of gotchas generating mangled history
     rewrites.  Hit Ctrl-C before proceeding to abort, then use an
     alternative filtering tool such as 'git filter-repo'
     (https://github.com/newren/git-filter-repo/) instead.  See the
     filter-branch manual page for more details; to squelch this warning,
     set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Rewrite e3e9213b7144b3f6ecbe18e0bfe69feb81a4b733 (95/105) (5 seconds passed, remaining 0 predicted)
Ref 'refs/heads/myco' was rewritten
+ popd
~/repos/mhp
+ git remote add dev-scripts /var/folders/dt/215qvyd54xb2fchcpqj8x4fc0000gn/T/submodule-rewrite-XXXXXX.0uXEZxvu
+ git_version_lte 2.8.4
+++ echo 2.8.4
+++ tr . '\n'
+++ head -n 4
++ printf %03d%03d%03d%03d 2 8 4
+ OP_VERSION=002008004000
++ git version
+ GIT_VERSION='git version 2.24.1'
+++ echo 2.24.1
+++ sed -E 's/([0-9.]*).*/\1/'
+++ tr . '\n'
+++ head -n 4
++ printf %03d%03d%03d%03d 2 24 1
+ GIT_VERSION=002024001000
+ echo -e '002024001000\n002008004000'
+ sort
+ head -n1
002008004000
+ '[' 002024001000 -le 002008004000 ']'
+ ALLOW_UNRELATED_HISTORIES=--allow-unrelated-histories
++ git tag --list --merged dev-scripts/myco
fatal: malformed object name dev-scripts/myco
+ git merge -s ours --no-commit --allow-unrelated-histories dev-scripts/myco
merge: dev-scripts/myco - not something we can merge

Also had this happen with a similar submodule that was in the root. Before I ran this I commented out this git fetch --tags "${sub} line as it caused issues during previous attempts

curioustechizen commented 4 years ago

I had the same problems with tags and I commented them. Then I run into the same problem "not something we can merge". Seems to be a git problem rather than a problem with this script.

Try git checkout dev-scripts/myco - it fails with

error: pathspec 'dev-scripts/myco' did not match any file(s) known to git

It might be a timing problem because in one instance I was able to run this git merge command myself and it did work, but I could not run the rest of the script.

If you cd to the tmpdir you will see a .git folder there so it is a valid remote. I'm not sure why git cannot recognise it as a remote though. Edited to add: git remote -v also lists the remote correctly.

I'm on Git 2.21.0

devinrhode2 commented 4 years ago

@curioustechizen you could use git subrepo in a unique way. Convert the submodules to git-subrepo, but, I've had poor performance pushing/pulling to the "child repo" repositories. Since you are trying to move away from submodules, you simply deprecate/never push/pull to the child-repo's origin.

devinrhode2 commented 4 years ago

With that approach, no developers actually need to know about or use git-subrepo. It's simply used one time to convert from submodules. In theory you could also remove the .gitrepo files, but I think it'd be better to keep them

WonderCsabo commented 1 year ago

I had the same problem with tags, and i commented out those lines. However it turned out, that line is also needed to merge the normal files. So the script is fine, but if you want to remove adding the tags, do not comment out the whole git fetch --tags lines. Just only remove the --tags flag from the lines!