2 from __future__ import absolute_import, print_function
3 from binascii import hexlify, unhexlify
4 from collections import namedtuple
5 from stat import S_ISDIR
6 import os, sys, textwrap, time
8 from bup import compat, git, client, vfs
9 from bup.compat import (
15 from bup.git import get_cat_data, parse_commit, walk_object
16 from bup.helpers import add_error, debug1, log, saved_errors
17 from bup.helpers import hostname, tty_width, parse_num
18 from bup.io import path_msg
19 from bup.pwdgrp import userfullname, username
20 from bup.repo import LocalRepo, RemoteRepo
23 "usage: bup get [-s source] [-r remote] (<--ff|--append|...> REF [DEST])...",
25 """Transfer data from a source repository to a destination repository
26 according to the methods specified (--ff, --ff:, --append, etc.).
27 Both repositories default to BUP_DIR. A remote destination may be
28 specified with -r, and data may be pulled from a remote repository
29 with the related "bup on HOST get ..." command.""",
31 ('optional arguments:',
32 (('-h, --help', 'show this help message and exit'),
34 'increase log output (can be specified more than once)'),
35 ('-q, --quiet', "don't show progress meter"),
36 ('-s SOURCE, --source SOURCE',
37 'path to the source repository (defaults to BUP_DIR)'),
38 ('-r REMOTE, --remote REMOTE',
39 'hostname:/path/to/repo of remote destination repository'),
40 ('-t --print-trees', 'output a tree id for each ref set'),
41 ('-c, --print-commits', 'output a commit id for each ref set'),
42 ('--print-tags', 'output an id for each tag'),
43 ('--bwlimit BWLIMIT', 'maximum bytes/sec to transmit to server'),
44 ('-0, -1, -2, -3, -4, -5, -6, -7, -8, -9, --compress LEVEL',
45 'set compression LEVEL (default: 1)'))),
48 (('--ff REF, --ff: REF DEST',
49 'fast-forward dest REF (or DEST) to match source REF'),
50 ('--append REF, --append: REF DEST',
51 'append REF (treeish or committish) to dest REF (or DEST)'),
52 ('--pick REF, --pick: REF DEST',
53 'append single source REF commit to dest REF (or DEST)'),
54 ('--force-pick REF, --force-pick: REF DEST',
55 '--pick, overwriting REF (or DEST)'),
56 ('--new-tag REF, --new-tag: REF DEST',
57 'tag source ref REF as REF (or DEST) in dest unless it already exists'),
58 ('--replace, --replace: REF DEST',
59 'overwrite REF (or DEST) in dest with source REF'),
61 'fetch REF anonymously (without destination ref)'))))
63 def render_opts(opts, width=None):
67 for args, desc in opts:
68 result.append(textwrap.fill(args, width=width,
69 initial_indent=(' ' * 2),
70 subsequent_indent=(' ' * 4)))
72 result.append(textwrap.fill(desc, width=width,
73 initial_indent=(' ' * 6),
74 subsequent_indent=(' ' * 6)))
78 def usage(argspec, width=None):
81 usage, preamble, groups = argspec[0], argspec[1], argspec[2:]
83 msg.append(textwrap.fill(usage, width=width, subsequent_indent=' '))
85 msg.append(textwrap.fill(preamble.replace('\n', ' '), width=width))
87 for group_name, group_args in groups:
88 msg.extend(['\n', group_name, '\n'])
89 msg.extend(render_opts(group_args, width=width))
92 def misuse(message=None):
93 sys.stderr.write(usage(argspec))
95 sys.stderr.write('\nerror: ')
96 sys.stderr.write(message)
97 sys.stderr.write('\n')
100 def require_n_args_or_die(n, args):
101 if len(args) < n + 1:
102 misuse('%s argument requires %d %s'
103 % (n, 'values' if n == 1 else 'value'))
104 result = args[1:1+n], args[1+n:]
105 assert len(result[0]) == n
108 Spec = namedtuple('Spec', ('method', 'src', 'dest'))
112 return '--%s %s' % (s.method, path_msg(s.src))
113 return '--%s: %s %s' % (s.method, path_msg(s.src), path_msg(s.dest))
115 def parse_args(args):
122 opt.print_commits = opt.print_trees = opt.print_tags = False
125 opt.source = opt.remote = None
126 opt.target_specs = []
128 remaining = args[1:] # Skip argv[0]
131 if arg in (b'-h', b'--help'):
132 sys.stdout.write(usage(argspec))
134 elif arg in (b'-v', b'--verbose'):
136 remaining = remaining[1:]
137 elif arg in (b'--ff', b'--append', b'--pick', b'--force-pick',
138 b'--new-tag', b'--replace', b'--unnamed'):
139 (ref,), remaining = require_n_args_or_die(1, remaining)
140 opt.target_specs.append(Spec(method=arg[2:].decode('ascii'),
142 elif arg in (b'--ff:', b'--append:', b'--pick:', b'--force-pick:',
143 b'--new-tag:', b'--replace:'):
144 (ref, dest), remaining = require_n_args_or_die(2, remaining)
145 opt.target_specs.append(Spec(method=arg[2:-1].decode('ascii'),
147 elif arg in (b'-s', b'--source'):
148 (opt.source,), remaining = require_n_args_or_die(1, remaining)
149 elif arg in (b'-r', b'--remote'):
150 (opt.remote,), remaining = require_n_args_or_die(1, remaining)
151 elif arg in (b'-c', b'--print-commits'):
152 opt.print_commits, remaining = True, remaining[1:]
153 elif arg in (b'-t', b'--print-trees'):
154 opt.print_trees, remaining = True, remaining[1:]
155 elif arg == b'--print-tags':
156 opt.print_tags, remaining = True, remaining[1:]
157 elif arg in (b'-0', b'-1', b'-2', b'-3', b'-4', b'-5', b'-6', b'-7',
159 opt.compress = int(arg[1:])
160 remaining = remaining[1:]
161 elif arg == b'--compress':
162 (opt.compress,), remaining = require_n_args_or_die(1, remaining)
163 opt.compress = int(opt.compress)
164 elif arg == b'--bwlimit':
165 (opt.bwlimit,), remaining = require_n_args_or_die(1, remaining)
166 opt.bwlimit = int(opt.bwlimit)
167 elif arg.startswith(b'-') and len(arg) > 2 and arg[1] != b'-':
168 # Try to interpret this as -xyz, i.e. "-xyz -> -x -y -z".
169 # We do this last so that --foo -bar is valid if --foo
171 remaining[0:1] = (b'-' + bytes_from_byte(c) for c in arg[1:])
178 # FIXME: client error handling (remote exceptions, etc.)
180 # FIXME: walk_object in in git.py doesn't support opt.verbose. Do we
181 # need to adjust for that here?
182 def get_random_item(name, hash, repo, writer, opt):
183 def already_seen(oid):
184 return writer.exists(unhexlify(oid))
185 for item in walk_object(repo.cat, hash, stop_at=already_seen,
187 # already_seen ensures that writer.exists(id) is false.
188 # Otherwise, just_write() would fail.
189 writer.just_write(item.oid, item.type, item.data)
192 def append_commit(name, hash, parent, src_repo, writer, opt):
194 items = parse_commit(get_cat_data(src_repo.cat(hash), b'commit'))
195 tree = unhexlify(items.tree)
196 author = b'%s <%s>' % (items.author_name, items.author_mail)
197 author_time = (items.author_sec, items.author_offset)
198 committer = b'%s <%s@%s>' % (userfullname(), username(), hostname())
199 get_random_item(name, hexlify(tree), src_repo, writer, opt)
200 c = writer.new_commit(tree, parent,
201 author, items.author_sec, items.author_offset,
202 committer, now, None,
207 def append_commits(commits, src_name, dest_hash, src_repo, writer, opt):
208 last_c, tree = dest_hash, None
209 for commit in commits:
210 last_c, tree = append_commit(src_name, commit, last_c,
211 src_repo, writer, opt)
212 assert(tree is not None)
215 Loc = namedtuple('Loc', ['type', 'hash', 'path'])
216 default_loc = Loc(None, None, None)
218 def find_vfs_item(name, repo):
219 res = repo.resolve(name, follow=False, want_meta=False)
220 leaf_name, leaf_item = res[-1]
223 kind = type(leaf_item)
226 elif kind == vfs.Tags:
228 elif kind == vfs.RevList:
230 elif kind == vfs.Commit:
231 if len(res) > 1 and isinstance(res[-2][1], vfs.RevList):
235 elif kind == vfs.Item:
236 if S_ISDIR(vfs.item_mode(leaf_item)):
240 elif kind == vfs.Chunky:
242 elif kind == vfs.FakeLink:
243 # Don't have to worry about ELOOP, excepting malicious
244 # remotes, since "latest" is the only FakeLink.
245 assert leaf_name == b'latest'
246 res = repo.resolve(leaf_item.target, parent=res[:-1],
247 follow=False, want_meta=False)
248 leaf_name, leaf_item = res[-1]
250 assert isinstance(leaf_item, vfs.Commit)
251 name = b'/'.join(x[0] for x in res)
254 raise Exception('unexpected resolution for %s: %r'
255 % (path_msg(name), res))
256 path = b'/'.join(name for name, item in res)
257 if hasattr(leaf_item, 'coid'):
258 result = Loc(type=kind, hash=leaf_item.coid, path=path)
259 elif hasattr(leaf_item, 'oid'):
260 result = Loc(type=kind, hash=leaf_item.oid, path=path)
262 result = Loc(type=kind, hash=None, path=path)
266 Target = namedtuple('Target', ['spec', 'src', 'dest'])
270 loc = loc._replace(hash=hexlify(loc.hash))
274 # FIXME: see if resolve() means we can drop the vfs path cleanup
276 def cleanup_vfs_path(p):
277 result = os.path.normpath(p)
278 if result.startswith(b'/'):
283 def validate_vfs_path(p, spec):
284 if p.startswith(b'/.') \
285 and not p.startswith(b'/.tag/'):
286 misuse('unsupported destination path %s in %s'
287 % (path_msg(p), spec_msg(spec)))
291 def resolve_src(spec, src_repo):
292 src = find_vfs_item(spec.src, src_repo)
293 spec_args = spec_msg(spec)
295 misuse('cannot find source for %s' % spec_args)
296 if src.type == 'root':
297 misuse('cannot fetch entire repository for %s' % spec_args)
298 if src.type == 'tags':
299 misuse('cannot fetch entire /.tag directory for %s' % spec_args)
300 debug1('src: %s\n' % loc_desc(src))
304 def get_save_branch(repo, path):
305 res = repo.resolve(path, follow=False, want_meta=False)
306 leaf_name, leaf_item = res[-1]
308 misuse('error: cannot access %r in %r' % (leaf_name, path))
310 res_path = b'/'.join(name for name, item in res[:-1])
314 def resolve_branch_dest(spec, src, src_repo, dest_repo):
315 # Resulting dest must be treeish, or not exist.
317 # Pick a default dest.
318 if src.type == 'branch':
319 spec = spec._replace(dest=spec.src)
320 elif src.type == 'save':
321 spec = spec._replace(dest=get_save_branch(src_repo, spec.src))
322 elif src.path.startswith(b'/.tag/'): # Dest defaults to the same.
323 spec = spec._replace(dest=spec.src)
325 spec_args = spec_msg(spec)
327 misuse('no destination (implicit or explicit) for %s', spec_args)
329 dest = find_vfs_item(spec.dest, dest_repo)
331 if dest.type == 'commit':
332 misuse('destination for %s is a tagged commit, not a branch'
334 if dest.type != 'branch':
335 misuse('destination for %s is a %s, not a branch'
336 % (spec_args, dest.type))
338 dest = default_loc._replace(path=cleanup_vfs_path(spec.dest))
340 if dest.path.startswith(b'/.'):
341 misuse('destination for %s must be a valid branch name' % spec_args)
343 debug1('dest: %s\n' % loc_desc(dest))
347 def resolve_ff(spec, src_repo, dest_repo):
348 src = resolve_src(spec, src_repo)
349 spec_args = spec_msg(spec)
350 if src.type == 'tree':
351 misuse('%s is impossible; can only --append a tree to a branch'
353 if src.type not in ('branch', 'save', 'commit'):
354 misuse('source for %s must be a branch, save, or commit, not %s'
355 % (spec_args, src.type))
356 spec, dest = resolve_branch_dest(spec, src, src_repo, dest_repo)
357 return Target(spec=spec, src=src, dest=dest)
360 def handle_ff(item, src_repo, writer, opt):
361 assert item.spec.method == 'ff'
362 assert item.src.type in ('branch', 'save', 'commit')
363 src_oidx = hexlify(item.src.hash)
364 dest_oidx = hexlify(item.dest.hash) if item.dest.hash else None
365 if not dest_oidx or dest_oidx in src_repo.rev_list(src_oidx):
367 get_random_item(item.spec.src, src_oidx, src_repo, writer, opt)
368 commit_items = parse_commit(get_cat_data(src_repo.cat(src_oidx), b'commit'))
369 return item.src.hash, unhexlify(commit_items.tree)
370 misuse('destination is not an ancestor of source for %s'
371 % spec_msg(item.spec))
372 # misuse() doesn't return
376 def resolve_append(spec, src_repo, dest_repo):
377 src = resolve_src(spec, src_repo)
378 if src.type not in ('branch', 'save', 'commit', 'tree'):
379 misuse('source for %s must be a branch, save, commit, or tree, not %s'
380 % (spec_msg(spec), src.type))
381 spec, dest = resolve_branch_dest(spec, src, src_repo, dest_repo)
382 return Target(spec=spec, src=src, dest=dest)
385 def handle_append(item, src_repo, writer, opt):
386 assert item.spec.method == 'append'
387 assert item.src.type in ('branch', 'save', 'commit', 'tree')
388 assert item.dest.type == 'branch' or not item.dest.type
389 src_oidx = hexlify(item.src.hash)
390 if item.src.type == 'tree':
391 get_random_item(item.spec.src, src_oidx, src_repo, writer, opt)
392 parent = item.dest.hash
393 msg = b'bup save\n\nGenerated by command:\n%r\n' % compat.get_argvb()
394 userline = b'%s <%s@%s>' % (userfullname(), username(), hostname())
396 commit = writer.new_commit(item.src.hash, parent,
398 userline, now, None, msg)
399 return commit, item.src.hash
400 commits = list(src_repo.rev_list(src_oidx))
402 return append_commits(commits, item.spec.src, item.dest.hash,
403 src_repo, writer, opt)
406 def resolve_pick(spec, src_repo, dest_repo):
407 src = resolve_src(spec, src_repo)
408 spec_args = spec_msg(spec)
409 if src.type == 'tree':
410 misuse('%s is impossible; can only --append a tree' % spec_args)
411 if src.type not in ('commit', 'save'):
412 misuse('%s impossible; can only pick a commit or save, not %s'
413 % (spec_args, src.type))
415 if src.path.startswith(b'/.tag/'):
416 spec = spec._replace(dest=spec.src)
417 elif src.type == 'save':
418 spec = spec._replace(dest=get_save_branch(src_repo, spec.src))
420 misuse('no destination provided for %s', spec_args)
421 dest = find_vfs_item(spec.dest, dest_repo)
423 cp = validate_vfs_path(cleanup_vfs_path(spec.dest), spec)
424 dest = default_loc._replace(path=cp)
426 if not dest.type == 'branch' and not dest.path.startswith(b'/.tag/'):
427 misuse('%s destination is not a tag or branch' % spec_args)
428 if spec.method == 'pick' \
429 and dest.hash and dest.path.startswith(b'/.tag/'):
430 misuse('cannot overwrite existing tag for %s (requires --force-pick)'
432 return Target(spec=spec, src=src, dest=dest)
435 def handle_pick(item, src_repo, writer, opt):
436 assert item.spec.method in ('pick', 'force-pick')
437 assert item.src.type in ('save', 'commit')
438 src_oidx = hexlify(item.src.hash)
440 return append_commit(item.spec.src, src_oidx, item.dest.hash,
441 src_repo, writer, opt)
442 return append_commit(item.spec.src, src_oidx, None, src_repo, writer, opt)
445 def resolve_new_tag(spec, src_repo, dest_repo):
446 src = resolve_src(spec, src_repo)
447 spec_args = spec_msg(spec)
448 if not spec.dest and src.path.startswith(b'/.tag/'):
449 spec = spec._replace(dest=src.path)
451 misuse('no destination (implicit or explicit) for %s', spec_args)
452 dest = find_vfs_item(spec.dest, dest_repo)
454 dest = default_loc._replace(path=cleanup_vfs_path(spec.dest))
455 if not dest.path.startswith(b'/.tag/'):
456 misuse('destination for %s must be a VFS tag' % spec_args)
458 misuse('cannot overwrite existing tag for %s (requires --replace)'
460 return Target(spec=spec, src=src, dest=dest)
463 def handle_new_tag(item, src_repo, writer, opt):
464 assert item.spec.method == 'new-tag'
465 assert item.dest.path.startswith(b'/.tag/')
466 get_random_item(item.spec.src, hexlify(item.src.hash),
467 src_repo, writer, opt)
468 return (item.src.hash,)
471 def resolve_replace(spec, src_repo, dest_repo):
472 src = resolve_src(spec, src_repo)
473 spec_args = spec_msg(spec)
475 if src.path.startswith(b'/.tag/') or src.type == 'branch':
476 spec = spec._replace(dest=spec.src)
478 misuse('no destination provided for %s', spec_args)
479 dest = find_vfs_item(spec.dest, dest_repo)
481 if not dest.type == 'branch' and not dest.path.startswith(b'/.tag/'):
482 misuse('%s impossible; can only overwrite branch or tag'
485 cp = validate_vfs_path(cleanup_vfs_path(spec.dest), spec)
486 dest = default_loc._replace(path=cp)
487 if not dest.path.startswith(b'/.tag/') \
488 and not src.type in ('branch', 'save', 'commit'):
489 misuse('cannot overwrite branch with %s for %s' % (src.type, spec_args))
490 return Target(spec=spec, src=src, dest=dest)
493 def handle_replace(item, src_repo, writer, opt):
494 assert(item.spec.method == 'replace')
495 if item.dest.path.startswith(b'/.tag/'):
496 get_random_item(item.spec.src, hexlify(item.src.hash),
497 src_repo, writer, opt)
498 return (item.src.hash,)
499 assert(item.dest.type == 'branch' or not item.dest.type)
500 src_oidx = hexlify(item.src.hash)
501 get_random_item(item.spec.src, src_oidx, src_repo, writer, opt)
502 commit_items = parse_commit(get_cat_data(src_repo.cat(src_oidx), b'commit'))
503 return item.src.hash, unhexlify(commit_items.tree)
506 def resolve_unnamed(spec, src_repo, dest_repo):
508 misuse('destination name given for %s' % spec_msg(spec))
509 src = resolve_src(spec, src_repo)
510 return Target(spec=spec, src=src, dest=None)
513 def handle_unnamed(item, src_repo, writer, opt):
514 get_random_item(item.spec.src, hexlify(item.src.hash),
515 src_repo, writer, opt)
519 def resolve_targets(specs, src_repo, dest_repo):
521 common_args = src_repo, dest_repo
523 debug1('initial-spec: %r\n' % (spec,))
524 if spec.method == 'ff':
525 resolved_items.append(resolve_ff(spec, *common_args))
526 elif spec.method == 'append':
527 resolved_items.append(resolve_append(spec, *common_args))
528 elif spec.method in ('pick', 'force-pick'):
529 resolved_items.append(resolve_pick(spec, *common_args))
530 elif spec.method == 'new-tag':
531 resolved_items.append(resolve_new_tag(spec, *common_args))
532 elif spec.method == 'replace':
533 resolved_items.append(resolve_replace(spec, *common_args))
534 elif spec.method == 'unnamed':
535 resolved_items.append(resolve_unnamed(spec, *common_args))
536 else: # Should be impossible -- prevented by the option parser.
539 # FIXME: check for prefix overlap? i.e.:
540 # bup get --ff foo --ff: baz foo/bar
541 # bup get --new-tag .tag/foo --new-tag: bar .tag/foo/bar
543 # Now that we have all the items, check for duplicate tags.
544 tags_targeted = set()
545 for item in resolved_items:
546 dest_path = item.dest and item.dest.path
548 assert(dest_path.startswith(b'/'))
549 if dest_path.startswith(b'/.tag/'):
550 if dest_path in tags_targeted:
551 if item.spec.method not in ('replace', 'force-pick'):
552 misuse('cannot overwrite tag %s via %s' \
553 % (path_msg(dest_path), spec_msg(item.spec)))
555 tags_targeted.add(dest_path)
556 return resolved_items
559 def log_item(name, type, opt, tree=None, commit=None, tag=None):
560 if tag and opt.print_tags:
562 if tree and opt.print_trees:
564 if commit and opt.print_commits:
565 print(hexstr(commit))
568 if type in ('root', 'branch', 'save', 'commit', 'tree'):
569 if not name.endswith(b'/'):
571 log('%s%s\n' % (path_msg(name), last))
574 is_reverse = environ.get(b'BUP_SERVER_REVERSE')
575 opt = parse_args(argv)
576 git.check_repo_or_die()
578 opt.source = argv_bytes(opt.source)
580 client.bwlimit = parse_num(opt.bwlimit)
581 if is_reverse and opt.remote:
582 misuse("don't use -r in reverse mode; it's automatic")
583 if opt.remote or is_reverse:
584 dest_repo = RemoteRepo(opt.remote)
586 dest_repo = LocalRepo()
588 with dest_repo as dest_repo:
589 with LocalRepo(repo_dir=opt.source) as src_repo:
590 with dest_repo.new_packwriter(compression_level=opt.compress) as writer:
591 # Resolve and validate all sources and destinations,
592 # implicit or explicit, and do it up-front, so we can
593 # fail before we start writing (for any obviously
595 target_items = resolve_targets(opt.target_specs,
598 updated_refs = {} # ref_name -> (original_ref, tip_commit(bin))
599 no_ref_info = (None, None)
601 handlers = {'ff': handle_ff,
602 'append': handle_append,
603 'force-pick': handle_pick,
605 'new-tag': handle_new_tag,
606 'replace': handle_replace,
607 'unnamed': handle_unnamed}
609 for item in target_items:
610 debug1('get-spec: %r\n' % (item.spec,))
611 debug1('get-src: %s\n' % loc_desc(item.src))
612 debug1('get-dest: %s\n' % loc_desc(item.dest))
613 dest_path = item.dest and item.dest.path
615 if dest_path.startswith(b'/.tag/'):
616 dest_ref = b'refs/tags/%s' % dest_path[6:]
618 dest_ref = b'refs/heads/%s' % dest_path[1:]
622 dest_hash = item.dest and item.dest.hash
623 orig_ref, cur_ref = updated_refs.get(dest_ref, no_ref_info)
624 orig_ref = orig_ref or dest_hash
625 cur_ref = cur_ref or dest_hash
627 handler = handlers[item.spec.method]
628 item_result = handler(item, src_repo, writer, opt)
629 if len(item_result) > 1:
630 new_id, tree = item_result
632 new_id = item_result[0]
635 log_item(item.spec.src, item.src.type, opt)
637 updated_refs[dest_ref] = (orig_ref, new_id)
638 if dest_ref.startswith(b'refs/tags/'):
639 log_item(item.spec.src, item.src.type, opt, tag=new_id)
641 log_item(item.spec.src, item.src.type, opt,
642 tree=tree, commit=new_id)
644 # Only update the refs at the very end, once the writer is
645 # closed, so that if something goes wrong above, the old refs
646 # will be undisturbed.
647 for ref_name, info in updated_refs.items():
648 orig_ref, new_ref = info
650 dest_repo.update_ref(ref_name, new_ref, orig_ref)
652 new_hex = hexlify(new_ref)
654 orig_hex = hexlify(orig_ref)
655 log('updated %r (%s -> %s)\n' % (ref_name, orig_hex, new_hex))
657 log('updated %r (%s)\n' % (ref_name, new_hex))
658 except (git.GitError, client.ClientError) as ex:
659 add_error('unable to update ref %r: %s' % (ref_name, ex))
662 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))