~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shelf.py

  • Committer: Michael Ellerman
  • Date: 2005-11-28 06:25:38 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-20051128062538-a641c1279063c5fc
New shelf layout. Shelves now sit under .bzr/x-shelf/default/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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: "