~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2006-02-11 03:27:01 UTC
  • mto: (0.1.73 shelf-tmp)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20060211032701-43996d6a9e744b1a
Make patch parsing cope with shelf messages, starting with '#'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python
2
2
 
3
 
from patches import parse_patches
4
3
import os
5
4
import sys
6
 
import string
7
 
import glob
8
 
import bzrlib
9
 
from bzrlib.commands import Command
10
 
from bzrlib.branch import Branch
11
 
from bzrlib import DEFAULT_IGNORE
12
 
from hunk_selector import HunkSelector
 
5
from errors import CommandError
 
6
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
13
7
from diffstat import DiffStat
14
 
 
15
 
DEFAULT_IGNORE.append('./.bzr-shelf*')
16
 
 
17
 
class QuitException(Exception):
18
 
    pass
 
8
from patchsource import PatchSource, FilePatchSource
 
9
 
 
10
BASE_DIR = '.shelf'
19
11
 
20
12
class Shelf(object):
21
 
    def __init__(self, location):
22
 
        self.branch = Branch.open_containing(location)[0]
23
 
 
24
 
    def shelf_suffix(self, index):
25
 
        if index == 0:
26
 
            return ""
27
 
        else:
28
 
            return "-%d" % index
 
13
    def __init__(self, base, name='default'):
 
14
        self.name = name
 
15
        self.base = base
 
16
        shelf_base = os.path.join(self.base, BASE_DIR)
 
17
        self.shelf_dir = os.path.join(shelf_base, name)
 
18
 
 
19
        for dir in [shelf_base, self.shelf_dir]:
 
20
            if not os.path.isdir(dir):
 
21
                os.mkdir(dir)
 
22
 
 
23
    def __path(self, idx):
 
24
        return os.path.join(self.shelf_dir, '%.2d' % idx)
29
25
 
30
26
    def next_shelf(self):
31
 
        def name_sequence():
32
 
            i = 0
33
 
            while True:
34
 
                yield self.shelf_suffix(i)
35
 
                i = i + 1
36
 
 
37
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
38
 
        for end in name_sequence():
39
 
            name = stem + end
 
27
        index = 0
 
28
        while True:
 
29
            name = self.__path(index)
40
30
            if not os.path.exists(name):
41
31
                return name
 
32
            index += 1
42
33
 
43
34
    def last_shelf(self):
44
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
45
 
        shelves = glob.glob(stem)
46
 
        shelves.extend(glob.glob(stem + '-*'))
47
 
        def shelf_index(name):
48
 
            if name == stem:
49
 
                return 0
50
 
            return int(name[len(stem)+1:])
51
 
        shelvenums = [shelf_index(f) for f in shelves]
52
 
        shelvenums.sort()
 
35
        shelves = os.listdir(self.shelf_dir)
 
36
        indexes = [int(f) for f in shelves]
 
37
        indexes.sort()
53
38
 
54
 
        if len(shelvenums) == 0:
 
39
        if len(indexes) == 0:
55
40
            return None
56
 
        return stem + self.shelf_suffix(shelvenums[-1])
 
41
 
 
42
        return self.__path(indexes[-1])
57
43
 
58
44
    def get_shelf_message(self, shelf):
59
45
        prefix = "# shelf: "
61
47
            return None
62
48
        return shelf[len(prefix):shelf.index('\n')]
63
49
 
64
 
    def unshelve(self):
 
50
    def __show_status(self, source):
 
51
        if source.can_live_update():
 
52
            diff_stat = str(DiffStat(source.readlines()))
 
53
            if len(diff_stat) > 0:
 
54
                print >> sys.stderr, 'Diff status is now:\n', diff_stat
 
55
            else:
 
56
                print >> sys.stderr, 'No changes left in working tree.'
 
57
 
 
58
    def unshelve(self, patch_source, pick_hunks=False):
65
59
        shelf = self.last_shelf()
66
60
 
67
61
        if shelf is None:
68
 
            raise Exception("No shelf found in '%s'" % self.branch.base)
69
 
 
70
 
        patch = open(shelf, 'r').read()
 
62
            raise CommandError("No shelf found in '%s'" % self.base)
 
63
 
 
64
        patches = FilePatchSource(shelf).readpatches()
 
65
        if pick_hunks:
 
66
            to_unshelve, to_remain = UnshelveHunkSelector(patches).select()
 
67
        else:
 
68
            to_unshelve = patches
 
69
            to_remain = []
 
70
 
 
71
        if len(to_unshelve) == 0:
 
72
            raise CommandError('Nothing to unshelve')
71
73
 
72
74
        print >>sys.stderr, "Reapplying shelved patches",
73
 
        message = self.get_shelf_message(patch)
 
75
        message = self.get_shelf_message(open(shelf, 'r').read())
74
76
        if message is not None:
75
77
            print >>sys.stderr, ' "%s"' % message
76
78
        else:
77
79
            print >>sys.stderr, ""
78
 
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
79
 
        pipe.write(patch)
 
80
 
 
81
        pipe = os.popen('patch -d %s -s -p0' % self.base, 'w')
 
82
        for patch in to_unshelve:
 
83
            pipe.write(str(patch))
80
84
        pipe.flush()
81
85
 
82
86
        if pipe.close() is not None:
83
 
            raise Exception("Failed running patch!")
84
 
 
85
 
        os.remove(shelf)
86
 
 
87
 
        diff_stat = DiffStat(self.get_patches(None, None))
88
 
        print 'Diff status is now:\n', diff_stat
89
 
 
90
 
        return True
91
 
 
92
 
    def get_patches(self, revision, file_list):
93
 
        from StringIO import StringIO
94
 
        from bzrlib.diff import show_diff
95
 
        out = StringIO()
96
 
        show_diff(self.branch, revision, specific_files=file_list, output=out)
97
 
        out.seek(0)
98
 
        return out.readlines()
99
 
 
100
 
    def shelve(self, all_hunks=False, message=None, revision=None,
101
 
             file_list=None):
102
 
        patches = parse_patches(self.get_patches(revision, file_list))
103
 
 
104
 
        if not all_hunks:
105
 
            try:
106
 
                patches = HunkSelector(patches).select()
107
 
            except QuitException:
108
 
                return False
109
 
 
110
 
        if len(patches) == 0:
111
 
            print >>sys.stderr, 'Nothing to shelve'
112
 
            return True
 
87
            raise CommandError("Failed running patch!")
 
88
 
 
89
        if len(to_remain) == 0:
 
90
            os.remove(shelf)
 
91
        else:
 
92
            f = open(shelf, 'w')
 
93
            for patch in to_remain:
 
94
                f.write(str(patch))
 
95
            f.close()
 
96
 
 
97
        self.__show_status(patch_source)
 
98
 
 
99
    def shelve(self, patch_source, pick_hunks=False, message=None):
 
100
        patches = patch_source.readpatches()
 
101
 
 
102
        if pick_hunks:
 
103
            to_shelve = ShelveHunkSelector(patches).select()[0]
 
104
        else:
 
105
            to_shelve = patches
 
106
 
 
107
        if len(to_shelve) == 0:
 
108
            raise CommandError('Nothing to shelve')
113
109
 
114
110
        shelf = self.next_shelf()
115
 
        print >>sys.stderr, "Saving shelved patches to", shelf
 
111
        print >>sys.stderr, "Saving to shelf '%s', patch '%s'" % \
 
112
                (self.name, os.path.basename(shelf))
116
113
        shelf = open(shelf, 'a')
117
114
        if message is not None:
118
115
            assert '\n' not in message
119
116
            shelf.write("# shelf: %s\n" % message)
120
 
        for patch in patches:
 
117
        for patch in to_shelve:
121
118
            shelf.write(str(patch))
122
119
 
123
120
        shelf.flush()
124
121
        os.fsync(shelf.fileno())
125
122
        shelf.close()
126
123
 
127
 
        print >>sys.stderr, "Reverting shelved patches"
128
 
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
129
 
        for patch in patches:
 
124
        print >>sys.stderr, "Reverting shelved patches."
 
125
        pipe = os.popen('patch -d %s -sR -p0' % self.base, 'w')
 
126
        for patch in to_shelve:
130
127
            pipe.write(str(patch))
131
128
        pipe.flush()
132
129
 
133
130
        if pipe.close() is not None:
134
 
            raise Exception("Failed running patch!")
135
 
 
136
 
        diff_stat = DiffStat(self.get_patches(None, None))
137
 
        print 'Diff status is now:\n', diff_stat
138
 
 
139
 
        return True
140
 
 
 
131
            raise CommandError("Failed running patch!")
 
132
 
 
133
        self.__show_status(patch_source)