~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Aaron Bentley
  • Date: 2005-10-27 04:45:06 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20051027044506-45f616c07537a1da
Got the shell basics working properly

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
14
 
15
15
DEFAULT_IGNORE.append('./.bzr-shelf*')
18
18
    pass
19
19
 
20
20
class Shelf(object):
21
 
    def __init__(self, location, name='default'):
 
21
    def __init__(self, location):
22
22
        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)
 
23
 
 
24
    def shelf_suffix(self, index):
 
25
        if index == 0:
 
26
            return ""
 
27
        else:
 
28
            return "-%d" % index
34
29
 
35
30
    def next_shelf(self):
36
 
        index = 0
37
 
        while True:
38
 
            name = self.__path(index)
 
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
39
40
            if not os.path.exists(name):
40
41
                return name
41
 
            index += 1
42
42
 
43
43
    def last_shelf(self):
44
 
        shelves = os.listdir(self.shelf_dir)
45
 
        indexes = [int(f) for f in shelves]
46
 
        indexes.sort()
 
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()
47
53
 
48
 
        if len(indexes) == 0:
 
54
        if len(shelvenums) == 0:
49
55
            return None
50
 
 
51
 
        return self.__path(indexes[-1])
 
56
        return stem + self.shelf_suffix(shelvenums[-1])
52
57
 
53
58
    def get_shelf_message(self, shelf):
54
59
        prefix = "# shelf: "
56
61
            return None
57
62
        return shelf[len(prefix):shelf.index('\n')]
58
63
 
59
 
    def unshelve(self, pick_hunks=False):
 
64
    def unshelve(self):
60
65
        shelf = self.last_shelf()
61
66
 
62
67
        if shelf is None:
63
68
            raise Exception("No shelf found in '%s'" % self.branch.base)
64
69
 
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
 
70
        patch = open(shelf, 'r').read()
75
71
 
76
72
        print >>sys.stderr, "Reapplying shelved patches",
77
 
        message = self.get_shelf_message(open(shelf, 'r').read())
 
73
        message = self.get_shelf_message(patch)
78
74
        if message is not None:
79
75
            print >>sys.stderr, ' "%s"' % message
80
76
        else:
81
77
            print >>sys.stderr, ""
82
78
        pipe = os.popen('patch -d %s -s -p0' % self.branch.base, 'w')
83
 
        for patch in patches:
84
 
            pipe.write(str(patch))
 
79
        pipe.write(patch)
85
80
        pipe.flush()
86
81
 
87
82
        if pipe.close() is not None:
102
97
        out.seek(0)
103
98
        return out.readlines()
104
99
 
105
 
    def shelve(self, pick_hunks=False, message=None, revision=None,
 
100
    def shelve(self, all_hunks=False, message=None, revision=None,
106
101
             file_list=None):
107
102
        patches = parse_patches(self.get_patches(revision, file_list))
108
103
 
109
 
        if pick_hunks:
 
104
        if not all_hunks:
110
105
            try:
111
 
                patches = ShelveHunkSelector(patches).select()
 
106
                patches = HunkSelector(patches).select()
112
107
            except QuitException:
113
108
                return False
114
109