Skip to content

Fixing the rebase/squash issue #617

Closed
@taktran

Description

@taktran

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:

  1. Check if the subrepo.parent value is stale with

    git 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

  2. 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"
    
  3. Run the git subrepo command as normal

  4. 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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions