-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat-file-cmd.py
executable file
·69 lines (56 loc) · 1.79 KB
/
cat-file-cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
"""": # -*-python-*-
bup_python="$(dirname "$0")/bup-python" || exit $?
exec "$bup_python" "$0" ${1+"$@"}
"""
# end of bup preamble
import re, stat, sys
from bup import options, git, vfs
from bup.helpers import chunkyreader, handle_ctrl_c, log, saved_errors
optspec = """
bup cat-file [--meta|--bupm] /branch/revision/[path]
--
meta print the target's metadata entry (decoded then reencoded) to stdout
bupm print the target directory's .bupm file directly to stdout
"""
handle_ctrl_c()
o = options.Options(optspec)
(opt, flags, extra) = o.parse(sys.argv[1:])
git.check_repo_or_die()
top = vfs.RefList(None)
if not extra:
o.fatal('must specify a target')
if len(extra) > 1:
o.fatal('only one target file allowed')
if opt.bupm and opt.meta:
o.fatal('--meta and --bupm are incompatible')
target = extra[0]
if not re.match(r'/*[^/]+/[^/]+', target):
o.fatal("path %r doesn't include a branch and revision" % target)
try:
n = top.lresolve(target)
except vfs.NodeError as e:
o.fatal(e)
if isinstance(n, vfs.FakeSymlink):
# Source is actually /foo/what, i.e. a top-level commit
# like /foo/latest, which is a symlink to ../.commit/SHA.
# So dereference it.
target = n.dereference()
if opt.bupm:
if not stat.S_ISDIR(n.mode):
o.fatal('%r is not a directory' % target)
mfile = n.metadata_file() # VFS file -- cannot close().
if mfile:
meta_stream = mfile.open()
sys.stdout.write(meta_stream.read())
elif opt.meta:
sys.stdout.write(n.metadata().encode())
else:
if stat.S_ISREG(n.mode):
for b in chunkyreader(n.open()):
sys.stdout.write(b)
else:
o.fatal('%r is not a plain file' % target)
if saved_errors:
log('warning: %d errors encountered\n' % len(saved_errors))
sys.exit(1)