~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-07-01 22:25:01 UTC
  • mfrom: (408.1.1 bzrtools)
  • Revision ID: aaron.bentley@utoronto.ca-20060701222501-a5dfbdeacbccd2da
Update colordiff to use wrapped versions of diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
 
57
57
    def delete(self, patch):
58
58
        path = self.__path_from_user(patch)
59
 
        os.remove(path)
 
59
        os.rename(path, '%s~' % path)
60
60
 
61
 
    def display(self, patch):
62
 
        path = self.__path_from_user(patch)
 
61
    def display(self, patch=None):
 
62
        if patch is None:
 
63
            path = self.last_patch()
 
64
        else:
 
65
            path = self.__path_from_user(patch)
63
66
        sys.stdout.write(open(path).read())
64
67
 
65
68
    def list(self):
78
81
    def __path_from_user(self, patch_id):
79
82
        try:
80
83
            patch_index = int(patch_id)
81
 
        except TypeError:
 
84
        except (TypeError, ValueError):
82
85
            raise CommandError("Invalid patch name '%s'" % patch_id)
83
86
 
84
87
        path = self.__path(patch_index)
130
133
            return None
131
134
        return patch[len(self.MESSAGE_PREFIX):patch.index('\n')]
132
135
 
133
 
    def unshelve(self, patch_source, all_hunks=False, force=False):
 
136
    def unshelve(self, patch_source, patch_name=None, all=False, force=False):
134
137
        self._check_upgrade()
135
138
 
136
 
        patch_name = self.last_patch()
137
 
 
138
139
        if patch_name is None:
 
140
            patch_path = self.last_patch()
 
141
        else:
 
142
            patch_path = self.__path_from_user(patch_name)
 
143
 
 
144
        if patch_path is None:
139
145
            raise CommandError("No patch found on shelf %s" % self.name)
140
146
 
141
 
        hunks = FilePatchSource(patch_name).readhunks()
142
 
        if all_hunks:
143
 
            to_unshelve = hunks
 
147
        patches = FilePatchSource(patch_path).readpatches()
 
148
        if all:
 
149
            to_unshelve = patches
144
150
            to_remain = []
145
151
        else:
146
 
            to_unshelve, to_remain = UnshelveHunkSelector(hunks).select()
 
152
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
147
153
 
148
154
        if len(to_unshelve) == 0:
149
155
            raise CommandError('Nothing to unshelve')
150
156
 
151
 
        message = self.get_patch_message(patch_name)
 
157
        message = self.get_patch_message(patch_path)
152
158
        if message is None:
153
159
            message = "No message saved with patch."
154
160
        self.log('Unshelving from %s/%s: "%s"\n' % \
155
 
                (self.name, os.path.basename(patch_name), message))
 
161
                (self.name, os.path.basename(patch_path), message))
156
162
 
157
163
        try:
158
164
            self._run_patch(to_unshelve, dry_run=True)
159
165
            self._run_patch(to_unshelve)
160
166
        except PatchFailed:
161
167
            try:
162
 
                self._run_patch(to_unshelve, strip=0, dry_run=True)
163
 
                self._run_patch(to_unshelve, strip=0)
 
168
                self._run_patch(to_unshelve, strip=1, dry_run=True)
 
169
                self._run_patch(to_unshelve, strip=1)
164
170
            except PatchFailed:
165
171
                if force:
166
172
                    self.log('Warning: Unshelving failed, forcing as ' \
174
180
                    "longer applies cleanly to the working tree!")
175
181
 
176
182
        # Backup the shelved patch
177
 
        os.rename(patch_name, '%s~' % patch_name)
 
183
        os.rename(patch_path, '%s~' % patch_path)
178
184
 
179
185
        if len(to_remain) > 0:
180
 
            f = open(patch_name, 'w')
181
 
            for hunk in to_remain:
182
 
                f.write(str(hunk))
 
186
            f = open(patch_path, 'w')
 
187
            for patch in to_remain:
 
188
                f.write(str(patch))
183
189
            f.close()
184
190
 
185
 
    def shelve(self, patch_source, all_hunks=False, message=None):
 
191
    def shelve(self, patch_source, all=False, message=None):
186
192
        self._check_upgrade()
187
193
 
188
 
        hunks = patch_source.readhunks()
 
194
        patches = patch_source.readpatches()
189
195
 
190
 
        if all_hunks:
191
 
            to_shelve = hunks
 
196
        if all:
 
197
            to_shelve = patches
192
198
        else:
193
 
            to_shelve = ShelveHunkSelector(hunks).select()[0]
 
199
            to_shelve = ShelveHunkSelector(patches).select()[0]
194
200
 
195
201
        if len(to_shelve) == 0:
196
202
            raise CommandError('Nothing to shelve')
199
205
            timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
200
206
            message = "Changes shelved on %s" % timestamp
201
207
 
202
 
        patch_name = self.next_patch()
 
208
        patch_path = self.next_patch()
203
209
        self.log('Shelving to %s/%s: "%s"\n' % \
204
 
                (self.name, os.path.basename(patch_name), message))
 
210
                (self.name, os.path.basename(patch_path), message))
205
211
 
206
 
        patch = open(patch_name, 'a')
 
212
        f = open(patch_path, 'a')
207
213
 
208
214
        assert '\n' not in message
209
 
        patch.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
210
 
 
211
 
        for hunk in to_shelve:
212
 
            patch.write(str(hunk))
213
 
 
214
 
        patch.flush()
215
 
        os.fsync(patch.fileno())
216
 
        patch.close()
 
215
        f.write("%s%s\n" % (self.MESSAGE_PREFIX, message))
 
216
 
 
217
        for patch in to_shelve:
 
218
            f.write(str(patch))
 
219
 
 
220
        f.flush()
 
221
        os.fsync(f.fileno())
 
222
        f.close()
217
223
 
218
224
        try:
219
225
            self._run_patch(to_shelve, reverse=True, dry_run=True)
220
226
            self._run_patch(to_shelve, reverse=True)
221
227
        except PatchFailed:
222
228
            try:
223
 
                self._run_patch(to_shelve, reverse=True, strip=0, dry_run=True)
224
 
                self._run_patch(to_shelve, reverse=True, strip=0)
 
229
                self._run_patch(to_shelve, reverse=True, strip=1, dry_run=True)
 
230
                self._run_patch(to_shelve, reverse=True, strip=1)
225
231
            except PatchFailed:
226
232
                raise CommandError("Failed removing shelved changes from the"
227
233
                    "working tree!")
228
234
 
229
 
    def _run_patch(self, patches, strip=1, reverse=False, dry_run=False):
 
235
    def _run_patch(self, patches, strip=0, reverse=False, dry_run=False):
230
236
        args = ['patch', '-d', self.base, '-s', '-p%d' % strip, '-f']
231
237
        if reverse:
232
238
            args.append('-R')