~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to patch.py

  • Committer: ghigo
  • Date: 2006-03-14 21:21:49 UTC
  • mto: This revision was merged to the branch mainline in revision 330.
  • Revision ID: ghigo@venice-20060314212149-174e9a109f343f26
Now the patch command work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
    def __init__(self, tt, tree, bzrdir):
35
35
        self.link = None
36
36
        self.changes = []
 
37
        self.mode = None
37
38
        self.command = None
38
39
        self.target_id = None
39
40
        self.tt = tt
47
48
        self.inv = self.repo.get_inventory(h[-1])
48
49
 
49
50
 
50
 
    def get_fileid_no(self, path):
51
 
        print "+++++++++++++++",path,"+++++++++++++++++"
52
 
        return self.inv.path2id(path)
53
 
 
54
 
 
55
51
    def get_transid(self, path):
56
 
        print "****************",path,"*****************"
57
52
        return self.tt.trans_id_tree_path(path)
58
53
 
59
54
 
69
64
                continue
70
65
 
71
66
            if s[i] == x:
72
 
                return s[3:i],i+1
 
67
                return s[1:i],i+1
73
68
 
74
69
            i += 1
75
70
 
76
71
        assert(False)
77
72
 
 
73
 
78
74
    def extractname(self, s):
79
 
        space = s.find(" ",13)    # find the 2nd space
80
 
        assert(space)
 
75
        space=-1
 
76
        for i in range(0,3):
 
77
            space = s.find(" ",space+1)    # find the 2nd space
 
78
        assert(space>=0)
81
79
        return self._extractname(s[space+1:])[0]
82
80
 
 
81
 
83
82
    def extractnames(self, s):
84
 
        space = s.find(" ",14)    # find the 2nd space
85
 
        assert(space)
 
83
        space=-1
 
84
        for i in range(0,3):
 
85
            space = s.find(" ",space+1)    # find the 2nd space
 
86
        assert(space>=0)
86
87
        s=s[space+1:]
87
88
        source, pos = self._extractname(s)
88
89
        assert( pos +4 < len(s) )
90
91
 
91
92
        return source,dest
92
93
 
 
94
 
93
95
    def flush(self):
94
96
        self.process( )
95
97
 
 
98
 
96
99
    def do_patch(self, changes, targetid):
97
 
        # applay change to target_id
98
 
        print "******** do patch *********"
99
 
        print changes
100
 
        print "******** do patch *********"
 
100
        # apply change to target_id
 
101
        if 1:
 
102
            cmd = ['patch', '-o', self.tt._limbo_name(targetid),
 
103
                '--directory', self.branch.base, '-p1']
 
104
            #sys.stdout.write("# %r\n"%cmd)
 
105
            child_proc = Popen(cmd, stdin=PIPE)
 
106
            for line in changes:
 
107
                #sys.stdout.write("# %s\n"%line)
 
108
                child_proc.stdin.write(line+'\n')
 
109
            child_proc.stdin.close()
 
110
            r = child_proc.wait()
 
111
 
101
112
        self.changes = []
102
113
 
103
114
    def process(self, cmd = None):
107
118
 
108
119
        if ( cmd == None or cmd.startswith("===") ) and self.changes:
109
120
            assert(self.target_id)
110
 
            self.do_patch(self.changes, self.target_id)
 
121
 
 
122
            #print "********** mode=",self.mode
 
123
 
 
124
            if self.mode == "mv" or self.mode == "mod":
 
125
                self.tt.delete_contents(self.target_id)
 
126
 
 
127
            if self.mode == "add" or self.mode == "mod" or self.mode == "mv":
 
128
                self.tt.create_file([],self.target_id)
 
129
                self.do_patch(self.changes, self.target_id)
 
130
 
111
131
            self.target_id = None
112
132
            self.changes = []
 
133
            self.mode = None
113
134
 
114
135
        if cmd == None: return
115
136
        if not cmd.startswith("==="):
116
137
            if self.skipchange: return
117
138
            assert(self.target_id)
 
139
            assert(self.mode)
118
140
            self.changes.append(cmd)
119
141
            return
120
142
 
123
145
        self.target_id = None
124
146
        # skip change
125
147
        self.skipchange = False
 
148
        self.mode = None
126
149
 
127
150
        if ( cmd.startswith("=== removed file") or
128
151
             cmd.startswith("=== removed directory") or
129
152
             cmd.startswith("=== removed symlink") ):
130
153
 
131
 
            target = self.extractname(cmd)
 
154
            target = self.extractname(cmd)[2:]
132
155
            tid = self.get_transid(target)
133
156
            self.tt.delete_versioned(tid)
134
157
            print "removing '%s'"%target
135
158
            self.skipchange = True
 
159
            self.mode = "rm"
136
160
 
137
161
        elif cmd.startswith("=== added file"):
138
 
            target = self.extractname(cmd)
 
162
            
 
163
            target = self.extractname(cmd)[2:]
139
164
            self.target_id = self.get_transid(target)
 
165
            #self.tt.create_file([],self.target_id)
 
166
            print "adding '%s'"%target
 
167
            self.mode = "add"
140
168
 
141
169
        elif cmd.startswith("=== modified file"):
142
 
            target = self.extractname(cmd)
 
170
            target = self.extractname(cmd)[2:]
143
171
            self.target_id = self.get_transid(target)
 
172
            self.mode = "mod"
 
173
            print "patching '%s'"%target
144
174
 
145
175
        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
 
176
            target = self.extractname(cmd)[2:]
 
177
            target_id = self.get_transid(target)
 
178
            self.tt.create_directory(target_id)
 
179
            print "adding directory '%s'"%target
152
180
 
153
181
        elif cmd.startswith("=== added symlink"):
154
182
            assert(not self.link)
155
 
            self.link = self.extractname(cmd)
 
183
            self.link = self.extractname(cmd)[2:]
156
184
 
157
185
        elif cmd.startswith("=== target is"):
158
186
            assert(self.link)
159
187
            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 )
164
 
            print "symlinking '%s' => '%s'"%(target, self.link)
 
188
            link_id = self.get_transid(self.link)
 
189
            
 
190
            print "symlinking '%s' => '%s'\n"%(target, link_id)
 
191
            self.tt.create_symlink(target, link_id)
165
192
            self.link = None
166
193
 
167
194
        elif ( cmd.startswith("=== renamed symlink") or
168
195
               cmd.startswith("=== renamed file") or
169
196
               cmd.startswith("=== renamed directory") ):
170
197
 
171
 
            space = cmd.find(" ",14)    # find the 2nd space
172
 
            assert(space)
173
 
            source,dest = self.extractnames(cmd[space+1:])
 
198
            source,dest = self.extractnames(cmd)
 
199
            source = source[2:]
 
200
            dest = dest[2:]
174
201
            sourceid = self.get_transid(source)
175
 
            pdir = os.path.basedir(dest)
176
 
            pname = os.path.basename(dest)
 
202
            pdir,pname = os.path.split(dest)
177
203
            pdir_id = self.get_transid(pdir)
178
204
            self.tt.adjust_path(pname, pdir_id, sourceid)
179
205
 
 
206
            if cmd.startswith("=== renamed file"):
 
207
                self.target_id = sourceid
 
208
                self.mode = "mv"
 
209
 
180
210
            print "renaming '%s' => '%s'"%(source,dest)
181
211
 
182
212
 
225
255
        child_proc.stdin.close()
226
256
        r = child_proc.wait()
227
257
    else:
228
 
        #bzr_tags_proc = BzrTagProc(WorkingTree.open_containing(u'.')[0])
229
 
        #child_proc = None
230
 
        #for line in my_file:
231
 
            #if line.startswith("=== "):
232
 
                #if child_proc:
233
 
                    #child_proc.stdin.close()
234
 
                    #r = child_proc.wait()
235
 
                    #child_proc = None
236
 
                #bzr_tags_proc.process(line[4:])
237
 
            #else:
238
 
                #if not child_proc:
239
 
                    #child_proc = Popen(cmd, stdin=PIPE)
240
 
                ##sys.stdout.write("# %s"%line)
241
 
                #child_proc.stdin.write(line)
242
 
        #if child_proc:
243
 
            #child_proc.stdin.close()
244
 
            #r = child_proc.wait()
245
 
        #bzr_tags_proc.flush( )
 
258
 
246
259
        do_patch(sys.stdin.readlines( ))
247
260
    return r
 
 
b'\\ No newline at end of file'