DEV Community

Discussion on: Find Changes Between Two Git Commits Without Cloning

Collapse
 
tsmmark profile image
Mark Allen

Great post. However the preferred approach suggested in the article has some shortcomings — mainly, the diff will include files that changed in master, relative to your branch. Not the end of the world, but there may be a better way.

I found a way to deepen the shallow clone depth until the merge-base commit is found. Posted in a comment here: github.com/hasura/smooth-checkout-...

#!/bin/bash
set -euo pipefail

# From: https://stackoverflow.com/a/56113247/2696867
echo "--- git fetching shallow merge base"

# TODO: Consider using PR base branch $BUILDKITE_PULL_REQUEST_BASE_BRANCH, and default to master if no PR.
echo "Fetching commits until we find the merge-base / fork-point between current commit and master"
while [ -z $( git merge-base master $BUILDKITE_COMMIT ) ]; do
  echo "git fetch --deepen=50 origin master $BUILDKITE_COMMIT"
  git fetch --deepen=50 origin master $BUILDKITE_COMMIT
done

echo "Done."
Enter fullscreen mode Exit fullscreen mode

This allows you to have a shallow clone but still have it go as deep as you need to be able to git diff between current branch and base branch:

git --no-pager diff master... --name-only
Enter fullscreen mode Exit fullscreen mode