14
14
DEFAULT_IGNORE.append('./.bzr-shelf*')
17
return run_bzr('root')[0].strip()
19
def shelf_suffix(index):
32
stem = os.path.join(tree_root(), '.bzr-shelf')
33
for end in name_sequence():
35
if not os.path.exists(name):
39
stem = os.path.join(tree_root(), '.bzr-shelf')
40
shelves = glob.glob(stem)
41
shelves.extend(glob.glob(stem + '-*'))
42
def shelf_index(name):
45
return int(name[len(stem)+1:])
46
shelvenums = [shelf_index(f) for f in shelves]
49
if len(shelvenums) == 0:
51
return stem + shelf_suffix(shelvenums[-1])
53
def get_shelf_message(shelf):
55
if not shelf.startswith(prefix):
57
return shelf[len(prefix):shelf.index('\n')]
63
raise Exception("No shelf found in '%s'" % tree_root())
65
patch = open(shelf, 'r').read()
67
print >>sys.stderr, "Reapplying shelved patches",
68
message = get_shelf_message(patch)
69
if message is not None:
70
print >>sys.stderr, ' "%s"' % message
72
print >>sys.stderr, ""
73
pipe = os.popen('patch -d %s -s -p0' % tree_root(), 'w')
77
if pipe.close() is not None:
78
raise Exception("Failed running patch!")
81
print 'Diff status is now:'
82
os.system('bzr diff | diffstat')
86
class QuitException(Exception):
89
def shelve(message = None, revision = None, file_list = None):
91
if revision is not None:
92
cmd.extend(['--revision', str(revision[0])])
93
if file_list is not None:
95
patches = parse_patches(run_bzr(cmd))
97
patches = HunkSelector(patches).select()
101
if len(patches) == 0:
102
print >>sys.stderr, 'Nothing to shelve'
106
print >>sys.stderr, "Saving shelved patches to", shelf
107
shelf = open(shelf, 'a')
108
if message is not None:
109
assert '\n' not in message
110
shelf.write("# shelf: %s\n" % message)
111
for patch in patches:
112
shelf.write(str(patch))
115
os.fsync(shelf.fileno())
118
print >>sys.stderr, "Reverting shelved patches"
119
pipe = os.popen('patch -d %s -sR -p0' % tree_root(), 'w')
120
for patch in patches:
121
pipe.write(str(patch))
124
if pipe.close() is not None:
125
raise Exception("Failed running patch!")
127
print 'Diff status is now:'
128
os.system('bzr diff | diffstat')
132
16
def run_bzr(args):
133
17
if type(args) is str:
137
21
if pipe.close() is not None:
138
22
raise Exception("Failed running bzr")
25
class QuitException(Exception):
30
self.bzr_root = run_bzr('root')[0].strip()
32
def shelf_suffix(self, index):
42
yield self.shelf_suffix(i)
45
stem = os.path.join(self.bzr_root, '.bzr-shelf')
46
for end in name_sequence():
48
if not os.path.exists(name):
52
stem = os.path.join(self.bzr_root, '.bzr-shelf')
53
shelves = glob.glob(stem)
54
shelves.extend(glob.glob(stem + '-*'))
55
def shelf_index(name):
58
return int(name[len(stem)+1:])
59
shelvenums = [shelf_index(f) for f in shelves]
62
if len(shelvenums) == 0:
64
return stem + self.shelf_suffix(shelvenums[-1])
66
def get_shelf_message(self, shelf):
68
if not shelf.startswith(prefix):
70
return shelf[len(prefix):shelf.index('\n')]
73
shelf = self.last_shelf()
76
raise Exception("No shelf found in '%s'" % self.bzr_root)
78
patch = open(shelf, 'r').read()
80
print >>sys.stderr, "Reapplying shelved patches",
81
message = self.get_shelf_message(patch)
82
if message is not None:
83
print >>sys.stderr, ' "%s"' % message
85
print >>sys.stderr, ""
86
pipe = os.popen('patch -d %s -s -p0' % self.bzr_root, 'w')
90
if pipe.close() is not None:
91
raise Exception("Failed running patch!")
94
print 'Diff status is now:'
95
os.system('bzr diff | diffstat')
99
def shelve(self, message=None, revision=None, file_list=None):
101
if revision is not None:
102
cmd.extend(['--revision', str(revision[0])])
103
if file_list is not None:
104
cmd.extend(file_list)
105
patches = parse_patches(run_bzr(cmd))
107
patches = HunkSelector(patches).select()
108
except QuitException:
111
if len(patches) == 0:
112
print >>sys.stderr, 'Nothing to shelve'
115
shelf = self.next_shelf()
116
print >>sys.stderr, "Saving shelved patches to", shelf
117
shelf = open(shelf, 'a')
118
if message is not None:
119
assert '\n' not in message
120
shelf.write("# shelf: %s\n" % message)
121
for patch in patches:
122
shelf.write(str(patch))
125
os.fsync(shelf.fileno())
128
print >>sys.stderr, "Reverting shelved patches"
129
pipe = os.popen('patch -d %s -sR -p0' % self.bzr_root, 'w')
130
for patch in patches:
131
pipe.write(str(patch))
134
if pipe.close() is not None:
135
raise Exception("Failed running patch!")
137
print 'Diff status is now:'
138
os.system('bzr diff | diffstat')