1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# Copyright (C) 2008 Aaron Bentley <aaron@aaronbentley.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import copy
from cStringIO import StringIO
import os.path
import shutil
import sys
import tempfile
from bzrlib import (builtins, diff, osutils, patches, workingtree)
from bzrlib.plugins.bzrtools import colordiff, hunk_selector
from bzrlib.plugins.bzrtools.patch import run_patch
from bzrlib.plugins.shelf2 import prepare_shelf
class Shelver(object):
def __init__(self, work_tree, target_tree, path):
self.work_tree = work_tree
self.target_tree = target_tree
self.path = path
self.diff_file = StringIO()
self.text_differ = diff.DiffText(self.target_tree, self.work_tree,
self.diff_file)
self.diff_writer = colordiff.DiffWriter(sys.stdout, False)
@classmethod
def from_args(klass, revision=None):
tree, path = workingtree.WorkingTree.open_containing('.')
target_tree = builtins._get_one_revision_tree('shelf2', revision,
tree.branch, tree)
return klass(tree, target_tree, path)
def run(self):
creator = prepare_shelf.ShelfCreator(self.work_tree, self.target_tree)
self.tempdir = tempfile.mkdtemp()
try:
for change in creator:
if change[0] == 'modify text':
self.handle_modify_text(creator, change[1])
choice = self.prompt('Shelve changes? [y/n]')
if choice == 'y':
creator.write_shelf()
creator.transform()
finally:
shutil.rmtree(self.tempdir)
creator.finalize()
def get_parsed_patch(self, file_id):
old_path = self.work_tree.id2path(file_id)
new_path = self.target_tree.id2path(file_id)
try:
patch = self.text_differ.diff(file_id, old_path, new_path, 'file',
'file')
self.diff_file.seek(0)
return patches.parse_patch(self.diff_file)
finally:
self.diff_file.truncate(0)
def __getchar(self):
import tty
import termios
fd = sys.stdin.fileno()
settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
return ch
def prompt(self, question):
print question,
char = self.__getchar()
print ""
return char
def get_patched_text(self, file_id, patch):
target_file = self.target_tree.get_file(file_id)
try:
if len(patch.hunks) == 0:
return target_file.read()
filename = os.path.join(self.tempdir, 'patch-target')
outfile = open(filename, 'w+b')
try:
osutils.pumpfile(target_file, outfile)
finally:
outfile.close()
finally:
target_file.close()
run_patch('.', [str(patch)], target_file=filename)
outfile = open(filename, 'rb')
try:
return outfile.read()
finally:
outfile.close()
def handle_modify_text(self, creator, file_id):
parsed = self.get_parsed_patch(file_id)
selected_hunks = []
final_patch = copy.copy(parsed)
final_patch.hunks = []
for hunk in parsed.hunks:
self.diff_writer.write(str(hunk))
char = self.prompt('Shelve? [y/n]')
if char == 'n':
final_patch.hunks.append(hunk)
patched_text = self.get_patched_text(file_id, final_patch)
creator.shelve_text(file_id, patched_text)
class Unshelver(object):
@classmethod
def from_args(klass):
tree, path = workingtree.WorkingTree.open_containing('.')
shelf_filename = tree.bzrdir.root_transport.local_abspath('.shelf2/01')
return klass(tree, shelf_filename)
def __init__(self, tree, shelf_filename):
self.tree = tree
self.shelf_filename = shelf_filename
def run(self):
self.tree.lock_write()
try:
unshelver = prepare_shelf.Unshelver.from_tree_and_shelf(
self.tree, self.shelf_filename)
unshelver.unshelve()
finally:
self.tree.unlock()
|