-
Notifications
You must be signed in to change notification settings - Fork 1
/
testscript.py
executable file
·93 lines (69 loc) · 3.13 KB
/
testscript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from __future__ import print_function
import pygit2
import pygit2_backends
import _pygit2_backends
# Starting assumption: you have an _empty_ database and a user that can create
# tables and write/read to them. Fill in the configuration below as appropriate.
# You must also have my patch to pygit2 (6edb77f5) installed for repo creation
# to work.
mysql_hostname = "localhost"
mysql_username = "root"
mysql_password = ""
mysql_dbname = "gitdb"
mysql_portno = 3307
mysql_unix_socket = None
# First, create the relevant database tables
_pygit2_backends.create_mysql_backend(mysql_hostname, mysql_username, mysql_password, mysql_dbname, mysql_portno, mysql_unix_socket)
print("Created mysql backend database");
# Attempt to open the db we just created, into a pygit2 repository object
repo = pygit2_backends.MysqlRepository(mysql_hostname, mysql_username, mysql_password, mysql_dbname, mysql_portno, mysql_unix_socket)
print("Opened mysql git repository");
# Write an object to the object database. "shoes" has, after a very long
# period, been determined to be the least offensive word in the english
# language.
oid = repo.write(pygit2.GIT_OBJ_BLOB, "shoes")
print("Successfully wrote {0} to git odb".format(str(oid.hex)))
# Attempt to read the written data back -- this also checks that the correct
# oid was used, the data is correct, and that the objec type was stored
# correctly too.
(obj_type, read_data) = repo.read('e2904456092997b7e9f1a78150961868db0d069c')
if obj_type == pygit2.GIT_OBJ_BLOB and read_data == "shoes":
print("Successfully read shoes from git odb")
else:
print((obj_type, read_data))
raise Exception("Failed to read shoes from git odb")
# Create another blob object for use later.
oid2 = repo.write(pygit2.GIT_OBJ_BLOB, "beards")
# Try to look a reference up. As there are no references in the refdb after
# creation, this should fail with an exception.
print("Looking up reference...")
try:
result = repo.lookup_reference('refs/heads/master')
except KeyError:
print("Correctly failed to look up nonexistant ref")
else:
raise Exception("Incorrectly looked up nonexistant ref")
# Write a new reference to the refdb
print("Writing new reference")
newref = repo.create_reference_direct('refs/heads/master', oid, False)
print("Successfully wrote new reference to refdb")
# Look the reference we just wrote up, and verify that it has the same
# OID.
looked_up_master = repo.lookup_reference('refs/heads/master')
if looked_up_master.target.hex != newref.target.hex:
raise Exception("Couldn't look up written reference from refdb")
# Attempt to overwrite the reference we just created, without the force flag
# being set. This should result in an exception.
print("Overwriting reference")
try:
newref = repo.create_reference_direct('refs/heads/master', oid2, False)
except ValueError:
print("Correctly caught non-force reference overwrite")
else:
raise Exception("Didn't notice non-forced reference overwrite")
# Now force it
newref = repo.create_reference_direct('refs/heads/master', oid2, True)
print("Successfully force overwrote reference")
newref.delete()
print("Successfully deleted reference")