9
9
from bzrlib.commands import Command
10
10
from bzrlib.branch import Branch
11
11
from bzrlib import DEFAULT_IGNORE
12
from hunk_selector import HunkSelector
12
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
13
13
from diffstat import DiffStat
15
15
DEFAULT_IGNORE.append('./.bzr-shelf*')
20
20
class Shelf(object):
21
def __init__(self, location):
21
def __init__(self, location, name='default'):
22
22
self.branch = Branch.open_containing(location)[0]
24
def shelf_suffix(self, index):
23
base = self.branch.controlfilename('x-shelf')
24
self.shelf_dir = os.path.join(base, name)
26
# FIXME surely there's an easier way to do this?
27
t = self.branch._transport
28
for dir in [base, self.shelf_dir]:
32
def __path(self, idx):
33
return os.path.join(self.shelf_dir, '%.2d' % idx)
30
35
def next_shelf(self):
34
yield self.shelf_suffix(i)
37
stem = os.path.join(self.branch.base, '.bzr-shelf')
38
for end in name_sequence():
38
name = self.__path(index)
40
39
if not os.path.exists(name):
43
43
def last_shelf(self):
44
stem = os.path.join(self.branch.base, '.bzr-shelf')
45
shelves = glob.glob(stem)
46
shelves.extend(glob.glob(stem + '-*'))
47
def shelf_index(name):
50
return int(name[len(stem)+1:])
51
shelvenums = [shelf_index(f) for f in shelves]
44
shelves = os.listdir(self.shelf_dir)
45
indexes = [int(f) for f in shelves]
54
if len(shelvenums) == 0:
56
return stem + self.shelf_suffix(shelvenums[-1])
51
return self.__path(indexes[-1])
58
53
def get_shelf_message(self, shelf):
59
54
prefix = "# shelf: "
62
57
return shelf[len(prefix):shelf.index('\n')]
59
def unshelve(self, pick_hunks=False):
65
60
shelf = self.last_shelf()
68
63
raise Exception("No shelf found in '%s'" % self.branch.base)
70
patch = open(shelf, 'r').read()
65
patches = parse_patches(open(shelf, 'r').readlines())
68
patches = UnshelveHunkSelector(patches).select()
73
print >>sys.stderr, 'Nothing to unshelve'
72
76
print >>sys.stderr, "Reapplying shelved patches",
73
message = self.get_shelf_message(patch)
77
message = self.get_shelf_message(open(shelf, 'r').read())
74
78
if message is not None:
75
79
print >>sys.stderr, ' "%s"' % message
77
81
print >>sys.stderr, ""
78
82
pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
84
pipe.write(str(patch))
82
87
if pipe.close() is not None:
98
103
return out.readlines()
100
def shelve(self, all_hunks=False, message=None, revision=None,
105
def shelve(self, pick_hunks=False, message=None, revision=None,
102
107
patches = parse_patches(self.get_patches(revision, file_list))
106
patches = HunkSelector(patches).select()
111
patches = ShelveHunkSelector(patches).select()
107
112
except QuitException: