~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2005-11-11 17:43:12 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051111174312-1c627d82a07bf8fd
Added patch for tab-in-patch-filename support

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 ShelveHunkSelector, UnshelveHunkSelector
 
12
from hunk_selector import HunkSelector
13
13
from diffstat import DiffStat
 
14
from subprocess import Popen, PIPE
14
15
 
15
16
DEFAULT_IGNORE.append('./.bzr-shelf*')
16
17
 
18
19
    pass
19
20
 
20
21
class Shelf(object):
21
 
    def __init__(self, location, name='default'):
 
22
    def __init__(self, location):
22
23
        self.branch = Branch.open_containing(location)[0]
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)
 
24
 
 
25
    def shelf_suffix(self, index):
 
26
        if index == 0:
 
27
            return ""
 
28
        else:
 
29
            return "-%d" % index
34
30
 
35
31
    def next_shelf(self):
36
 
        index = 0
37
 
        while True:
38
 
            name = self.__path(index)
 
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
39
41
            if not os.path.exists(name):
40
42
                return name
41
 
            index += 1
42
43
 
43
44
    def last_shelf(self):
44
 
        shelves = os.listdir(self.shelf_dir)
45
 
        indexes = [int(f) for f in shelves]
46
 
        indexes.sort()
 
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()
47
54
 
48
 
        if len(indexes) == 0:
 
55
        if len(shelvenums) == 0:
49
56
            return None
50
 
 
51
 
        return self.__path(indexes[-1])
 
57
        return stem + self.shelf_suffix(shelvenums[-1])
52
58
 
53
59
    def get_shelf_message(self, shelf):
54
60
        prefix = "# shelf: "
56
62
            return None
57
63
        return shelf[len(prefix):shelf.index('\n')]
58
64
 
59
 
    def unshelve(self, pick_hunks=False):
 
65
    def unshelve(self):
60
66
        shelf = self.last_shelf()
61
67
 
62
68
        if shelf is None:
63
69
            raise Exception("No shelf found in '%s'" % self.branch.base)
64
70
 
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
 
71
        patch = open(shelf, 'r').read()
75
72
 
76
73
        print >>sys.stderr, "Reapplying shelved patches",
77
 
        message = self.get_shelf_message(open(shelf, 'r').read())
 
74
        message = self.get_shelf_message(patch)
78
75
        if message is not None:
79
76
            print >>sys.stderr, ' "%s"' % message
80
77
        else:
81
78
            print >>sys.stderr, ""
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
 
 
 
79
        run_patch(self.branch.base, (patch,))
90
80
        os.remove(shelf)
91
81
 
92
82
        diff_stat = DiffStat(self.get_patches(None, None))
93
83
        print 'Diff status is now:\n', diff_stat
94
84
 
95
 
        return True
 
85
        return 1
96
86
 
97
87
    def get_patches(self, revision, file_list):
98
88
        from StringIO import StringIO
102
92
        out.seek(0)
103
93
        return out.readlines()
104
94
 
105
 
    def shelve(self, pick_hunks=False, message=None, revision=None,
 
95
    def shelve(self, all_hunks=False, message=None, revision=None,
106
96
             file_list=None):
107
97
        patches = parse_patches(self.get_patches(revision, file_list))
108
98
 
109
 
        if pick_hunks:
 
99
        if not all_hunks:
110
100
            try:
111
 
                patches = ShelveHunkSelector(patches).select()
 
101
                patches = HunkSelector(patches).select()
112
102
            except QuitException:
113
103
                return False
114
104
 
115
105
        if len(patches) == 0:
116
106
            print >>sys.stderr, 'Nothing to shelve'
117
 
            return True
 
107
            return 0
118
108
 
119
109
        shelf = self.next_shelf()
120
110
        print >>sys.stderr, "Saving shelved patches to", shelf
130
120
        shelf.close()
131
121
 
132
122
        print >>sys.stderr, "Reverting shelved patches"
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!")
 
123
        run_patch(self.branch.base, patches, reverse=True)
140
124
 
141
125
        diff_stat = DiffStat(self.get_patches(None, None))
142
126
        print 'Diff status is now:\n', diff_stat
143
127
 
144
 
        return True
 
128
        return 1
145
129
 
 
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