1
- use std:: {
2
- fmt:: { Debug , Formatter } ,
3
- sync:: Arc ,
4
- } ;
5
-
6
- use parking_lot:: Mutex ;
1
+ use std:: fmt:: { Debug , Formatter } ;
7
2
8
3
use crate :: git:: { CommitDiff , CommitDiffLoader , CommitDiffLoaderOptions , Config , GitError , RepositoryLoadKind } ;
9
4
10
5
/// A light cloneable, simple wrapper around the `git2::Repository` struct
11
- #[ derive( Clone ) ]
12
6
pub ( crate ) struct Repository {
13
- repository : Arc < Mutex < git2:: Repository > > ,
7
+ repository : git2:: Repository ,
14
8
}
15
9
16
10
impl Repository {
@@ -27,20 +21,15 @@ impl Repository {
27
21
cause : e,
28
22
}
29
23
} ) ?;
30
- Ok ( Self {
31
- repository : Arc :: new ( Mutex :: new ( repository) ) ,
32
- } )
24
+ Ok ( Self { repository } )
33
25
}
34
26
35
27
/// Load the git configuration for the repository.
36
28
///
37
29
/// # Errors
38
30
/// Will result in an error if the configuration is invalid.
39
31
pub ( crate ) fn load_config ( & self ) -> Result < Config , GitError > {
40
- self . repository
41
- . lock ( )
42
- . config ( )
43
- . map_err ( |e| GitError :: ConfigLoad { cause : e } )
32
+ self . repository . config ( ) . map_err ( |e| GitError :: ConfigLoad { cause : e } )
44
33
}
45
34
46
35
/// Load a diff for a commit hash
@@ -54,12 +43,10 @@ impl Repository {
54
43
) -> Result < CommitDiff , GitError > {
55
44
let oid = self
56
45
. repository
57
- . lock ( )
58
46
. revparse_single ( hash)
59
47
. map_err ( |e| GitError :: CommitLoad { cause : e } ) ?
60
48
. id ( ) ;
61
- let diff_loader_repository = Arc :: clone ( & self . repository ) ;
62
- let loader = CommitDiffLoader :: new ( diff_loader_repository, config) ;
49
+ let loader = CommitDiffLoader :: new ( & self . repository , config) ;
63
50
64
51
loader
65
52
. load_from_hash ( oid)
@@ -69,29 +56,23 @@ impl Repository {
69
56
70
57
impl From < git2:: Repository > for Repository {
71
58
fn from ( repository : git2:: Repository ) -> Self {
72
- Self {
73
- repository : Arc :: new ( Mutex :: new ( repository) ) ,
74
- }
59
+ Self { repository }
75
60
}
76
61
}
77
62
78
63
impl Debug for Repository {
79
64
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result < ( ) , std:: fmt:: Error > {
80
65
f. debug_struct ( "Repository" )
81
- . field ( "[path]" , & self . repository . lock ( ) . path ( ) )
66
+ . field ( "[path]" , & self . repository . path ( ) )
82
67
. finish ( )
83
68
}
84
69
}
85
70
86
71
#[ cfg( test) ]
87
72
mod tests {
88
- use std:: {
89
- path:: { Path , PathBuf } ,
90
- sync:: Arc ,
91
- } ;
73
+ use std:: path:: { Path , PathBuf } ;
92
74
93
75
use git2:: { Oid , Signature } ;
94
- use parking_lot:: Mutex ;
95
76
96
77
use crate :: git:: { Commit , GitError , Reference , Repository , RepositoryLoadKind } ;
97
78
@@ -107,18 +88,16 @@ mod tests {
107
88
cause : e,
108
89
}
109
90
} ) ?;
110
- Ok ( Self {
111
- repository : Arc :: new ( Mutex :: new ( repository) ) ,
112
- } )
91
+ Ok ( Self { repository } )
113
92
}
114
93
115
94
/// Find a reference by the reference name.
116
95
///
117
96
/// # Errors
118
97
/// Will result in an error if the reference cannot be found.
119
98
pub ( crate ) fn find_reference ( & self , reference : & str ) -> Result < Reference , GitError > {
120
- let repo = self . repository . lock ( ) ;
121
- let git2_reference = repo
99
+ let git2_reference = self
100
+ . repository
122
101
. find_reference ( reference)
123
102
. map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
124
103
Ok ( Reference :: from ( & git2_reference) )
@@ -129,39 +108,35 @@ mod tests {
129
108
/// # Errors
130
109
/// Will result in an error if the reference cannot be found or is not a commit.
131
110
pub ( crate ) fn find_commit ( & self , reference : & str ) -> Result < Commit , GitError > {
132
- let repo = self . repository . lock ( ) ;
133
- let git2_reference = repo
111
+ let reference = self
112
+ . repository
134
113
. find_reference ( reference)
135
114
. map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
136
- Commit :: try_from ( & git2_reference )
115
+ Commit :: try_from ( & reference )
137
116
}
138
117
139
118
pub ( crate ) fn repo_path ( & self ) -> PathBuf {
140
- self . repository . lock ( ) . path ( ) . to_path_buf ( )
119
+ self . repository . path ( ) . to_path_buf ( )
141
120
}
142
121
143
122
pub ( crate ) fn head_id ( & self , head_name : & str ) -> Result < Oid , git2:: Error > {
144
- let repo = self . repository . lock ( ) ;
145
123
let ref_name = format ! ( "refs/heads/{head_name}" ) ;
146
- let revision = repo . revparse_single ( ref_name. as_str ( ) ) ?;
124
+ let revision = self . repository . revparse_single ( ref_name. as_str ( ) ) ?;
147
125
Ok ( revision. id ( ) )
148
126
}
149
127
150
128
pub ( crate ) fn commit_id_from_ref ( & self , reference : & str ) -> Result < Oid , git2:: Error > {
151
- let repo = self . repository . lock ( ) ;
152
- let commit = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
129
+ let commit = self . repository . find_reference ( reference) ?. peel_to_commit ( ) ?;
153
130
Ok ( commit. id ( ) )
154
131
}
155
132
156
133
pub ( crate ) fn add_path_to_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
157
- let repo = self . repository . lock ( ) ;
158
- let mut index = repo. index ( ) ?;
134
+ let mut index = self . repository . index ( ) ?;
159
135
index. add_path ( path)
160
136
}
161
137
162
138
pub ( crate ) fn remove_path_from_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
163
- let repo = self . repository . lock ( ) ;
164
- let mut index = repo. index ( ) ?;
139
+ let mut index = self . repository . index ( ) ?;
165
140
index. remove_path ( path)
166
141
}
167
142
@@ -172,15 +147,16 @@ mod tests {
172
147
committer : & Signature < ' _ > ,
173
148
message : & str ,
174
149
) -> Result < ( ) , git2:: Error > {
175
- let repo = self . repository . lock ( ) ;
176
- let tree = repo. find_tree ( repo. index ( ) ?. write_tree ( ) ?) ?;
177
- let head = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
178
- _ = repo. commit ( Some ( "HEAD" ) , author, committer, message, & tree, & [ & head] ) ?;
150
+ let tree = self . repository . find_tree ( self . repository . index ( ) ?. write_tree ( ) ?) ?;
151
+ let head = self . repository . find_reference ( reference) ?. peel_to_commit ( ) ?;
152
+ _ = self
153
+ . repository
154
+ . commit ( Some ( "HEAD" ) , author, committer, message, & tree, & [ & head] ) ?;
179
155
Ok ( ( ) )
180
156
}
181
157
182
- pub ( crate ) fn repository ( & self ) -> Arc < Mutex < git2:: Repository > > {
183
- Arc :: clone ( & self . repository )
158
+ pub ( crate ) fn repository ( & self ) -> & git2:: Repository {
159
+ & self . repository
184
160
}
185
161
}
186
162
}
@@ -267,10 +243,11 @@ mod unix_tests {
267
243
fn load_commit_diff_with_non_commit ( ) {
268
244
with_temp_repository ( |repository| {
269
245
let blob_ref = {
270
- let git2_repository = repository. repository ( ) ;
271
- let git2_lock = git2_repository. lock ( ) ;
272
- let blob = git2_lock. blob ( b"foo" ) . unwrap ( ) ;
273
- _ = git2_lock. reference ( "refs/blob" , blob, false , "blob" ) . unwrap ( ) ;
246
+ let blob = repository. repository ( ) . blob ( b"foo" ) . unwrap ( ) ;
247
+ _ = repository
248
+ . repository ( )
249
+ . reference ( "refs/blob" , blob, false , "blob" )
250
+ . unwrap ( ) ;
274
251
blob. to_string ( )
275
252
} ;
276
253
0 commit comments