Description
I've encountered the issue where if you squash or rebase the container repository containing the subrepo folder, the .gitrepo
> subrepo.parent
value gets stale because the commit shas have changed. Any, subsequent git subrepo push
or git subrepo pull
will give the error:
git-subrepo: Command failed: 'git branch subrepo/external/ag-website-shared '.
fatal: not a valid object name: ''
There are numerous github issues that highlight this issue, and the general solution seems to be to modify the subrepo.parent
value manually. However, I have a work around that appears to work, but would love comments on whether it's a good approach, and if so, maybe we can get it merged into the repo properly.
My approach is to create a script that wraps git subrepo
and handles the case where the parent sha is stale, and fixes it, then runs the git subrepo
command again. If it isn't stale, it just runs the command as normal. The steps are:
-
Check if the
subrepo.parent
value is stale withgit merge-base --is-ancestor ${gitRepoParentValue} HEAD
It returns an error code if the parent sha can't be found, otherwise it returns nothing.
I got the idea of how to check this from Clear error when rebase has broken parent reference #415
-
If it is stale, do the extra step of:
# Get the commit sha of the _parent_ of the `.gitrepo` file git log --format=%P --follow -1 ${subRepoFolder}/.gitrepo # Set the `.gitrepo` parent value to the parent commit git config --file ${subRepoFolder}/.gitrepo subrepo.parent ${gitRepoParentCommit} # Commit the changes git commit -am "Update .gitrepo parent sha"
-
Run the
git subrepo
command as normal -
As a cleanup task, if the parent was stale, I squash the commit of the
git subrepo
with the extra commit for updating.gitrepo
(step 2).
Essentially, step 2 is a temporary step, just to get git subrepo
commands to work, and once it works, that commit is no longer necessary. I'm not 100% sure this will work for all cases, but it works for the typical github PR flow that rebases or squashes commits. With this script, it doesn't matter if the parent sha is stale from a rebase, as it will be "cleaned" up when the next person runs the script.
Here is the implementation: https://github.com/ag-grid/ag-website-shared/blob/78c8d85224fcc688cd446273ea50a6ffbfbde8d9/scripts/subrepo/src/lib/runSubRepoCommand.ts (the folder containing it, wraps it in a CLI, so that it can be run in the container repositories).
Can anyone see any downsides to this approach?
Other issues that highlight this problem:
- Clear error when rebase has broken parent reference #415
- git-subrepo: Can't commit: 'subrepo/myrepo' doesn't contain upstream HEAD: #602
- Tracking breaks after rebasing a clone/pull commit parent #600
- Not a valid object name when push dot named subrepo #583
- git subrepo pull after squashing #578
git subrepo pull/branch
fails after force-pushing to remote #541- Proposed workflow for pulling changes into feature branches in combination with squash-merges #464
- Possible to pull without squashing? #344