#!/bin/bash
set -euo pipefail

mkdir demo
cd demo

git init main

(
    git init sub1 &&
    cd sub1 &&
    dd if=/dev/urandom of=f bs=40 count=1 &&
    git add f &&
    git commit -m f
)

(
    cd main &&
    git submodule add ../sub1 sub1 &&
    > sub2 && git add sub2 &&
    git commit -m 'add both submodules' &&
    git tag start
)

#make a commit that replaces sub1 in a l->d transition
(
    cd main &&
    git rm sub1 &&
    mkdir sub1 &&
    git submodule add ../sub1 sub1/sub1 &&
    git commit -m 'f to d' &&

    #And another commit that just adds a random file.
    >foo &&
    git add foo &&
    git commit -m 'foo' &&

    git tag l-to-d

    # Now we want to get back to the start state
    git submodule deinit sub1/sub1 &&
    git checkout -b sub2 start &&
    # Finally, we want to cherry-pick an innocuous-looking commit from a branch
    # where we have previously made the l->d change
    # This should not fail, but does.
    git cherry-pick l-to-d
)
