]> arthur.barton.de Git - bup.git/blob - lib/cmd/cat-file-cmd.py
Bypass Python 3 glibc argv problems by routing args through env
[bup.git] / lib / cmd / cat-file-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import re, stat, sys
19
20 from bup import compat, options, git, vfs
21 from bup.compat import argv_bytes
22 from bup.helpers import chunkyreader, handle_ctrl_c, log, saved_errors
23 from bup.io import byte_stream
24 from bup.repo import LocalRepo
25
26 optspec = """
27 bup cat-file [--meta|--bupm] /branch/revision/[path]
28 --
29 meta        print the target's metadata entry (decoded then reencoded) to stdout
30 bupm        print the target directory's .bupm file directly to stdout
31 """
32
33 handle_ctrl_c()
34
35 o = options.Options(optspec)
36 opt, flags, extra = o.parse(compat.argv[1:])
37
38 git.check_repo_or_die()
39
40 if not extra:
41     o.fatal('must specify a target')
42 if len(extra) > 1:
43     o.fatal('only one target file allowed')
44 if opt.bupm and opt.meta:
45     o.fatal('--meta and --bupm are incompatible')
46     
47 target = argv_bytes(extra[0])
48
49 if not re.match(br'/*[^/]+/[^/]+', target):
50     o.fatal("path %r doesn't include a branch and revision" % target)
51
52 repo = LocalRepo()
53 resolved = vfs.resolve(repo, target, follow=False)
54 leaf_name, leaf_item = resolved[-1]
55 if not leaf_item:
56     log('error: cannot access %r in %r\n'
57         % ('/'.join(name for name, item in resolved), path))
58     sys.exit(1)
59
60 mode = vfs.item_mode(leaf_item)
61
62 sys.stdout.flush()
63 out = byte_stream(sys.stdout)
64
65 if opt.bupm:
66     if not stat.S_ISDIR(mode):
67         o.fatal('%r is not a directory' % target)
68     _, bupm_oid = vfs.tree_data_and_bupm(repo, leaf_item.oid)
69     if bupm_oid:
70         with vfs.tree_data_reader(repo, bupm_oid) as meta_stream:
71             out.write(meta_stream.read())
72 elif opt.meta:
73     augmented = vfs.augment_item_meta(repo, leaf_item, include_size=True)
74     out.write(augmented.meta.encode())
75 else:
76     if stat.S_ISREG(mode):
77         with vfs.fopen(repo, leaf_item) as f:
78             for b in chunkyreader(f):
79                 out.write(b)
80     else:
81         o.fatal('%r is not a plain file' % target)
82
83 if saved_errors:
84     log('warning: %d errors encountered\n' % len(saved_errors))
85     sys.exit(1)