~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-03-10 20:58:35 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060310205835-08dc7d4cedbc64c1
Updated docs

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
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: "
70
76
            print >>sys.stderr, ' "%s"' % message
71
77
        else:
72
78
            print >>sys.stderr, ""
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
 
 
 
79
        run_patch(self.branch.base, (patch,))
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 True
 
85
        return 1
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, pick_hunks=False, message=None, revision=None,
 
95
    def shelve(self, all_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 pick_hunks:
 
99
        if not all_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 True
 
107
            return 0
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
 
        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!")
 
123
        run_patch(self.branch.base, patches, reverse=True)
130
124
 
131
125
        diff_stat = DiffStat(self.get_patches(None, None))
132
126
        print 'Diff status is now:\n', diff_stat
133
127
 
134
 
        return True
 
128
        return 1
135
129
 
 
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