~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2005-05-26 14:20:29 UTC
  • Revision ID: abentley@troll-20050526142029-e919772712205486
Added bug report

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
 
 
15
 
DEFAULT_IGNORE.append('./.bzr-shelf*')
16
 
 
17
 
class QuitException(Exception):
18
 
    pass
19
 
 
20
 
class Shelf(object):
21
 
    def __init__(self, location):
22
 
        self.branch = Branch.open_containing(location)[0]
23
 
 
24
 
    def shelf_suffix(self, index):
25
 
        if index == 0:
26
 
            return ""
27
 
        else:
28
 
            return "-%d" % index
29
 
 
30
 
    def next_shelf(self):
31
 
        def name_sequence():
32
 
            i = 0
33
 
            while True:
34
 
                yield self.shelf_suffix(i)
35
 
                i = i + 1
36
 
 
37
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
38
 
        for end in name_sequence():
39
 
            name = stem + end
40
 
            if not os.path.exists(name):
41
 
                return name
42
 
 
43
 
    def last_shelf(self):
44
 
        stem = os.path.join(self.branch.base, '.bzr-shelf')
45
 
        shelves = glob.glob(stem)
46
 
        shelves.extend(glob.glob(stem + '-*'))
47
 
        def shelf_index(name):
48
 
            if name == stem:
49
 
                return 0
50
 
            return int(name[len(stem)+1:])
51
 
        shelvenums = [shelf_index(f) for f in shelves]
52
 
        shelvenums.sort()
53
 
 
54
 
        if len(shelvenums) == 0:
55
 
            return None
56
 
        return stem + self.shelf_suffix(shelvenums[-1])
57
 
 
58
 
    def get_shelf_message(self, shelf):
59
 
        prefix = "# shelf: "
60
 
        if not shelf.startswith(prefix):
61
 
            return None
62
 
        return shelf[len(prefix):shelf.index('\n')]
63
 
 
64
 
    def unshelve(self):
65
 
        shelf = self.last_shelf()
66
 
 
67
 
        if shelf is None:
68
 
            raise Exception("No shelf found in '%s'" % self.branch.base)
69
 
 
70
 
        patch = open(shelf, 'r').read()
71
 
 
72
 
        print >>sys.stderr, "Reapplying shelved patches",
73
 
        message = self.get_shelf_message(patch)
74
 
        if message is not None:
75
 
            print >>sys.stderr, ' "%s"' % message
76
 
        else:
77
 
            print >>sys.stderr, ""
78
 
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
79
 
        pipe.write(patch)
80
 
        pipe.flush()
81
 
 
82
 
        if pipe.close() is not None:
83
 
            raise Exception("Failed running patch!")
84
 
 
85
 
        os.remove(shelf)
86
 
 
87
 
        diff_stat = DiffStat(self.get_patches(None, None))
88
 
        print 'Diff status is now:\n', diff_stat
89
 
 
90
 
        return True
91
 
 
92
 
    def get_patches(self, revision, file_list):
93
 
        from StringIO import StringIO
94
 
        from bzrlib.diff import show_diff
95
 
        out = StringIO()
96
 
        show_diff(self.branch, revision, specific_files=file_list, output=out)
97
 
        out.seek(0)
98
 
        return out.readlines()
99
 
 
100
 
    def shelve(self, all_hunks=False, message=None, revision=None,
101
 
             file_list=None):
102
 
        patches = parse_patches(self.get_patches(revision, file_list))
103
 
 
104
 
        if not all_hunks:
105
 
            try:
106
 
                patches = HunkSelector(patches).select()
107
 
            except QuitException:
108
 
                return False
109
 
 
110
 
        if len(patches) == 0:
111
 
            print >>sys.stderr, 'Nothing to shelve'
112
 
            return True
113
 
 
114
 
        shelf = self.next_shelf()
115
 
        print >>sys.stderr, "Saving shelved patches to", shelf
116
 
        shelf = open(shelf, 'a')
117
 
        if message is not None:
118
 
            assert '\n' not in message
119
 
            shelf.write("# shelf: %s\n" % message)
120
 
        for patch in patches:
121
 
            shelf.write(str(patch))
122
 
 
123
 
        shelf.flush()
124
 
        os.fsync(shelf.fileno())
125
 
        shelf.close()
126
 
 
127
 
        print >>sys.stderr, "Reverting shelved patches"
128
 
        pipe = os.popen('patch -d %s -sR -p0' % self.branch.base, 'w')
129
 
        for patch in patches:
130
 
            pipe.write(str(patch))
131
 
        pipe.flush()
132
 
 
133
 
        if pipe.close() is not None:
134
 
            raise Exception("Failed running patch!")
135
 
 
136
 
        diff_stat = DiffStat(self.get_patches(None, None))
137
 
        print 'Diff status is now:\n', diff_stat
138
 
 
139
 
        return True
140