~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2006-02-21 04:04:27 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060221040427-70ecc1e24f2c4e80
forced ancestry graph to use utf-8 Dot output

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from patches import parse_patches
 
4
import os
 
5
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
 
13
from diffstat import DiffStat
 
14
from subprocess import Popen, PIPE
 
15
 
 
16
DEFAULT_IGNORE.append('./.bzr-shelf*')
 
17
 
 
18
class QuitException(Exception):
 
19
    pass
 
20
 
 
21
class Shelf(object):
 
22
    def __init__(self, location):
 
23
        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
 
30
 
 
31
    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
 
41
            if not os.path.exists(name):
 
42
                return name
 
43
 
 
44
    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()
 
54
 
 
55
        if len(shelvenums) == 0:
 
56
            return None
 
57
        return stem + self.shelf_suffix(shelvenums[-1])
 
58
 
 
59
    def get_shelf_message(self, shelf):
 
60
        prefix = "# shelf: "
 
61
        if not shelf.startswith(prefix):
 
62
            return None
 
63
        return shelf[len(prefix):shelf.index('\n')]
 
64
 
 
65
    def unshelve(self):
 
66
        shelf = self.last_shelf()
 
67
 
 
68
        if shelf is None:
 
69
            raise Exception("No shelf found in '%s'" % self.branch.base)
 
70
 
 
71
        patch = open(shelf, 'r').read()
 
72
 
 
73
        print >>sys.stderr, "Reapplying shelved patches",
 
74
        message = self.get_shelf_message(patch)
 
75
        if message is not None:
 
76
            print >>sys.stderr, ' "%s"' % message
 
77
        else:
 
78
            print >>sys.stderr, ""
 
79
        run_patch(self.branch.base, (patch,))
 
80
        os.remove(shelf)
 
81
 
 
82
        diff_stat = DiffStat(self.get_patches(None, None))
 
83
        print 'Diff status is now:\n', diff_stat
 
84
 
 
85
        return 1
 
86
 
 
87
    def get_patches(self, revision, file_list):
 
88
        from StringIO import StringIO
 
89
        from bzrlib.diff import show_diff
 
90
        out = StringIO()
 
91
        show_diff(self.branch, revision, specific_files=file_list, output=out)
 
92
        out.seek(0)
 
93
        return out.readlines()
 
94
 
 
95
    def shelve(self, all_hunks=False, message=None, revision=None,
 
96
             file_list=None):
 
97
        patches = parse_patches(self.get_patches(revision, file_list))
 
98
 
 
99
        if not all_hunks:
 
100
            try:
 
101
                patches = HunkSelector(patches).select()
 
102
            except QuitException:
 
103
                return False
 
104
 
 
105
        if len(patches) == 0:
 
106
            print >>sys.stderr, 'Nothing to shelve'
 
107
            return 0
 
108
 
 
109
        shelf = self.next_shelf()
 
110
        print >>sys.stderr, "Saving shelved patches to", shelf
 
111
        shelf = open(shelf, 'a')
 
112
        if message is not None:
 
113
            assert '\n' not in message
 
114
            shelf.write("# shelf: %s\n" % message)
 
115
        for patch in patches:
 
116
            shelf.write(str(patch))
 
117
 
 
118
        shelf.flush()
 
119
        os.fsync(shelf.fileno())
 
120
        shelf.close()
 
121
 
 
122
        print >>sys.stderr, "Reverting shelved patches"
 
123
        run_patch(self.branch.base, patches, reverse=True)
 
124
 
 
125
        diff_stat = DiffStat(self.get_patches(None, None))
 
126
        print 'Diff status is now:\n', diff_stat
 
127
 
 
128
        return 1
 
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