20
20
from urlparse import urlsplit, urlunsplit
21
21
from bzrlib.workingtree import WorkingTree
23
import bzrlib.transform
25
"""This class handle the additional bazaar diff tags
28
"""This class process the bzr patch
32
execute attribute support
30
def __init__(self, tree):
34
def __init__(self, tt, tree, bzrdir):
41
self.skipchange = True
44
self.branch = self.bzrdir.open_branch( )
45
self.repo = self.bzrdir.find_repository( )
46
h = self.branch.revision_history( )
47
self.inv = self.repo.get_inventory(h[-1])
50
def get_fileid_no(self, path):
51
print "+++++++++++++++",path,"+++++++++++++++++"
52
return self.inv.path2id(path)
55
def get_transid(self, path):
56
print "****************",path,"*****************"
57
return self.tt.trans_id_tree_path(path)
35
60
def _extractname(self, s):
72
action = bzrlib.add.add_action_add_and_print
73
added, ignored = bzrlib.add.smart_add([name], False, action)
96
def do_patch(self, changes, targetid):
97
# applay change to target_id
98
print "******** do patch *********"
100
print "******** do patch *********"
75
103
def process(self, cmd = None):
77
os.rename(self.renamed[0], self.renamed[1])
82
if ( cmd.startswith("removed file") or
83
cmd.startswith("removed symlink") ):
85
target = self.extractname(cmd)
86
print "removing '%s'"%target
87
if not cmd.startswith("removed file"):
89
self.tree.remove([target])
91
elif cmd.startswith("removed directory"):
93
target = self.extractname(cmd)
94
print "removing '%s'"%target
96
self.tree.remove([target])
98
elif cmd.startswith("added file"):
99
target = self.extractname(cmd)
100
print "adding '%s'"%target
105
elif cmd.startswith("added directory"):
106
target = self.extractname(cmd)
107
print "adding '%s'"%target
111
elif cmd.startswith("added symlink"):
105
while cmd and len(cmd) and cmd[-1] in "\n\r":
108
if ( cmd == None or cmd.startswith("===") ) and self.changes:
109
assert(self.target_id)
110
self.do_patch(self.changes, self.target_id)
111
self.target_id = None
114
if cmd == None: return
115
if not cmd.startswith("==="):
116
if self.skipchange: return
117
assert(self.target_id)
118
self.changes.append(cmd)
121
# thr target_id is set during a rename; but there is possibility
122
# that after a rename, is not to do a change
123
self.target_id = None
125
self.skipchange = False
127
if ( cmd.startswith("=== removed file") or
128
cmd.startswith("=== removed directory") or
129
cmd.startswith("=== removed symlink") ):
131
target = self.extractname(cmd)
132
tid = self.get_transid(target)
133
self.tt.delete_versioned(tid)
134
print "removing '%s'"%target
135
self.skipchange = True
137
elif cmd.startswith("=== added file"):
138
target = self.extractname(cmd)
139
self.target_id = self.get_transid(target)
141
elif cmd.startswith("=== modified file"):
142
target = self.extractname(cmd)
143
self.target_id = self.get_transid(target)
145
elif cmd.startswith("=== added directory"):
146
target = self.extractname(cmd)
147
pdir = os.path.basedir(target)
148
pname = os.path.basename(target)
149
pdir_id = self.get_transid(pdir)
150
self.tt.new_directory(pname, pdir_id)
151
print "adding '%s'"%target
153
elif cmd.startswith("=== added symlink"):
112
154
assert(not self.link)
113
155
self.link = self.extractname(cmd)
115
elif cmd.startswith("target is"):
157
elif cmd.startswith("=== target is"):
116
158
assert(self.link)
117
159
target = self.extractname(cmd)
160
pdir = os.path.basedir(self.link)
161
pname = os.path.basename(self.link)
162
pdir_id = self.get_transid(pdir)
163
self.tt.new_symlink(pname, pdir_id, target )
118
164
print "symlinking '%s' => '%s'"%(target, self.link)
119
os.symlink(target, self.link)
123
elif ( cmd.startswith("renamed symlink") or
124
cmd.startswith("renamed file") or
125
cmd.startswith("renamed directory") ):
127
space = cmd.find(" ",10) # find the 2nd space
167
elif ( cmd.startswith("=== renamed symlink") or
168
cmd.startswith("=== renamed file") or
169
cmd.startswith("=== renamed directory") ):
171
space = cmd.find(" ",14) # find the 2nd space
129
173
source,dest = self.extractnames(cmd[space+1:])
174
sourceid = self.get_transid(source)
175
pdir = os.path.basedir(dest)
176
pname = os.path.basename(dest)
177
pdir_id = self.get_transid(pdir)
178
self.tt.adjust_path(pname, pdir_id, sourceid)
130
180
print "renaming '%s' => '%s'"%(source,dest)
132
#os.rename(source,dest)
133
self.tree.rename_one(source,dest)
136
184
sys.stderr.write("Unsupported tag: '%s'\n"%cmd)
193
tree = WorkingTree.open_containing(u'.')[0]
194
tt = bzrlib.transform.TreeTransform(tree)
195
bzrdir = bzrlib.bzrdir.BzrDir.open(".")
196
bzr_tags_proc = BzrPatchProc(tt, tree, bzrdir)
199
# we have to remove the \n\r
200
bzr_tags_proc.process(l)
202
bzr_tags_proc.flush( )
203
bzr_tags_proc.apply( )
140
205
def patch(branch, location, strip, legacy):
141
206
"""Apply a patch to a branch, using patch(1). URLs may be used."""
160
225
child_proc.stdin.close()
161
226
r = child_proc.wait()
163
bzr_tags_proc = BzrTagProc(WorkingTree.open_containing(u'.')[0])
166
if line.startswith("=== "):
168
child_proc.stdin.close()
169
r = child_proc.wait()
171
bzr_tags_proc.process(line[4:])
174
child_proc = Popen(cmd, stdin=PIPE)
175
#sys.stdout.write("# %s"%line)
176
child_proc.stdin.write(line)
178
child_proc.stdin.close()
179
r = child_proc.wait()
180
bzr_tags_proc.flush( )
228
#bzr_tags_proc = BzrTagProc(WorkingTree.open_containing(u'.')[0])
230
#for line in my_file:
231
#if line.startswith("=== "):
233
#child_proc.stdin.close()
234
#r = child_proc.wait()
236
#bzr_tags_proc.process(line[4:])
239
#child_proc = Popen(cmd, stdin=PIPE)
240
##sys.stdout.write("# %s"%line)
241
#child_proc.stdin.write(line)
243
#child_proc.stdin.close()
244
#r = child_proc.wait()
245
#bzr_tags_proc.flush( )
246
do_patch(sys.stdin.readlines( ))
b'\\ No newline at end of file'