Skip to content

Instantly share code, notes, and snippets.

@mahelbir
Created March 7, 2023 01:17
Show Gist options
  • Save mahelbir/75e4ccb2f0979897444edb05eb7fa8b2 to your computer and use it in GitHub Desktop.
Save mahelbir/75e4ccb2f0979897444edb05eb7fa8b2 to your computer and use it in GitHub Desktop.
Python script to zip changed files with .git (differences between working directory and staging area)
import subprocess
import os
import zipfile
import sys
from datetime import datetime
out = subprocess.check_output(["git", "diff", "--name-only", "--diff-filter=d"]).decode().splitlines()
out2 = subprocess.check_output(["git", "ls-files", "--others", "--exclude-standard"]).decode().splitlines()
if len(out) > 0 or len(out2) > 0:
if ".gitignore" in out:
out.remove(".gitignore")
if ".gitignore" in out2:
out2.remove(".gitignore")
if sys.argv[0] in out:
out.remove(sys.argv[0])
if sys.argv[0] in out2:
out2.remove(sys.argv[0])
zipName = "patch-" + datetime.now().strftime("%Y%m%d%H%M") + ".zip"
with zipfile.ZipFile(zipName, "w") as zip:
for line in out:
if os.path.exists(line):
zip.write(line)
for line in out2:
if os.path.exists(line):
zip.write(line)
print("New patch:", zipName)
else:
print("No patch!")
@mahelbir
Copy link
Author

mahelbir commented Mar 7, 2023

Usage: py PATCH.py
OR
Usage: python PATCH.py
OR
Usage: python3 PATCH.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment