]> arthur.barton.de Git - bup.git/blob - cmd-ls.py
cmd-ls and cmd-fuse: toys for browsing your available backups.
[bup.git] / cmd-ls.py
1 #!/usr/bin/env python
2 import sys, os, re, time
3 import options, git
4
5
6 def namesplit(path):
7     path = re.sub(r'/+', '/', path)
8     while 1:
9         p2 = re.sub(r'/[^/]+/\.\.(/|$)', '/', path)  # handle ../ notation
10         if p2 == path: break
11         path = p2
12     l = path.split('/', 3)
13     ref = None
14     date = None
15     dir = None
16     assert(l[0] == '')
17     if len(l) > 1:
18         ref = l[1] or None
19     if len(l) > 2:
20         date = l[2]
21     if len(l) > 3:
22         dir = l[3]
23     return (ref, date, dir)
24
25
26 optspec = """
27 bup ls <dirs...>
28 --
29 s,hash   show hash for each file
30 """
31 o = options.Options('bup ls', optspec)
32 (opt, flags, extra) = o.parse(sys.argv[1:])
33
34 git.check_repo_or_die()
35
36 if not extra:
37     extra = ['/']
38
39 for d in extra:
40     (ref, date, path) = namesplit(d)
41     if not ref:
42         for (name,sha) in git.list_refs():
43             name = re.sub('^refs/heads/', '', name)
44             if opt.hash:
45                 print '%s %s' % (sha.encode('hex'), name)
46             else:
47                 print name
48     elif not date:
49         dates = list(git.rev_list(ref))
50         dates.sort()
51         for (date,commit) in dates:
52             l = time.localtime(date)
53             print repr((time.strftime('%Y-%m-%d-%H%M%S', l),commit))
54     else:
55         dates = list(git.rev_list(ref))
56         dates.sort(reverse=True)
57         try:
58             dp = time.strptime(date, '%Y-%m-%d-%H%M%S')
59         except ValueError:
60             dp = time.strptime(date, '%Y-%m-%d')
61         dt = time.mktime(dp)
62         commit = None
63         for (d,commit) in dates:
64             if d <= dt: break
65         assert(commit)
66         it = cp.get('%s:%s' % (commit.encode('hex'), path or ''))
67         type = it.next()
68         if type == 'tree':
69             for (mode,name,sha) in git._treeparse(''.join(it)):
70                 if opt.hash:
71                     print '%s %s' % (sha.encode('hex'), name)
72                 else:
73                     print name
74         else:
75             (dir,name) = os.path.split(path)
76             if opt.hash:
77                 print '%s %s' % ('?', name)  # FIXME
78             else:
79                 print name