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