#13 - add -k option to stash save to allow stashing without reverting wc

This commit is contained in:
Josh Holtrop 2012-06-27 09:08:59 -04:00
parent daa68953f9
commit fd8702af29
2 changed files with 34 additions and 22 deletions

5
README
View File

@ -61,8 +61,9 @@ Implemented subcommands:
- the stashes behaves as a "stack" where "save" pushes a new stash object - the stashes behaves as a "stack" where "save" pushes a new stash object
and "pop" pops the newest one from the top of the stack and "pop" pops the newest one from the top of the stack
commands: commands:
save (default if not specified): save [-k] (default if not specified):
- save changes as a "stash" object and revert them from working copy - save changes as a "stash" object
- revert changes from working copy unless -k (keep working copy) given
- this only works with text files, not binary files - this only works with text files, not binary files
list: list:
- show a list of all stash objects - show a list of all stash objects

51
jsvn
View File

@ -13,6 +13,7 @@ from subprocess import *
import traceback import traceback
import datetime import datetime
import types import types
import getopt
STATUS_LINE_REGEX = r'[ACDIMRX?!~ ][CM ][L ][+ ][SX ][KOTB ]..(.+)' STATUS_LINE_REGEX = r'[ACDIMRX?!~ ][CM ][L ][+ ][SX ][KOTB ]..(.+)'
@ -812,11 +813,19 @@ def externals(argv, svn, out):
return RET_OK return RET_OK
def stash(argv, svn, out): def stash(argv, svn, out):
argv = argv[1:] # strip 'stash' command
action = 'save' action = 'save'
if len(argv) >= 2: if len(argv) >= 1:
if not argv[1].startswith('-'): if not argv[0].startswith('-'):
action = argv[1] action = argv[0]
argv = argv[1:]
# now argv only contains options/arguments to the stash subcommand itself
if action == 'save': if action == 'save':
keep_wc = False
options, args = getopt.getopt(argv, 'k')
for opt, val in options:
if opt == '-k':
keep_wc = True
owd = os.getcwd() owd = os.getcwd()
wc_dir = get_svn_wc_root(svn) wc_dir = get_svn_wc_root(svn)
os.chdir(wc_dir) os.chdir(wc_dir)
@ -864,17 +873,19 @@ def stash(argv, svn, out):
files_changed[target] = action files_changed[target] = action
else: else:
files_changed[target] = prop_action files_changed[target] = prop_action
Popen([svn, 'revert', target], stdout=PIPE).wait() if not keep_wc:
if action == 'A': # do the actual revert if -k not given
# a file was added, so to stash it we must remove Popen([svn, 'revert', target], stdout=PIPE).wait()
# it in addition to reverting the add if action == 'A':
if os.path.isfile(target): # a file was added, so to stash it we must
os.unlink(target) # remove it in addition to reverting the add
elif os.path.isdir(target): if os.path.isfile(target):
if len(os.listdir(target)) == 0: os.unlink(target)
os.rmdir(target) elif os.path.isdir(target):
else: if len(os.listdir(target)) == 0:
raise ValueError('unhandled target type') os.rmdir(target)
else:
raise ValueError('unhandled target type')
# write stash info # write stash info
if len(files_changed) == 1: if len(files_changed) == 1:
fname = files_changed.keys()[0] fname = files_changed.keys()[0]
@ -959,8 +970,8 @@ def stash(argv, svn, out):
stash_ids = get_stash_ids(svn) stash_ids = get_stash_ids(svn)
if len(stash_ids) > 0: if len(stash_ids) > 0:
stash_idx = stash_ids[-1] stash_idx = stash_ids[-1]
if len(argv) >= 3: if len(argv) >= 1:
stash_idx = int(argv[2]) stash_idx = int(argv[0])
stash_fname = get_stash_fname(svn, stash_idx) stash_fname = get_stash_fname(svn, stash_idx)
p = Popen([svn, 'patch', stash_fname], stdout=PIPE) p = Popen([svn, 'patch', stash_fname], stdout=PIPE)
filter_update(p.stdout, out) filter_update(p.stdout, out)
@ -977,8 +988,8 @@ def stash(argv, svn, out):
stash_ids = get_stash_ids(svn) stash_ids = get_stash_ids(svn)
if len(stash_ids) > 0: if len(stash_ids) > 0:
stash_id = stash_ids[-1] stash_id = stash_ids[-1]
if len(argv) >= 3: if len(argv) >= 1:
stash_id = int(argv[2]) stash_id = int(argv[0])
if stash_id in stash_ids: if stash_id in stash_ids:
stash_fname = get_stash_fname(svn, stash_id) stash_fname = get_stash_fname(svn, stash_id)
fd = open(stash_fname, 'r') fd = open(stash_fname, 'r')
@ -993,8 +1004,8 @@ def stash(argv, svn, out):
stash_ids = get_stash_ids(svn) stash_ids = get_stash_ids(svn)
if len(stash_ids) > 0: if len(stash_ids) > 0:
stash_id = stash_ids[-1] stash_id = stash_ids[-1]
if len(argv) >= 3: if len(argv) >= 1:
stash_id = int(argv[2]) stash_id = int(argv[0])
stash_fname = get_stash_fname(svn, stash_id) stash_fname = get_stash_fname(svn, stash_id)
os.unlink(stash_fname) os.unlink(stash_fname)
out.write('Dropped stash %d\n' % stash_id) out.write('Dropped stash %d\n' % stash_id)