oliverdavies.uk/source/_posts/finding-the-last-commit-that-a-patch-applies-to.md
2020-03-26 10:27:39 +00:00

956 B

title excerpt date tags draft
Finding the last commit that a patch applies to How to find the last commit in a Git repository that a patch applies to. 2020-03-26
bash
git
true
#!/usr/bin/env bash

# https://www.drupal.org/files/issues/2018-08-28/group-configurable-entities-as-group-content-2797793-58.patch

patch_filename=group-configurable-entities-as-group-content-2797793-58.patch
first_commit=6e8c22a
last_commit=8.x-1.x

find_commits_between() {
  first_commit=$1
  last_commit=$2

  git rev-list --reverse --ancestry-path $first_commit^...$last_commit
}

reset_repo() {
  git reset --hard $1 >& /dev/null
}

apply_patch() {
  git apply --check $patch_filename >& /dev/null
}

for sha1 in $(find_commits_between $first_commit $last_commit); do
  echo "Trying ${sha1}..."

  reset_repo $sha1
  apply_patch

  if [[ $? -eq 0 ]]; then
    echo "Patch applies"
    continue
  fi

  echo "Patch does not apply"
  exit 1
done