@@ -139,6 +139,25 @@ impl<'repo> Submodule<'repo> {
139
139
Ok ( ( ) )
140
140
}
141
141
142
+ /// Set up the subrepository for a submodule in preparation for clone.
143
+ ///
144
+ /// This function can be called to init and set up a submodule repository
145
+ /// from a submodule in preparation to clone it from its remote.
146
+
147
+ /// use_gitlink: Should the workdir contain a gitlink to the repo in
148
+ /// .git/modules vs. repo directly in workdir.
149
+ pub fn repo_init ( & mut self , use_gitlink : bool ) -> Result < Repository , Error > {
150
+ unsafe {
151
+ let mut raw_repo = ptr:: null_mut ( ) ;
152
+ try_call ! ( raw:: git_submodule_repo_init(
153
+ & mut raw_repo,
154
+ self . raw,
155
+ use_gitlink
156
+ ) ) ;
157
+ Ok ( Binding :: from_raw ( raw_repo) )
158
+ }
159
+ }
160
+
142
161
/// Open the repository for a submodule.
143
162
///
144
163
/// This will only work if the submodule is checked out into the working
@@ -399,16 +418,54 @@ mod tests {
399
418
let ( _td, parent) = crate :: test:: repo_init ( ) ;
400
419
401
420
let url1 = Url :: from_file_path ( & repo1. workdir ( ) . unwrap ( ) ) . unwrap ( ) ;
402
- let url3 = Url :: from_file_path ( & repo2. workdir ( ) . unwrap ( ) ) . unwrap ( ) ;
421
+ let url2 = Url :: from_file_path ( & repo2. workdir ( ) . unwrap ( ) ) . unwrap ( ) ;
403
422
let mut s1 = parent
404
423
. submodule ( & url1. to_string ( ) , Path :: new ( "bar" ) , true )
405
424
. unwrap ( ) ;
406
425
let mut s2 = parent
407
- . submodule ( & url3 . to_string ( ) , Path :: new ( "bar2" ) , true )
426
+ . submodule ( & url2 . to_string ( ) , Path :: new ( "bar2" ) , true )
408
427
. unwrap ( ) ;
409
428
// -----------------------------------
410
429
411
430
t ! ( s1. clone( Some ( & mut SubmoduleUpdateOptions :: default ( ) ) ) ) ;
412
431
t ! ( s2. clone( None ) ) ;
413
432
}
433
+
434
+ #[ test]
435
+ fn repo_init_submodule ( ) {
436
+ // -----------------------------------
437
+ // Same as `clone_submodule()`
438
+ let ( _td, child) = crate :: test:: repo_init ( ) ;
439
+ let ( _td, parent) = crate :: test:: repo_init ( ) ;
440
+
441
+ let url_child = Url :: from_file_path ( & child. workdir ( ) . unwrap ( ) ) . unwrap ( ) ;
442
+ let url_parent = Url :: from_file_path ( & parent. workdir ( ) . unwrap ( ) ) . unwrap ( ) ;
443
+ let mut sub = parent
444
+ . submodule ( & url_child. to_string ( ) , Path :: new ( "bar" ) , true )
445
+ . unwrap ( ) ;
446
+
447
+ // -----------------------------------
448
+ // Let's commit the submodule for later clone
449
+ t ! ( sub. clone( None ) ) ;
450
+ t ! ( sub. add_to_index( true ) ) ;
451
+ t ! ( sub. add_finalize( ) ) ;
452
+
453
+ crate :: test:: commit ( & parent) ;
454
+
455
+ // Clone the parent to init its submodules
456
+ let td = TempDir :: new ( ) . unwrap ( ) ;
457
+ let new_parent = Repository :: clone ( & url_parent. to_string ( ) , & td) . unwrap ( ) ;
458
+
459
+ let mut submodules = new_parent. submodules ( ) . unwrap ( ) ;
460
+ let child = submodules. first_mut ( ) . unwrap ( ) ;
461
+
462
+ // First init child
463
+ t ! ( child. init( false ) ) ;
464
+ assert_eq ! ( child. url( ) . unwrap( ) , url_child. as_str( ) ) ;
465
+
466
+ // open() is not possible before initializing the repo
467
+ assert ! ( child. open( ) . is_err( ) ) ;
468
+ t ! ( child. repo_init( true ) ) ;
469
+ assert ! ( child. open( ) . is_ok( ) ) ;
470
+ }
414
471
}
0 commit comments