Skip to content

Instantly share code, notes, and snippets.

@hartikainen
Last active March 13, 2020 10:43
Show Gist options
  • Save hartikainen/2cd351aea6af11b1a48632bc51aa9985 to your computer and use it in GitHub Desktop.
Save hartikainen/2cd351aea6af11b1a48632bc51aa9985 to your computer and use it in GitHub Desktop.
Saves the diff between index and working copy
# NOTE(hartikainen): Might want to check that the working tree is clean.
commit_sha="$(cat ./name_rev.txt | awk -F' ' '{print $1}')"
git checkout "${commit_sha}"
echo "Applying patch: $(cat ./patch.diff)"
git apply patch.diff
import os
import git
repository_path = os.getcwd()
repository = git.Repo(repository_path, search_parent_directories=True)
# See https://gitpython.readthedocs.io/en/stable/tutorial.html#obtaining-diff-information
diff = repository.git.diff('HEAD')
with open('./patch.diff', 'w') as f:
# NOTE(hartikainen): Needs a line break at the end in order for
# `git apply ...` to work.
f.write(f"{diff}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment