Skip to content

Instantly share code, notes, and snippets.

@mrdrozdov mrdrozdov/

Created Feb 22, 2020
What would you like to do?
#!/usr/bin/env python
import json
import os
import shutil
import subprocess
import sys
import tempfile
# env
GT_HOME = os.environ.get('GT_HOME', os.path.expanduser('~/.git-tmp'))
GT_CONFIG = os.path.join(GT_HOME, 'config.json')
GT_STATE = os.path.join(GT_HOME, 'state.json')
# defaults
default_config = {}
default_state = {}
# config
def init_config():
if not os.path.exists(GT_HOME):
print('Creating git-tmp home at: {}'.format(GT_HOME))
if not os.path.exists(GT_CONFIG):
print('Creating git-tmp config at: {}'.format(GT_CONFIG))
with open(GT_CONFIG, 'w') as f:
f.write(json.dumps(default_config, sort_keys=True, indent=4))
def read_config():
with open(GT_CONFIG) as f:
return json.loads(
cfg = read_config()
# check if git controlled
if os.system('git rev-parse') != 0:
print('Not a git directory. Cancelling.')
# create tmp dir
def read_state():
if not os.path.exists(GT_STATE):
with open(GT_STATE, 'w') as f:
with open(GT_STATE) as f:
return json.loads(
def write_state(state):
with open(GT_STATE, 'w') as f:
def update_gt_state(projname, temp_dir):
state = read_state()
print('Found previous projects:')
temp_dir_lst = state.get('temp_dir_lst', [])
if len(temp_dir_lst) == 0:
print('None found!')
for prev_projname, prev_temp_dir in temp_dir_lst:
print('{} {} exists={}'.format(prev_projname, prev_temp_dir, os.path.exists(prev_temp_dir)))
state.setdefault('temp_dir_lst', []).append([projname, temp_dir])
res =['git', 'rev-parse', '--git-dir'], stdout=subprocess.PIPE)
gitdir = os.path.abspath(res.stdout.decode('utf-8').strip())
print('Found git directory: {}'.format(gitdir))
projdir = os.path.dirname(gitdir)
projname = os.path.basename(projdir)
temp_dir = tempfile.mkdtemp()
update_gt_state(projname, temp_dir)
print('Current tmp directory: {}'.format(temp_dir))
# copy directory
new_projdir = os.path.join(temp_dir, projname)
print('Copying project directory from: {}'.format(projdir))
shutil.copytree(projdir, new_projdir, symlinks=False, ignore=None)
print('To use the temporary project directory:\n\n\tcd {}'.format(new_projdir))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.