1
1
use std:: {
2
2
fmt:: { Debug , Formatter } ,
3
- path:: { Path , PathBuf } ,
4
3
sync:: Arc ,
5
4
} ;
6
5
7
- use git2:: { Oid , Signature } ;
8
6
use parking_lot:: Mutex ;
9
7
10
- use crate :: git:: {
11
- Commit ,
12
- CommitDiff ,
13
- CommitDiffLoader ,
14
- CommitDiffLoaderOptions ,
15
- Config ,
16
- GitError ,
17
- Reference ,
18
- RepositoryLoadKind ,
19
- } ;
8
+ use crate :: git:: { CommitDiff , CommitDiffLoader , CommitDiffLoaderOptions , Config , GitError , RepositoryLoadKind } ;
20
9
21
10
/// A light cloneable, simple wrapper around the `git2::Repository` struct
22
11
#[ derive( Clone ) ]
@@ -43,22 +32,6 @@ impl Repository {
43
32
} )
44
33
}
45
34
46
- /// Attempt to open an already-existing repository at `path`.
47
- ///
48
- /// # Errors
49
- /// Will result in an error if the repository cannot be opened.
50
- pub ( crate ) fn open_from_path ( path : & Path ) -> Result < Self , GitError > {
51
- let repository = git2:: Repository :: open ( path) . map_err ( |e| {
52
- GitError :: RepositoryLoad {
53
- kind : RepositoryLoadKind :: Path ,
54
- cause : e,
55
- }
56
- } ) ?;
57
- Ok ( Self {
58
- repository : Arc :: new ( Mutex :: new ( repository) ) ,
59
- } )
60
- }
61
-
62
35
/// Load the git configuration for the repository.
63
36
///
64
37
/// # Errors
@@ -93,78 +66,6 @@ impl Repository {
93
66
. map_err ( |e| GitError :: CommitLoad { cause : e } ) ?
94
67
. remove ( 0 ) )
95
68
}
96
-
97
- /// Find a reference by the reference name.
98
- ///
99
- /// # Errors
100
- /// Will result in an error if the reference cannot be found.
101
- pub ( crate ) fn find_reference ( & self , reference : & str ) -> Result < Reference , GitError > {
102
- let repo = self . repository . lock ( ) ;
103
- let git2_reference = repo
104
- . find_reference ( reference)
105
- . map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
106
- Ok ( Reference :: from ( & git2_reference) )
107
- }
108
-
109
- /// Find a commit by a reference name.
110
- ///
111
- /// # Errors
112
- /// Will result in an error if the reference cannot be found or is not a commit.
113
- pub ( crate ) fn find_commit ( & self , reference : & str ) -> Result < Commit , GitError > {
114
- let repo = self . repository . lock ( ) ;
115
- let git2_reference = repo
116
- . find_reference ( reference)
117
- . map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
118
- Commit :: try_from ( & git2_reference)
119
- }
120
-
121
- pub ( crate ) fn repo_path ( & self ) -> PathBuf {
122
- self . repository . lock ( ) . path ( ) . to_path_buf ( )
123
- }
124
-
125
- pub ( crate ) fn head_id ( & self , head_name : & str ) -> Result < Oid , git2:: Error > {
126
- let repo = self . repository . lock ( ) ;
127
- let ref_name = format ! ( "refs/heads/{head_name}" ) ;
128
- let revision = repo. revparse_single ( ref_name. as_str ( ) ) ?;
129
- Ok ( revision. id ( ) )
130
- }
131
-
132
- pub ( crate ) fn commit_id_from_ref ( & self , reference : & str ) -> Result < Oid , git2:: Error > {
133
- let repo = self . repository . lock ( ) ;
134
- let commit = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
135
- Ok ( commit. id ( ) )
136
- }
137
-
138
- pub ( crate ) fn add_path_to_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
139
- let repo = self . repository . lock ( ) ;
140
- let mut index = repo. index ( ) ?;
141
- index. add_path ( path)
142
- }
143
-
144
- pub ( crate ) fn remove_path_from_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
145
- let repo = self . repository . lock ( ) ;
146
- let mut index = repo. index ( ) ?;
147
- index. remove_path ( path)
148
- }
149
-
150
- pub ( crate ) fn create_commit_on_index (
151
- & self ,
152
- reference : & str ,
153
- author : & Signature < ' _ > ,
154
- committer : & Signature < ' _ > ,
155
- message : & str ,
156
- ) -> Result < ( ) , git2:: Error > {
157
- let repo = self . repository . lock ( ) ;
158
- let tree = repo. find_tree ( repo. index ( ) ?. write_tree ( ) ?) ?;
159
- let head = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
160
- _ = repo. commit ( Some ( "HEAD" ) , author, committer, message, & tree, & [ & head] ) ?;
161
- Ok ( ( ) )
162
- }
163
-
164
- #[ cfg( test) ]
165
- pub ( crate ) fn repository ( & self ) -> Arc < Mutex < git2:: Repository > > {
166
- Arc :: clone ( & self . repository )
167
- }
168
69
}
169
70
170
71
impl From < git2:: Repository > for Repository {
@@ -183,10 +84,112 @@ impl Debug for Repository {
183
84
}
184
85
}
185
86
87
+ #[ cfg( test) ]
88
+ mod tests {
89
+ use std:: {
90
+ path:: { Path , PathBuf } ,
91
+ sync:: Arc ,
92
+ } ;
93
+
94
+ use git2:: { Oid , Signature } ;
95
+ use parking_lot:: Mutex ;
96
+
97
+ use crate :: git:: { Commit , GitError , Reference , Repository , RepositoryLoadKind } ;
98
+
99
+ impl Repository {
100
+ /// Attempt to open an already-existing repository at `path`.
101
+ ///
102
+ /// # Errors
103
+ /// Will result in an error if the repository cannot be opened.
104
+ pub ( crate ) fn open_from_path ( path : & Path ) -> Result < Self , GitError > {
105
+ let repository = git2:: Repository :: open ( path) . map_err ( |e| {
106
+ GitError :: RepositoryLoad {
107
+ kind : RepositoryLoadKind :: Path ,
108
+ cause : e,
109
+ }
110
+ } ) ?;
111
+ Ok ( Self {
112
+ repository : Arc :: new ( Mutex :: new ( repository) ) ,
113
+ } )
114
+ }
115
+
116
+ /// Find a reference by the reference name.
117
+ ///
118
+ /// # Errors
119
+ /// Will result in an error if the reference cannot be found.
120
+ pub ( crate ) fn find_reference ( & self , reference : & str ) -> Result < Reference , GitError > {
121
+ let repo = self . repository . lock ( ) ;
122
+ let git2_reference = repo
123
+ . find_reference ( reference)
124
+ . map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
125
+ Ok ( Reference :: from ( & git2_reference) )
126
+ }
127
+
128
+ /// Find a commit by a reference name.
129
+ ///
130
+ /// # Errors
131
+ /// Will result in an error if the reference cannot be found or is not a commit.
132
+ pub ( crate ) fn find_commit ( & self , reference : & str ) -> Result < Commit , GitError > {
133
+ let repo = self . repository . lock ( ) ;
134
+ let git2_reference = repo
135
+ . find_reference ( reference)
136
+ . map_err ( |e| GitError :: ReferenceNotFound { cause : e } ) ?;
137
+ Commit :: try_from ( & git2_reference)
138
+ }
139
+
140
+ pub ( crate ) fn repo_path ( & self ) -> PathBuf {
141
+ self . repository . lock ( ) . path ( ) . to_path_buf ( )
142
+ }
143
+
144
+ pub ( crate ) fn head_id ( & self , head_name : & str ) -> Result < Oid , git2:: Error > {
145
+ let repo = self . repository . lock ( ) ;
146
+ let ref_name = format ! ( "refs/heads/{head_name}" ) ;
147
+ let revision = repo. revparse_single ( ref_name. as_str ( ) ) ?;
148
+ Ok ( revision. id ( ) )
149
+ }
150
+
151
+ pub ( crate ) fn commit_id_from_ref ( & self , reference : & str ) -> Result < Oid , git2:: Error > {
152
+ let repo = self . repository . lock ( ) ;
153
+ let commit = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
154
+ Ok ( commit. id ( ) )
155
+ }
156
+
157
+ pub ( crate ) fn add_path_to_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
158
+ let repo = self . repository . lock ( ) ;
159
+ let mut index = repo. index ( ) ?;
160
+ index. add_path ( path)
161
+ }
162
+
163
+ pub ( crate ) fn remove_path_from_index ( & self , path : & Path ) -> Result < ( ) , git2:: Error > {
164
+ let repo = self . repository . lock ( ) ;
165
+ let mut index = repo. index ( ) ?;
166
+ index. remove_path ( path)
167
+ }
168
+
169
+ pub ( crate ) fn create_commit_on_index (
170
+ & self ,
171
+ reference : & str ,
172
+ author : & Signature < ' _ > ,
173
+ committer : & Signature < ' _ > ,
174
+ message : & str ,
175
+ ) -> Result < ( ) , git2:: Error > {
176
+ let repo = self . repository . lock ( ) ;
177
+ let tree = repo. find_tree ( repo. index ( ) ?. write_tree ( ) ?) ?;
178
+ let head = repo. find_reference ( reference) ?. peel_to_commit ( ) ?;
179
+ _ = repo. commit ( Some ( "HEAD" ) , author, committer, message, & tree, & [ & head] ) ?;
180
+ Ok ( ( ) )
181
+ }
182
+
183
+ pub ( crate ) fn repository ( & self ) -> Arc < Mutex < git2:: Repository > > {
184
+ Arc :: clone ( & self . repository )
185
+ }
186
+ }
187
+ }
188
+
186
189
// Paths in Windows makes these tests difficult, so disable
187
190
#[ cfg( all( unix, test) ) ]
188
- mod tests {
189
- use std:: env :: set_var ;
191
+ mod unix_tests {
192
+ use std:: path :: Path ;
190
193
191
194
use claims:: { assert_err_eq, assert_ok} ;
192
195
use git2:: { ErrorClass , ErrorCode } ;
0 commit comments