~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 01:41:52 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129014152-f5ede8888bcebc48
HunkSelector was broken if you did a "done" followed by "status/invert" etc.
Fixup to make pychecker happy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
from bzrlib.commands import Command
10
10
from bzrlib.branch import Branch
11
11
from bzrlib import DEFAULT_IGNORE
12
 
from hunk_selector import HunkSelector
 
12
from hunk_selector import ShelveHunkSelector, UnshelveHunkSelector
13
13
from diffstat import DiffStat
14
 
from subprocess import Popen, PIPE
15
14
 
16
15
DEFAULT_IGNORE.append('./.bzr-shelf*')
17
16
 
19
18
    pass
20
19
 
21
20
class Shelf(object):
22
 
    def __init__(self, location):
 
21
    def __init__(self, location, name='default'):
23
22
        self.branch = Branch.open_containing(location)[0]
24
 
 
25
 
    def shelf_suffix(self, index):
26
 
        if index == 0:
27
 
            return ""
28
 
        else:
29
 
            return "-%d" % index
 
23
        base = self.branch.controlfilename('x-shelf')
 
24
        self.shelf_dir = os.path.join(base, name)
 
25
 
 
26
        # FIXME surely there's an easier way to do this?
 
27
        t = self.branch._transport
 
28
        for dir in [base, self.shelf_dir]:
 
29
            if not t.has(dir):
 
30
                t.mkdir(dir)
 
31
 
 
32
    def __path(self, idx):
 
33
        return os.path.join(self.shelf_dir, '%.2d' % idx)
30
34
 
31
35
    def next_shelf(self):
32
 
        def name_sequence():
33
 
            i = 0
34
 
            while True:
35
 
                yield self.shelf_suffix(i)
36
 
                i = i + 1
37
 
 
38
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
39
 
        for end in name_sequence():
40
 
            name = stem + end
 
36
        index = 0
 
37
        while True:
 
38
            name = self.__path(index)
41
39
            if not os.path.exists(name):
42
40
                return name
 
41
            index += 1
43
42
 
44
43
    def last_shelf(self):
45
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
46
 
        shelves = glob.glob(stem)
47
 
        shelves.extend(glob.glob(stem + '-*'))
48
 
        def shelf_index(name):
49
 
            if name == stem:
50
 
                return 0
51
 
            return int(name[len(stem)+1:])
52
 
        shelvenums = [shelf_index(f) for f in shelves]
53
 
        shelvenums.sort()
 
44
        shelves = os.listdir(self.shelf_dir)
 
45
        indexes = [int(f) for f in shelves]
 
46
        indexes.sort()
54
47
 
55
 
        if len(shelvenums) == 0:
 
48
        if len(indexes) == 0:
56
49
            return None
57
 
        return stem + self.shelf_suffix(shelvenums[-1])
 
50
 
 
51
        return self.__path(indexes[-1])
58
52
 
59
53
    def get_shelf_message(self, shelf):
60
54
        prefix = "# shelf: "
62
56
            return None
63
57
        return shelf[len(prefix):shelf.index('\n')]
64
58
 
65
 
    def unshelve(self):
 
59
    def unshelve(self, pick_hunks=False):
66
60
        shelf = self.last_shelf()
67
61
 
68
62
        if shelf is None:
69
63
            raise Exception("No shelf found in '%s'" % self.branch.base)
70
64
 
71
 
        patch = open(shelf, 'r').read()
 
65
        patches = parse_patches(open(shelf, 'r').readlines())
 
66
        if pick_hunks:
 
67
            try:
 
68
                patches = UnshelveHunkSelector(patches).select()
 
69
            except QuitException:
 
70
                return False
 
71
 
 
72
        if len(patches) == 0:
 
73
            print >>sys.stderr, 'Nothing to unshelve'
 
74
            return True
72
75
 
73
76
        print >>sys.stderr, "Reapplying shelved patches",
74
 
        message = self.get_shelf_message(patch)
 
77
        message = self.get_shelf_message(open(shelf, 'r').read())
75
78
        if message is not None:
76
79
            print >>sys.stderr, ' "%s"' % message
77
80
        else:
78
81
            print >>sys.stderr, ""
79
 
        run_patch(self.branch.base, (patch,))
 
82
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
 
83
        for patch in patches:
 
84
            pipe.write(str(patch))
 
85
        pipe.flush()
 
86
 
 
87
        if pipe.close() is not None:
 
88
            raise Exception("Failed running patch!")
 
89
 
80
90
        os.remove(shelf)
81
91
 
82
92
        diff_stat = DiffStat(self.get_patches(None, None))
83
93
        print 'Diff status is now:\n', diff_stat
84
94
 
85
 
        return 1
 
95
        return True
86
96
 
87
97
    def get_patches(self, revision, file_list):
88
98
        from StringIO import StringIO
92
102
        out.seek(0)
93
103
        return out.readlines()
94
104
 
95
 
    def shelve(self, all_hunks=False, message=None, revision=None,
 
105
    def shelve(self, pick_hunks=False, message=None, revision=None,
96
106
             file_list=None):
97
107
        patches = parse_patches(self.get_patches(revision, file_list))
98
108
 
99
 
        if not all_hunks:
 
109
        if pick_hunks:
100
110
            try:
101
 
                patches = HunkSelector(patches).select()
 
111
                patches = ShelveHunkSelector(patches).select()
102
112
            except QuitException:
103
113
                return False
104
114
 
105
115
        if len(patches) == 0:
106
116
            print >>sys.stderr, 'Nothing to shelve'
107
 
            return 0
 
117
            return True
108
118
 
109
119
        shelf = self.next_shelf()
110
120
        print >>sys.stderr, "Saving shelved patches to", shelf
120
130
        shelf.close()
121
131
 
122
132
        print >>sys.stderr, "Reverting shelved patches"
123
 
        run_patch(self.branch.base, patches, reverse=True)
 
133
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
 
134
        for patch in patches:
 
135
            pipe.write(str(patch))
 
136
        pipe.flush()
 
137
 
 
138
        if pipe.close() is not None:
 
139
            raise Exception("Failed running patch!")
124
140
 
125
141
        diff_stat = DiffStat(self.get_patches(None, None))
126
142
        print 'Diff status is now:\n', diff_stat
127
143
 
128
 
        return 1
 
144
        return True
129
145
 
130
 
def run_patch(branch_base, patches, reverse=False):
131
 
    args = ['patch', '-d', branch_base, '-s', '-p0', '-f']
132
 
    if reverse:
133
 
        args.append('-R')
134
 
    process = Popen(args, stdin=PIPE)
135
 
    for patch in patches:
136
 
        process.stdin.write(str(patch))
137
 
    process.stdin.close()
138
 
    result = process.wait()
139
 
    if result not in (0, 1):
140
 
        raise Exception("Error applying patches")
141
 
    return result