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