Skip to content

Instantly share code, notes, and snippets.

@rhizoome
Last active July 29, 2019 15:13
Show Gist options
  • Save rhizoome/91272786c948cf361bb55271b23bcc56 to your computer and use it in GitHub Desktop.
Save rhizoome/91272786c948cf361bb55271b23bcc56 to your computer and use it in GitHub Desktop.
Manage "permanent" lcoal changes in git-repos
We have directory for our Repositories: ~/Repositories
In there is ~/Repositories/projectA and ~/Repositories/projectA.stash. The
command save will save all files defined in projectA.stash to the stash and to
~/Repositories/projectA.patch. Load will search the save and pop it from stash. Reload
will apply ~/Repositories/projectA.patch in case you lost the stash or moved to
another machine.
I have the *.stash and *.patch under separete version control.
If you have a conflict go to the last revision you had no conflict and rebase it.
If you still have a conflict try imerge: https://github.com/mhagger/git-imerge
#!/usr/bin/env python3
import os
import sys
from subprocess import check_call, check_output
list_ = ["git", "stash", "list"]
stash_list = check_output(list_, encoding="UTF-8")
for stash_index in stash_list.split(os.linesep):
if "save_20caea9f0fa9f97e2376889" in stash_index:
part, _, _ = stash_index.partition("}")
_, _, index = part.partition("{")
pop = ["git", "stash", "pop", str(index)]
check_call(pop)
sys.exit(0)
#!/usr/bin/env python3
import codecs
from pathlib import Path
from subprocess import check_output
cwd = Path.cwd()
patch_path = cwd.with_suffix(".patch")
with codecs.open(patch_path, "rb") as f:
check_output(["patch", "-p1"], input=f.read())
#!/usr/bin/env python3
import codecs
import json
import os
import sys
from pathlib import Path
from subprocess import DEVNULL, CalledProcessError, check_call, check_output
save_id = "save_20caea9f0fa9f97e2376889"
list_ = ["git", "stash", "list"]
stash_list = check_output(list_, encoding="UTF-8")
for stash_index in stash_list.split(os.linesep):
if "save_20caea9f0fa9f97e2376889" in stash_index:
print("You already have a local save")
sys.exit(1)
diff = ["git", "diff", "--quiet", "--exit-code"]
try:
check_call(diff)
except CalledProcessError:
cwd = Path.cwd()
stash_info = cwd.with_suffix(".stash")
patch_path = cwd.with_suffix(".patch")
with codecs.open(stash_info, "r", encoding="UTF-8") as f:
stash_files = json.load(f)
patch = ["git", "diff", "--"] + stash_files
with codecs.open(patch_path, "w", encoding="UTF-8") as f:
f.truncate()
check_call(patch, stdout=f, stderr=DEVNULL)
stash = ["git", "stash", "push", "-m", save_id, "--"] + stash_files
check_call(stash)
untag = ["git", "tag", "-d", save_id]
tag = ["git", "tag", save_id]
try:
check_call(untag, stdout=DEVNULL, stderr=DEVNULL)
except CalledProcessError:
check_call(tag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment