~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2005-11-10 21:41:44 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051110214144-834e918cd2a88101
Handled status code again

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
from bzrlib.branch import Branch
11
11
from bzrlib import DEFAULT_IGNORE
12
12
from hunk_selector import HunkSelector
 
13
from diffstat import DiffStat
 
14
from subprocess import Popen, PIPE
13
15
 
14
16
DEFAULT_IGNORE.append('./.bzr-shelf*')
15
17
 
16
 
def run_bzr(args):
17
 
    if type(args) is str:
18
 
        args = [ args ]
19
 
    pipe = os.popen('bzr %s' % string.join(args, ' '), 'r')
20
 
    lines = pipe.readlines()
21
 
    if pipe.close() is not None:
22
 
        raise Exception("Failed running bzr")
23
 
    return lines
24
 
 
25
18
class QuitException(Exception):
26
19
    pass
27
20
 
28
21
class Shelf(object):
29
 
    def __init__(self):
30
 
        b = Branch.open_containing('.')[0]
31
 
        self.bzr_root = b.base
 
22
    def __init__(self, location):
 
23
        self.branch = Branch.open_containing(location)[0]
32
24
 
33
25
    def shelf_suffix(self, index):
34
26
        if index == 0:
43
35
                yield self.shelf_suffix(i)
44
36
                i = i + 1
45
37
 
46
 
        stem = os.path.join(self.bzr_root, '.bzr-shelf')
 
38
        stem = os.path.join(self.branch.base, '.bzr-shelf')
47
39
        for end in name_sequence():
48
40
            name = stem + end
49
41
            if not os.path.exists(name):
50
42
                return name
51
43
 
52
44
    def last_shelf(self):
53
 
        stem = os.path.join(self.bzr_root, '.bzr-shelf')
 
45
        stem = os.path.join(self.branch.base, '.bzr-shelf')
54
46
        shelves = glob.glob(stem)
55
47
        shelves.extend(glob.glob(stem + '-*'))
56
48
        def shelf_index(name):
74
66
        shelf = self.last_shelf()
75
67
 
76
68
        if shelf is None:
77
 
            raise Exception("No shelf found in '%s'" % self.bzr_root)
 
69
            raise Exception("No shelf found in '%s'" % self.branch.base)
78
70
 
79
71
        patch = open(shelf, 'r').read()
80
72
 
84
76
            print >>sys.stderr, ' "%s"' % message
85
77
        else:
86
78
            print >>sys.stderr, ""
87
 
        pipe = os.popen('patch -d %s -s -p0' % self.bzr_root, 'w')
88
 
        pipe.write(patch)
89
 
        pipe.flush()
90
 
 
91
 
        if pipe.close() is not None:
92
 
            raise Exception("Failed running patch!")
93
 
 
 
79
        run_patch(self.branch.base, (patch,))
94
80
        os.remove(shelf)
95
 
        print 'Diff status is now:'
96
 
        os.system('bzr diff | diffstat')
97
 
 
98
 
        return True
 
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()
99
94
 
100
95
    def shelve(self, all_hunks=False, message=None, revision=None,
101
96
             file_list=None):
102
 
        cmd = ['diff']
103
 
        if revision is not None:
104
 
            cmd.extend(['--revision', str(revision[0])])
105
 
        if file_list is not None:
106
 
            cmd.extend(file_list)
107
 
        patches = parse_patches(run_bzr(cmd))
 
97
        patches = parse_patches(self.get_patches(revision, file_list))
 
98
 
108
99
        if not all_hunks:
109
100
            try:
110
101
                patches = HunkSelector(patches).select()
113
104
 
114
105
        if len(patches) == 0:
115
106
            print >>sys.stderr, 'Nothing to shelve'
116
 
            return True
 
107
            return 0
117
108
 
118
109
        shelf = self.next_shelf()
119
110
        print >>sys.stderr, "Saving shelved patches to", shelf
129
120
        shelf.close()
130
121
 
131
122
        print >>sys.stderr, "Reverting shelved patches"
132
 
        pipe = os.popen('patch -d %s -sR -p0' % self.bzr_root, 'w')
133
 
        for patch in patches:
134
 
            pipe.write(str(patch))
135
 
        pipe.flush()
136
 
 
137
 
        if pipe.close() is not None:
138
 
            raise Exception("Failed running patch!")
139
 
 
140
 
        print 'Diff status is now:'
141
 
        os.system('bzr diff | diffstat')
142
 
 
143
 
        return True
144
 
 
 
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