1+ use std:: time:: UNIX_EPOCH ;
2+
13use chrono:: { DateTime , Local , TimeZone as _} ;
24
35use crate :: {
@@ -6,7 +8,7 @@ use crate::{
68} ;
79
810/// Represents a commit.
9- #[ derive( Debug , PartialEq , Eq ) ]
11+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
1012pub ( crate ) struct Commit {
1113 pub ( crate ) hash : String ,
1214 pub ( crate ) reference : Option < Reference > ,
@@ -19,6 +21,20 @@ pub(crate) struct Commit {
1921}
2022
2123impl Commit {
24+ #[ must_use]
25+ pub ( crate ) fn empty ( ) -> Self {
26+ Self {
27+ hash : String :: from ( "0000000000000000000000000000000000000000" ) ,
28+ reference : None ,
29+ author : User :: new ( None , None ) ,
30+ authored_date : None ,
31+ message : None ,
32+ committer : None ,
33+ committed_date : DateTime :: from ( UNIX_EPOCH ) ,
34+ summary : None ,
35+ }
36+ }
37+
2238 /// Get the hash of the commit
2339 #[ must_use]
2440 pub ( crate ) fn hash ( & self ) -> & str {
@@ -131,6 +147,34 @@ mod tests {
131147 with_temp_repository,
132148 } ;
133149
150+ impl Commit {
151+ pub ( crate ) fn new_with_hash ( hash : & str ) -> Self {
152+ Self {
153+ hash : String :: from ( hash) ,
154+ reference : None ,
155+ author : User :: new ( None , None ) ,
156+ authored_date : None ,
157+ message : None ,
158+ committer : None ,
159+ committed_date : DateTime :: from ( UNIX_EPOCH ) ,
160+ summary : None ,
161+ }
162+ }
163+ }
164+
165+ #[ test]
166+ fn empty ( ) {
167+ let commit = Commit :: empty ( ) ;
168+ assert_eq ! ( commit. hash( ) , "0000000000000000000000000000000000000000" ) ;
169+ assert_none ! ( commit. reference( ) ) ;
170+ assert_eq ! ( commit. author( ) , & User :: new( None , None ) ) ;
171+ assert_none ! ( commit. authored_date( ) ) ;
172+ assert_none ! ( commit. message( ) ) ;
173+ assert_none ! ( commit. committer( ) ) ;
174+ assert_eq ! ( commit. committed_date( ) . timestamp( ) , 0 ) ;
175+ assert_none ! ( commit. summary( ) ) ;
176+ }
177+
134178 #[ test]
135179 fn hash ( ) {
136180 let commit = CommitBuilder :: new ( "0123456789ABCDEF" ) . build ( ) ;
@@ -180,26 +224,25 @@ mod tests {
180224 #[ test]
181225 fn new_authored_date_same_committed_date ( ) {
182226 with_temp_repository ( |repository| {
183- let repo = crate :: git:: Repository :: from ( repository) ;
184- create_commit ( & repo, Some ( CreateCommitOptions :: new ( ) . author_time ( JAN_2021_EPOCH ) ) ) ;
185- let commit = repo. find_commit ( "refs/heads/main" ) . unwrap ( ) ;
227+ let commit = create_commit (
228+ & repository,
229+ Some ( CreateCommitOptions :: new ( ) . author_time ( JAN_2021_EPOCH ) ) ,
230+ ) ;
186231 assert_none ! ( commit. authored_date( ) ) ;
187232 } ) ;
188233 }
189234
190235 #[ test]
191236 fn new_authored_date_different_than_committed ( ) {
192237 with_temp_repository ( |repository| {
193- let repo = crate :: git:: Repository :: from ( repository) ;
194- create_commit (
195- & repo,
238+ let commit = create_commit (
239+ & repository,
196240 Some (
197241 CreateCommitOptions :: new ( )
198242 . commit_time ( JAN_2021_EPOCH )
199243 . author_time ( JAN_2021_EPOCH + 1 ) ,
200244 ) ,
201245 ) ;
202- let commit = repo. find_commit ( "refs/heads/main" ) . unwrap ( ) ;
203246 assert_some_eq ! (
204247 commit. authored_date( ) ,
205248 & DateTime :: parse_from_rfc3339( "2021-01-01T00:00:01Z" ) . unwrap( )
@@ -210,9 +253,7 @@ mod tests {
210253 #[ test]
211254 fn new_committer_different_than_author ( ) {
212255 with_temp_repository ( |repository| {
213- let repo = crate :: git:: Repository :: from ( repository) ;
214- create_commit ( & repo, Some ( CreateCommitOptions :: new ( ) . committer ( "Committer" ) ) ) ;
215- let commit = repo. find_commit ( "refs/heads/main" ) . unwrap ( ) ;
256+ let commit = create_commit ( & repository, Some ( CreateCommitOptions :: new ( ) . committer ( "Committer" ) ) ) ;
216257 assert_some_eq ! (
217258 commit. committer( ) ,
218259 & User :: new
( Some ( "Committer" ) , Some ( "[email protected] " ) ) @@ -223,8 +264,7 @@ mod tests {
223264 #[ test]
224265 fn new_committer_same_as_author ( ) {
225266 with_temp_repository ( |repository| {
226- let repo = crate :: git:: Repository :: from ( repository) ;
227- let commit = repo. find_commit ( "refs/heads/main" ) . unwrap ( ) ;
267+ let commit = create_commit ( & repository, None ) ;
228268 assert_none ! ( commit. committer( ) ) ;
229269 } ) ;
230270 }
0 commit comments