~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 01:41:15 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129014115-eff5eb80bc51e0f5
Make HunkSelector agnostic as to whether it's selecting for shelving or
unshelving. Create ShelveHunkSelector() and UnshelveHunkSelector() to
encapsulate this, a bit ugly, but reasonable.

Rename callbacks to be private, ie. beginning with _.

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