Last active
May 8, 2023 15:01
-
-
Save rayanfer32/5775c92d0ccddcf1d0af3650c42f35e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Run this script using `python backup_stashes.py` | |
To apply the stash use command | |
`git apply path/to/stash.patch` | |
""" | |
import os | |
import re | |
import hashlib | |
import unicodedata | |
OUT_FOLDER_NAME = "stash_backup" | |
def slugify(value, allow_unicode=False): | |
value = str(value) | |
if allow_unicode: | |
value = unicodedata.normalize('NFKC', value) | |
else: | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') | |
value = re.sub(r'[^\w\s-]', '', value.lower()) | |
return re.sub(r'[-\s]+', '-', value).strip('-_') | |
try: | |
os.mkdir(OUT_FOLDER_NAME) | |
except Exception as err: | |
print(f"{OUT_FOLDER_NAME} already created.") | |
# Get the list of stashes | |
stash_list = os.popen("git stash list").read().splitlines() | |
# Iterate over the stashes and create a patch file for each one | |
for i, stash in enumerate(stash_list): | |
# Get the patch for the stash | |
patch_content = os.popen(f"git stash show -p {stash.split(':')[0]}").read() | |
patch_hash = hashlib.sha256(patch_content.encode()).hexdigest()[:8] | |
# remove special chars from name and append content hash to the filename | |
stash_name = stash.split('}:')[1] | |
safe_filename = f"{slugify(stash_name)}_{patch_hash}.patch" | |
# Create the patch file | |
with open(f"stash_backup/{safe_filename}", "w") as patch_file: | |
patch_file.write(patch_content) | |
print('Created:',safe_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment