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 ShelveHunkSelector, UnshelveHunkSelector
12
from hunk_selector import HunkSelector
13
13
from diffstat import DiffStat
14
from subprocess import Popen, PIPE
15
16
DEFAULT_IGNORE.append('./.bzr-shelf*')
20
21
class Shelf(object):
21
def __init__(self, location, name='default'):
22
def __init__(self, location):
22
23
self.branch = Branch.open_containing(location)[0]
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)
25
def shelf_suffix(self, index):
35
31
def next_shelf(self):
38
name = self.__path(index)
35
yield self.shelf_suffix(i)
38
stem = os.path.join(self.branch.base, '.bzr-shelf')
39
for end in name_sequence():
39
41
if not os.path.exists(name):
43
44
def last_shelf(self):
44
shelves = os.listdir(self.shelf_dir)
45
indexes = [int(f) for f in shelves]
45
stem = os.path.join(self.branch.base, '.bzr-shelf')
46
shelves = glob.glob(stem)
47
shelves.extend(glob.glob(stem + '-*'))
48
def shelf_index(name):
51
return int(name[len(stem)+1:])
52
shelvenums = [shelf_index(f) for f in shelves]
55
if len(shelvenums) == 0:
51
return self.__path(indexes[-1])
57
return stem + self.shelf_suffix(shelvenums[-1])
53
59
def get_shelf_message(self, shelf):
54
60
prefix = "# shelf: "
57
63
return shelf[len(prefix):shelf.index('\n')]
59
def unshelve(self, pick_hunks=False):
60
66
shelf = self.last_shelf()
63
69
raise Exception("No shelf found in '%s'" % self.branch.base)
65
patches = parse_patches(open(shelf, 'r').readlines())
68
patches = UnshelveHunkSelector(patches).select()
73
print >>sys.stderr, 'Nothing to unshelve'
71
patch = open(shelf, 'r').read()
76
73
print >>sys.stderr, "Reapplying shelved patches",
77
message = self.get_shelf_message(open(shelf, 'r').read())
74
message = self.get_shelf_message(patch)
78
75
if message is not None:
79
76
print >>sys.stderr, ' "%s"' % message
81
78
print >>sys.stderr, ""
82
pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
84
pipe.write(str(patch))
87
if pipe.close() is not None:
88
raise Exception("Failed running patch!")
79
run_patch(self.branch.base, (patch,))
92
82
diff_stat = DiffStat(self.get_patches(None, None))
93
83
print 'Diff status is now:\n', diff_stat
97
87
def get_patches(self, revision, file_list):
98
88
from StringIO import StringIO
103
93
return out.readlines()
105
def shelve(self, pick_hunks=False, message=None, revision=None,
95
def shelve(self, all_hunks=False, message=None, revision=None,
107
97
patches = parse_patches(self.get_patches(revision, file_list))
111
patches = ShelveHunkSelector(patches).select()
101
patches = HunkSelector(patches).select()
112
102
except QuitException:
115
105
if len(patches) == 0:
116
106
print >>sys.stderr, 'Nothing to shelve'
119
109
shelf = self.next_shelf()
120
110
print >>sys.stderr, "Saving shelved patches to", shelf
132
122
print >>sys.stderr, "Reverting shelved patches"
133
pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
134
for patch in patches:
135
pipe.write(str(patch))
138
if pipe.close() is not None:
139
raise Exception("Failed running patch!")
123
run_patch(self.branch.base, patches, reverse=True)
141
125
diff_stat = DiffStat(self.get_patches(None, None))
142
126
print 'Diff status is now:\n', diff_stat
130
def run_patch(branch_base, patches, reverse=False):
131
args = ['patch', '-d', branch_base, '-s', '-p0', '-f']
134
process = Popen(args, stdin=PIPE)
135
for patch in patches:
136
process.stdin.write(str(patch))
137
process.stdin.close()
138
result = process.wait()
139
if result not in (0, 1):
140
raise Exception("Error applying patches")