gitgit-diffgit-remotegit-fetch

Git find modified files since <ref> from a shallow clone


I'm on a CI box running tests. To speed it up, I'm just doing a shallow clone:

git clone --depth 1 git@github.com:JoshCheek/some_repo.git

Assuming all the tests pass, I want to trigger the next step in the pipeline. What to trigger is based on which files changed between the last deployment (ref d123456) and the current ref I just tested (ref c123456). If I had done a normal clone, I could find out like this this:

git diff --name-only d123456 c123456

But my clone is shallow, so it doesn't know about those commits. I see that I can use git fetch --depth=n to get more of the history, but I only know the SHA, not the depth of the SHA. Here's a set of ways that could presumably answer this question:

# hypothetical remote diff
git diff --name-only origin/d123456 origin/c123456

# hypothetical ref based fetch
git fetch --shallow-through d123456
git diff --name-only d123456 c123456

# hypothetical way to find the depth I need
depth=`git remote depth-to d123456`
git fetch --depth "$depth"
git diff --name-only d123456 c123456

Otherwise it seems like I might have to write a loop and keep invoking --deepen until my history contains the commit. That seems painful (meaning annoying to write / maintain) and expensive (meaning slow, remember that the purpose of the shallow clone is to reduce this cost).


Solution

  • Otherwise it seems like I might have to write a loop and keep invoking --deepen until my history contains the commit. That seems painful ...

    It is painful (and slow, as you note a bit later).

    Modern Git (since version 2.11) does have a new git fetch option:

    --shallow-exclude=<revision>

      Deepen or shorten the history of a shallow repository to exclude commits reachable from a specified remote branch or tag. This option can be specified multiple times.

    I have not tried this; it's not clear if it allows a hash ID (the tests use names) and in any case you would specify the parent(s) of the commit you want to deepen through, rather than the commit you want to obtain. But it might suffice.

    (I really think a better method is to keep reference clones you can borrow-from.)