]> arthur.barton.de Git - bup.git/blob - lib/bup/t/tgit.py
1fa1c1c6d3f2d759f9b8cf74e14f1455e4b8fb25
[bup.git] / lib / bup / t / tgit.py
1 import struct, os, tempfile, time
2 from subprocess import check_call
3 from bup import git
4 from bup.helpers import *
5 from wvtest import *
6
7
8 top_dir = os.path.realpath('../../..')
9 bup_exe = top_dir + '/bup'
10 bup_tmp = top_dir + '/t/tmp'
11
12
13 def exc(*cmd):
14     cmd_str = ' '.join(cmd)
15     print >> sys.stderr, cmd_str
16     check_call(cmd)
17
18
19 def exo(*cmd):
20     cmd_str = ' '.join(cmd)
21     print >> sys.stderr, cmd_str
22     return readpipe(cmd)
23
24
25 @wvtest
26 def testmangle():
27     afile  = 0100644
28     afile2 = 0100770
29     alink  = 0120000
30     adir   = 0040000
31     adir2  = 0040777
32     WVPASSEQ(git.mangle_name("a", adir2, adir), "a")
33     WVPASSEQ(git.mangle_name(".bup", adir2, adir), ".bup.bupl")
34     WVPASSEQ(git.mangle_name("a.bupa", adir2, adir), "a.bupa.bupl")
35     WVPASSEQ(git.mangle_name("b.bup", alink, alink), "b.bup.bupl")
36     WVPASSEQ(git.mangle_name("b.bu", alink, alink), "b.bu")
37     WVPASSEQ(git.mangle_name("f", afile, afile2), "f")
38     WVPASSEQ(git.mangle_name("f.bup", afile, afile2), "f.bup.bupl")
39     WVPASSEQ(git.mangle_name("f.bup", afile, adir), "f.bup.bup")
40     WVPASSEQ(git.mangle_name("f", afile, adir), "f.bup")
41
42     WVPASSEQ(git.demangle_name("f.bup"), ("f", git.BUP_CHUNKED))
43     WVPASSEQ(git.demangle_name("f.bupl"), ("f", git.BUP_NORMAL))
44     WVPASSEQ(git.demangle_name("f.bup.bupl"), ("f.bup", git.BUP_NORMAL))
45
46     # for safety, we ignore .bup? suffixes we don't recognize.  Future
47     # versions might implement a .bup[a-z] extension as something other
48     # than BUP_NORMAL.
49     WVPASSEQ(git.demangle_name("f.bupa"), ("f.bupa", git.BUP_NORMAL))
50
51
52 @wvtest
53 def testencode():
54     s = 'hello world'
55     looseb = ''.join(git._encode_looseobj('blob', s))
56     looset = ''.join(git._encode_looseobj('tree', s))
57     loosec = ''.join(git._encode_looseobj('commit', s))
58     packb = ''.join(git._encode_packobj('blob', s))
59     packt = ''.join(git._encode_packobj('tree', s))
60     packc = ''.join(git._encode_packobj('commit', s))
61     WVPASSEQ(git._decode_looseobj(looseb), ('blob', s))
62     WVPASSEQ(git._decode_looseobj(looset), ('tree', s))
63     WVPASSEQ(git._decode_looseobj(loosec), ('commit', s))
64     WVPASSEQ(git._decode_packobj(packb), ('blob', s))
65     WVPASSEQ(git._decode_packobj(packt), ('tree', s))
66     WVPASSEQ(git._decode_packobj(packc), ('commit', s))
67
68
69 @wvtest
70 def testpacks():
71     initial_failures = wvfailure_count()
72     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
73     os.environ['BUP_MAIN_EXE'] = bup_exe
74     os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
75     git.init_repo(bupdir)
76     git.verbose = 1
77
78     w = git.PackWriter()
79     w.new_blob(os.urandom(100))
80     w.new_blob(os.urandom(100))
81     w.abort()
82
83     w = git.PackWriter()
84     hashes = []
85     nobj = 1000
86     for i in range(nobj):
87         hashes.append(w.new_blob(str(i)))
88     log('\n')
89     nameprefix = w.close()
90     print repr(nameprefix)
91     WVPASS(os.path.exists(nameprefix + '.pack'))
92     WVPASS(os.path.exists(nameprefix + '.idx'))
93
94     r = git.open_idx(nameprefix + '.idx')
95     print repr(r.fanout)
96
97     for i in range(nobj):
98         WVPASS(r.find_offset(hashes[i]) > 0)
99     WVPASS(r.exists(hashes[99]))
100     WVFAIL(r.exists('\0'*20))
101
102     pi = iter(r)
103     for h in sorted(hashes):
104         WVPASSEQ(str(pi.next()).encode('hex'), h.encode('hex'))
105
106     WVFAIL(r.find_offset('\0'*20))
107
108     r = git.PackIdxList(bupdir + '/objects/pack')
109     WVPASS(r.exists(hashes[5]))
110     WVPASS(r.exists(hashes[6]))
111     WVFAIL(r.exists('\0'*20))
112     if wvfailure_count() == initial_failures:
113         subprocess.call(['rm', '-rf', tmpdir])
114
115 @wvtest
116 def test_pack_name_lookup():
117     initial_failures = wvfailure_count()
118     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
119     os.environ['BUP_MAIN_EXE'] = bup_exe
120     os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
121     git.init_repo(bupdir)
122     git.verbose = 1
123     packdir = git.repo('objects/pack')
124
125     idxnames = []
126     hashes = []
127
128     for start in range(0,28,2):
129         w = git.PackWriter()
130         for i in range(start, start+2):
131             hashes.append(w.new_blob(str(i)))
132         log('\n')
133         idxnames.append(os.path.basename(w.close() + '.idx'))
134
135     r = git.PackIdxList(packdir)
136     WVPASSEQ(len(r.packs), 2)
137     for e,idxname in enumerate(idxnames):
138         for i in range(e*2, (e+1)*2):
139             WVPASSEQ(r.exists(hashes[i], want_source=True), idxname)
140     if wvfailure_count() == initial_failures:
141         subprocess.call(['rm', '-rf', tmpdir])
142
143
144 @wvtest
145 def test_long_index():
146     initial_failures = wvfailure_count()
147     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
148     os.environ['BUP_MAIN_EXE'] = bup_exe
149     os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
150     git.init_repo(bupdir)
151     w = git.PackWriter()
152     obj_bin = struct.pack('!IIIII',
153             0x00112233, 0x44556677, 0x88990011, 0x22334455, 0x66778899)
154     obj2_bin = struct.pack('!IIIII',
155             0x11223344, 0x55667788, 0x99001122, 0x33445566, 0x77889900)
156     obj3_bin = struct.pack('!IIIII',
157             0x22334455, 0x66778899, 0x00112233, 0x44556677, 0x88990011)
158     pack_bin = struct.pack('!IIIII',
159             0x99887766, 0x55443322, 0x11009988, 0x77665544, 0x33221100)
160     idx = list(list() for i in xrange(256))
161     idx[0].append((obj_bin, 1, 0xfffffffff))
162     idx[0x11].append((obj2_bin, 2, 0xffffffffff))
163     idx[0x22].append((obj3_bin, 3, 0xff))
164     (fd,name) = tempfile.mkstemp(suffix='.idx', dir=git.repo('objects'))
165     os.close(fd)
166     w.count = 3
167     r = w._write_pack_idx_v2(name, idx, pack_bin)
168     i = git.PackIdxV2(name, open(name, 'rb'))
169     WVPASSEQ(i.find_offset(obj_bin), 0xfffffffff)
170     WVPASSEQ(i.find_offset(obj2_bin), 0xffffffffff)
171     WVPASSEQ(i.find_offset(obj3_bin), 0xff)
172     if wvfailure_count() == initial_failures:
173         os.remove(name)
174         subprocess.call(['rm', '-rf', tmpdir])
175
176
177 @wvtest
178 def test_check_repo_or_die():
179     initial_failures = wvfailure_count()
180     orig_cwd = os.getcwd()
181     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
182     os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
183     try:
184         os.chdir(tmpdir)
185         git.init_repo(bupdir)
186         git.check_repo_or_die()
187         WVPASS('check_repo_or_die')  # if we reach this point the call above passed
188
189         os.rename(bupdir + '/objects/pack', bupdir + '/objects/pack.tmp')
190         open(bupdir + '/objects/pack', 'w').close()
191         try:
192             git.check_repo_or_die()
193         except SystemExit, e:
194             WVPASSEQ(e.code, 14)
195         else:
196             WVFAIL()
197         os.unlink(bupdir + '/objects/pack')
198         os.rename(bupdir + '/objects/pack.tmp', bupdir + '/objects/pack')
199
200         try:
201             git.check_repo_or_die('nonexistantbup.tmp')
202         except SystemExit, e:
203             WVPASSEQ(e.code, 15)
204         else:
205             WVFAIL()
206     finally:
207         os.chdir(orig_cwd)
208     if wvfailure_count() == initial_failures:
209         subprocess.call(['rm', '-rf', tmpdir])
210
211
212 @wvtest
213 def test_commit_parsing():
214     def restore_env_var(name, val):
215         if val is None:
216             del os.environ[name]
217         else:
218             os.environ[name] = val
219     def showval(commit, val):
220         return readpipe(['git', 'show', '-s',
221                          '--pretty=format:%s' % val, commit]).strip()
222     initial_failures = wvfailure_count()
223     orig_cwd = os.getcwd()
224     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
225     workdir = tmpdir + "/work"
226     repodir = workdir + '/.git'
227     orig_author_name = os.environ.get('GIT_AUTHOR_NAME')
228     orig_author_email = os.environ.get('GIT_AUTHOR_EMAIL')
229     orig_committer_name = os.environ.get('GIT_COMMITTER_NAME')
230     orig_committer_email = os.environ.get('GIT_COMMITTER_EMAIL')
231     os.environ['GIT_AUTHOR_NAME'] = 'bup test'
232     os.environ['GIT_COMMITTER_NAME'] = os.environ['GIT_AUTHOR_NAME']
233     os.environ['GIT_AUTHOR_EMAIL'] = 'bup@a425bc70a02811e49bdf73ee56450e6f'
234     os.environ['GIT_COMMITTER_EMAIL'] = os.environ['GIT_AUTHOR_EMAIL']
235     try:
236         readpipe(['git', 'init', workdir])
237         os.environ['GIT_DIR'] = os.environ['BUP_DIR'] = repodir
238         git.check_repo_or_die(repodir)
239         os.chdir(workdir)
240         with open('foo', 'w') as f:
241             print >> f, 'bar'
242         readpipe(['git', 'add', '.'])
243         readpipe(['git', 'commit', '-am', 'Do something',
244                   '--author', 'Someone <someone@somewhere>',
245                   '--date', 'Sat Oct 3 19:48:49 2009 -0400'])
246         commit = readpipe(['git', 'show-ref', '-s', 'master']).strip()
247         parents = showval(commit, '%P')
248         tree = showval(commit, '%T')
249         cname = showval(commit, '%cn')
250         cmail = showval(commit, '%ce')
251         cdate = showval(commit, '%ct')
252         coffs = showval(commit, '%ci')
253         coffs = coffs[-5:]
254         coff = (int(coffs[-4:-2]) * 60 * 60) + (int(coffs[-2:]) * 60)
255         if coffs[-5] == '-':
256             coff = - coff
257         commit_items = git.get_commit_items(commit, git.cp())
258         WVPASSEQ(commit_items.parents, [])
259         WVPASSEQ(commit_items.tree, tree)
260         WVPASSEQ(commit_items.author_name, 'Someone')
261         WVPASSEQ(commit_items.author_mail, 'someone@somewhere')
262         WVPASSEQ(commit_items.author_sec, 1254613729)
263         WVPASSEQ(commit_items.author_offset, -(4 * 60 * 60))
264         WVPASSEQ(commit_items.committer_name, cname)
265         WVPASSEQ(commit_items.committer_mail, cmail)
266         WVPASSEQ(commit_items.committer_sec, int(cdate))
267         WVPASSEQ(commit_items.committer_offset, coff)
268         WVPASSEQ(commit_items.message, 'Do something\n')
269         with open('bar', 'w') as f:
270             print >> f, 'baz'
271         readpipe(['git', 'add', '.'])
272         readpipe(['git', 'commit', '-am', 'Do something else'])
273         child = readpipe(['git', 'show-ref', '-s', 'master']).strip()
274         parents = showval(child, '%P')
275         commit_items = git.get_commit_items(child, git.cp())
276         WVPASSEQ(commit_items.parents, [commit])
277     finally:
278         os.chdir(orig_cwd)
279         restore_env_var('GIT_AUTHOR_NAME', orig_author_name)
280         restore_env_var('GIT_AUTHOR_EMAIL', orig_author_email)
281         restore_env_var('GIT_COMMITTER_NAME', orig_committer_name)
282         restore_env_var('GIT_COMMITTER_EMAIL', orig_committer_email)
283     if wvfailure_count() == initial_failures:
284         subprocess.call(['rm', '-rf', tmpdir])
285
286
287 @wvtest
288 def test_list_refs():
289     initial_failures = wvfailure_count()
290     tmpdir = tempfile.mkdtemp(dir=bup_tmp, prefix='bup-tgit-')
291     os.environ['BUP_MAIN_EXE'] = bup_exe
292     os.environ['BUP_DIR'] = bupdir = tmpdir + "/bup"
293     src = tmpdir + '/src'
294     mkdirp(src)
295     with open(src + '/1', 'w+') as f:
296         print f, 'something'
297     with open(src + '/2', 'w+') as f:
298         print f, 'something else'
299     git.init_repo(bupdir)
300     emptyset = frozenset()
301     WVPASSEQ(frozenset(git.list_refs()), emptyset)
302     WVPASSEQ(frozenset(git.list_refs(limit_to_tags=True)), emptyset)
303     WVPASSEQ(frozenset(git.list_refs(limit_to_heads=True)), emptyset)
304     exc(bup_exe, 'index', src)
305     exc(bup_exe, 'save', '-n', 'src', '--strip', src)
306     src_hash = exo('git', '--git-dir', bupdir,
307                    'rev-parse', 'src').strip().split('\n')
308     assert(len(src_hash) == 1)
309     src_hash = src_hash[0].decode('hex')
310     tree_hash = exo('git', '--git-dir', bupdir,
311                    'rev-parse', 'src:').strip().split('\n')[0].decode('hex')
312     blob_hash = exo('git', '--git-dir', bupdir,
313                    'rev-parse', 'src:1').strip().split('\n')[0].decode('hex')
314     WVPASSEQ(frozenset(git.list_refs()),
315              frozenset([('refs/heads/src', src_hash)]))
316     WVPASSEQ(frozenset(git.list_refs(limit_to_tags=True)), emptyset)
317     WVPASSEQ(frozenset(git.list_refs(limit_to_heads=True)),
318              frozenset([('refs/heads/src', src_hash)]))
319     exc('git', '--git-dir', bupdir, 'tag', 'commit-tag', 'src')
320     WVPASSEQ(frozenset(git.list_refs()),
321              frozenset([('refs/heads/src', src_hash),
322                         ('refs/tags/commit-tag', src_hash)]))
323     WVPASSEQ(frozenset(git.list_refs(limit_to_tags=True)),
324              frozenset([('refs/tags/commit-tag', src_hash)]))
325     WVPASSEQ(frozenset(git.list_refs(limit_to_heads=True)),
326              frozenset([('refs/heads/src', src_hash)]))
327     exc('git', '--git-dir', bupdir, 'tag', 'tree-tag', 'src:')
328     exc('git', '--git-dir', bupdir, 'tag', 'blob-tag', 'src:1')
329     os.unlink(bupdir + '/refs/heads/src')
330     expected_tags = frozenset([('refs/tags/commit-tag', src_hash),
331                                ('refs/tags/tree-tag', tree_hash),
332                                ('refs/tags/blob-tag', blob_hash)])
333     WVPASSEQ(frozenset(git.list_refs()), expected_tags)
334     WVPASSEQ(frozenset(git.list_refs(limit_to_heads=True)), frozenset([]))
335     WVPASSEQ(frozenset(git.list_refs(limit_to_tags=True)), expected_tags)
336     if wvfailure_count() == initial_failures:
337         subprocess.call(['rm', '-rf', tmpdir])