]> arthur.barton.de Git - bup.git/blob - cmd-ls.py
cmd-join: don't restart git cat-file so frequently.
[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 cp = git.CatPipe()
36
37 if not extra:
38     extra = ['/']
39
40 for d in extra:
41     (ref, date, path) = namesplit(d)
42     if not ref:
43         for (name,sha) in git.list_refs():
44             name = re.sub('^refs/heads/', '', name)
45             if opt.hash:
46                 print '%s %s' % (sha.encode('hex'), name)
47             else:
48                 print name
49     elif not date:
50         dates = list(git.rev_list(ref))
51         dates.sort()
52         for (date,commit) in dates:
53             l = time.localtime(date)
54             print repr((time.strftime('%Y-%m-%d-%H%M%S', l),commit))
55     else:
56         dates = list(git.rev_list(ref))
57         dates.sort(reverse=True)
58         try:
59             dp = time.strptime(date, '%Y-%m-%d-%H%M%S')
60         except ValueError:
61             dp = time.strptime(date, '%Y-%m-%d')
62         dt = time.mktime(dp)
63         commit = None
64         for (d,commit) in dates:
65             if d <= dt: break
66         assert(commit)
67         it = cp.get('%s:%s' % (commit.encode('hex'), path or ''))
68         type = it.next()
69         if type == 'tree':
70             for (mode,name,sha) in git._treeparse(''.join(it)):
71                 if opt.hash:
72                     print '%s %s' % (sha.encode('hex'), name)
73                 else:
74                     print name
75         else:
76             (dir,name) = os.path.split(path)
77             if opt.hash:
78                 print '%s %s' % ('?', name)  # FIXME
79             else:
80                 print name