]> arthur.barton.de Git - bup.git/blob - cmd-split.py
Split packs around 100M objects or 1G bytes.
[bup.git] / cmd-split.py
1 #!/usr/bin/env python2.5
2 import sys, time, struct
3 import hashsplit, git, options, client
4 from helpers import *
5 from subprocess import PIPE
6
7
8 optspec = """
9 bup split [-tcb] [-n name] [--bench] [filenames...]
10 --
11 r,remote=  remote repository path
12 b,blobs    output a series of blob ids
13 t,tree     output a tree id
14 c,commit   output a commit id
15 n,name=    name of backup set to update (if any)
16 v,verbose  increase log output (can be used more than once)
17 bench      print benchmark timings to stderr
18 max-pack-size=  maximum bytes in a single pack
19 max-pack-objects=  maximum number of objects in a single pack
20 """
21 o = options.Options('bup split', optspec)
22 (opt, flags, extra) = o.parse(sys.argv[1:])
23
24 git.check_repo_or_die()
25 if not (opt.blobs or opt.tree or opt.commit or opt.name):
26     log("bup split: use one or more of -b, -t, -c, -n\n")
27     o.usage()
28
29 hashsplit.split_verbosely = opt.verbose
30 if opt.verbose >= 2:
31     git.verbose = opt.verbose - 1
32     opt.bench = 1
33 if opt.max_pack_size:
34     hashsplit.max_pack_size = int(opt.max_pack_size)
35 if opt.max_pack_objects:
36     hashsplit.max_pack_objects = int(opt.max_pack_objects)
37
38 start_time = time.time()
39
40 refname = opt.name and 'refs/heads/%s' % opt.name or None
41 if opt.remote:
42     cli = client.Client(opt.remote)
43     oldref = refname and cli.read_ref(refname) or None
44     w = cli.new_packwriter()
45 else:
46     cli = None
47     oldref = refname and git.read_ref(refname) or None
48     w = git.PackWriter()
49     
50 (shalist,tree) = hashsplit.split_to_tree(w, hashsplit.autofiles(extra))
51
52 if opt.verbose:
53     log('\n')
54 if opt.blobs:
55     for (mode,name,bin) in shalist:
56         print bin.encode('hex')
57 if opt.tree:
58     print tree.encode('hex')
59 if opt.commit or opt.name:
60     msg = 'bup split\n\nGenerated by command:\n%r' % sys.argv
61     ref = opt.name and ('refs/heads/%s' % opt.name) or None
62     commit = w.new_commit(oldref, tree, msg)
63     if opt.commit:
64         print commit.encode('hex')
65
66 w.close()  # must close before we can update the ref
67         
68 if opt.name:
69     if cli:
70         cli.update_ref(refname, commit, oldref)
71     else:
72         git.update_ref(refname, commit, oldref)
73
74 if cli:
75     cli.close()
76
77 secs = time.time() - start_time
78 size = hashsplit.total_split
79 if opt.bench:
80     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
81         % (size/1024., secs, size/1024./secs))