~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2005-11-28 08:59:54 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-20051128085954-ce1fa812eeeab31d
Restart after a done was broken, and we weren't resetting the print logic at
the end of the loop either, so we wouldn't print the hunk after a 'dr'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from bzrlib import DEFAULT_IGNORE
12
12
from hunk_selector import HunkSelector
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: "
76
70
            print >>sys.stderr, ' "%s"' % message
77
71
        else:
78
72
            print >>sys.stderr, ""
79
 
        run_patch(self.branch.base, (patch,))
 
73
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
 
74
        pipe.write(patch)
 
75
        pipe.flush()
 
76
 
 
77
        if pipe.close() is not None:
 
78
            raise Exception("Failed running patch!")
 
79
 
80
80
        os.remove(shelf)
81
81
 
82
82
        diff_stat = DiffStat(self.get_patches(None, None))
83
83
        print 'Diff status is now:\n', diff_stat
84
84
 
85
 
        return 1
 
85
        return True
86
86
 
87
87
    def get_patches(self, revision, file_list):
88
88
        from StringIO import StringIO
92
92
        out.seek(0)
93
93
        return out.readlines()
94
94
 
95
 
    def shelve(self, all_hunks=False, message=None, revision=None,
 
95
    def shelve(self, pick_hunks=False, message=None, revision=None,
96
96
             file_list=None):
97
97
        patches = parse_patches(self.get_patches(revision, file_list))
98
98
 
99
 
        if not all_hunks:
 
99
        if pick_hunks:
100
100
            try:
101
101
                patches = HunkSelector(patches).select()
102
102
            except QuitException:
104
104
 
105
105
        if len(patches) == 0:
106
106
            print >>sys.stderr, 'Nothing to shelve'
107
 
            return 0
 
107
            return True
108
108
 
109
109
        shelf = self.next_shelf()
110
110
        print >>sys.stderr, "Saving shelved patches to", shelf
120
120
        shelf.close()
121
121
 
122
122
        print >>sys.stderr, "Reverting shelved patches"
123
 
        run_patch(self.branch.base, patches, reverse=True)
 
123
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
 
124
        for patch in patches:
 
125
            pipe.write(str(patch))
 
126
        pipe.flush()
 
127
 
 
128
        if pipe.close() is not None:
 
129
            raise Exception("Failed running patch!")
124
130
 
125
131
        diff_stat = DiffStat(self.get_patches(None, None))
126
132
        print 'Diff status is now:\n', diff_stat
127
133
 
128
 
        return 1
 
134
        return True
129
135
 
130
 
def run_patch(branch_base, patches, reverse=False):
131
 
    args = ['patch', '-d', branch_base, '-s', '-p1', '-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