4
4
from enum import Enum
5
5
import os
6
6
import shutil
7
-
7
+ import uuid
8
8
9
9
class TestRepository :
10
10
@@ -18,19 +18,31 @@ class VulnerabilityType(Enum):
18
18
__AUTHOR = pygit2 .
Signature (
'John Smith' ,
'[email protected] ' )
19
19
__COMMITER = pygit2 .
Signature (
'John Smith' ,
'[email protected] ' )
20
20
21
+ __initialCommit = None
22
+
21
23
introduced = []
22
24
fixed = []
23
25
last_affected = []
24
26
limit = []
25
27
26
- def __init__ (self , name : str ):
28
+ def __init__ (self , name : str ,debug :bool = False ):
29
+ self .debug = debug
27
30
self .name = name
28
31
if (os .path .exists (f"osv/testdata/test_repositories/{ name } " )):
29
32
shutil .rmtree (f"osv/testdata/test_repositories/{ name } " )
30
33
self .repo = pygit2 .init_repository (
31
34
f"osv/testdata/test_repositories/{ name } " , bare = False )
32
- #pygit2.settings.enable_caching(True)
33
-
35
+ #empty initial commit usefull for the creation of the repository
36
+ tree = self .repo .TreeBuilder ().write ()
37
+ self .__initialCommit = self .repo .create_commit ('refs/heads/main' , self .__AUTHOR ,
38
+ self .__COMMITER , "message" , tree , [])
39
+ self .create_branch (f"branch_{ self .__initialCommit .hex } " , self .__initialCommit )
40
+ self .repo .references .create (f"refs/remotes/origin/main" , self .__initialCommit )
41
+
42
+ def create_branch (self , name : str , commit : pygit2 .Oid ):
43
+ self .repo .references .create (f'refs/heads/{ name } ' , commit )
44
+ self .repo .references .create (f'refs/remotes/origin/{ name } ' , commit )
45
+
34
46
def add_empty_commit (
35
47
self ,
36
48
vulnerability : VulnerabilityType = VulnerabilityType .NONE ,
@@ -41,41 +53,36 @@ def add_empty_commit(
41
53
"""
42
54
43
55
tree = self .repo .TreeBuilder ().write ()
44
-
56
+ self . __AUTHOR = pygit2 . Signature ( str ( uuid . uuid1 ()), '[email protected] ' )
45
57
commit = None
46
58
47
- if (self .repo .is_empty ):
48
- commit = self .repo .create_commit ('refs/heads/master' , self .__AUTHOR ,
49
- self .__COMMITER , message , tree , [])
50
- self .repo .references .create ('refs/remotes/{0}/{1}' .format ("origin" , "master" ), commit )
51
- else :
59
+
60
+ if (len (parents ) == 0 ):
61
+ self .repo .create_branch (
62
+ f'branch_temp' , self .repo .revparse_single (self .__initialCommit .hex ))
63
+ commit = self .repo .create_commit ('refs/heads/branch_temp' , self .__AUTHOR ,
64
+ self .__COMMITER , message , tree , [self .__initialCommit ])
52
65
53
- created_branch : pygit2 .Branch = None
54
- branch_commit : pygit2 .Commit = None
55
- for parent in parents :
56
- if (self .__has_children (parent )):
57
- created_branch = self .repo .create_branch (
58
- f'branch_{ parent .hex } ' , self .repo .revparse_single (parent .hex ))
59
- branch_commit = parent
60
- self .repo .checkout (created_branch .name )
61
- break
66
+ self .repo .branches .delete (
67
+ f'branch_temp' )
68
+ self .create_branch (
69
+ f'branch_{ commit .hex } ' , commit )
62
70
63
- if (created_branch is None ):
64
- commit = self .repo .create_commit ('refs/heads/master' , self .__AUTHOR ,
65
- self .__COMMITER , message , tree ,
66
- parents )
67
- else :
68
- parents .remove (branch_commit )
69
- parents .insert (0 , branch_commit )
70
- commit = self .repo .create_commit (created_branch .name , self .__AUTHOR ,
71
- self .__COMMITER , message , tree ,
72
- parents )
73
- self .repo .references .create ('refs/remotes/{0}/{1}' .format ("origin" , branch_commit .name ), commit )
74
-
75
- self .repo .checkout ('refs/heads/master' )
76
- headCommit = self .repo .head .target
77
- self .repo .references .delete ('refs/remotes/{0}/{1}' .format ("origin" , "master" ))
78
- self .repo .references .create ('refs/remotes/{0}/{1}' .format ("origin" , "master" ),headCommit )
71
+ else :
72
+ self .repo .create_branch (
73
+ f'branch_temp' , self .repo .revparse_single (parents [0 ].hex ))
74
+ commit = self .repo .create_commit ('refs/heads/branch_temp' , self .__AUTHOR ,
75
+ self .__COMMITER , message , tree , parents )
76
+ self .repo .branches .delete (
77
+ f'branch_temp' )
78
+ self .create_branch (commit = commit , name = f'branch_{ commit .hex } ' )
79
+
80
+ self .repo .references .get ('refs/remotes/{0}/{1}' .format ("origin" , "main" )).set_target (commit )
81
+ self .repo .references .get ('refs/heads/main' ).set_target (commit )
82
+
83
+ if self .debug :
84
+ os .system (f"echo ------------------------------------------------------------------" )
85
+ os .system (f"git -C /home/creux/Documents/software_heritage/swhsec/osv.dev/osv/testdata/test_repositories/test log --all --graph --decorate" )
79
86
80
87
#self.repo.branches.delete(created_branch.branch_name)
81
88
@@ -94,6 +101,8 @@ def add_empty_commit(
94
101
raise ValueError ("Invalid vulnerability type" )
95
102
return commit
96
103
104
+
105
+
97
106
def __has_children (self , commit : pygit2 .Commit ) -> bool :
98
107
for current_commit in self .repo .walk (self .repo .head .target ,
99
108
pygit2 .GIT_SORT_TOPOLOGICAL ):
@@ -102,6 +111,11 @@ def __has_children(self, commit: pygit2.Commit) -> bool:
102
111
return True
103
112
return False
104
113
114
+ def remove (self ):
115
+ shutil .rmtree (f"osv/testdata/test_repositories/{ self .name } /" )
116
+ while os .path .exists (f"osv/testdata/test_repositories/{ self .name } /" ): # check if it exists
117
+ pass
118
+
105
119
def get_ranges (self ):
106
120
"""
107
121
return the ranges of the repository
0 commit comments