44//! The `clone` subcommand.
55
66use clap:: { Arg , ArgMatches , Command } ;
7- use futures:: future:: join_all;
87use std:: path:: Path ;
98use std:: process:: Command as SysCommand ;
109use tokio:: runtime:: Runtime ;
1110
1211use crate :: config;
1312use crate :: config:: { Locked , LockedSource } ;
1413use crate :: error:: * ;
15- use crate :: sess:: { Session , SessionIo } ;
14+ use crate :: sess:: { DependencySource , Session , SessionIo } ;
1615
1716/// Assemble the `clone` subcommand.
1817pub fn new ( ) -> Command {
@@ -37,7 +36,7 @@ pub fn new() -> Command {
3736/// Execute the `clone` subcommand.
3837pub fn run ( sess : & Session , path : & Path , matches : & ArgMatches ) -> Result < ( ) > {
3938 let dep = & matches. get_one :: < String > ( "name" ) . unwrap ( ) . to_lowercase ( ) ;
40- sess. dependency_with_name ( dep) ?;
39+ let depref = sess. dependency_with_name ( dep) ?;
4140
4241 let path_mod = matches. get_one :: < String > ( "path" ) . unwrap ( ) ; // TODO make this option for config in the Bender.yml file?
4342
@@ -57,6 +56,17 @@ pub fn run(sess: &Session, path: &Path, matches: &ArgMatches) -> Result<()> {
5756 }
5857 }
5958
59+ // Check if dependency is a git dependency
60+ match sess. dependency_source ( depref) {
61+ DependencySource :: Git { .. } | DependencySource :: Registry => { }
62+ DependencySource :: Path { .. } => {
63+ Err ( Error :: new ( format ! (
64+ "Dependency `{}` is not a git dependency, cannot clone." ,
65+ dep
66+ ) ) ) ?;
67+ }
68+ }
69+
6070 // Create dir
6171 if !path. join ( path_mod) . exists ( )
6272 && !SysCommand :: new ( "mkdir" )
@@ -77,35 +87,22 @@ pub fn run(sess: &Session, path: &Path, matches: &ArgMatches) -> Result<()> {
7787 let rt = Runtime :: new ( ) ?;
7888 let io = SessionIo :: new ( sess) ;
7989
80- let ids = matches
81- . get_many :: < String > ( "name" )
82- . unwrap ( )
83- . map ( |n| Ok ( ( n, sess. dependency_with_name ( n) ?) ) )
84- . collect :: < Result < Vec < _ > > > ( ) ?;
85- debugln ! ( "main: obtain checkouts {:?}" , ids) ;
86- let checkouts = rt
87- . block_on ( join_all (
88- ids. iter ( )
89- . map ( |& ( _, id) | io. checkout ( id, false , & [ ] ) )
90- . collect :: < Vec < _ > > ( ) ,
91- ) )
92- . into_iter ( )
93- . collect :: < Result < Vec < _ > > > ( ) ?;
94- debugln ! ( "main: checkouts {:#?}" , checkouts) ;
95- for c in checkouts {
96- if let Some ( s) = c. to_str ( ) {
97- if !Path :: new ( s) . exists ( ) {
98- Err ( Error :: new ( format ! ( "`{dep}` path `{s}` does not exist" ) ) ) ?;
99- }
100- let command = SysCommand :: new ( "cp" )
101- . arg ( "-rf" )
102- . arg ( s)
103- . arg ( path. join ( path_mod) . join ( dep) . to_str ( ) . unwrap ( ) )
104- . status ( ) ;
105- if !command. unwrap ( ) . success ( ) {
106- Err ( Error :: new ( format ! ( "Copying {} failed" , dep, ) ) ) ?;
107- }
108- // println!("{:?}", command);
90+ let id =
91+ sess. dependency_with_name ( & matches. get_one :: < String > ( "name" ) . unwrap ( ) . to_lowercase ( ) ) ?;
92+ debugln ! ( "main: obtain checkout {:?}" , id) ;
93+ let checkout = rt. block_on ( io. checkout ( id, false , & [ ] ) ) ?;
94+ debugln ! ( "main: checkout {:#?}" , checkout) ;
95+ if let Some ( s) = checkout. to_str ( ) {
96+ if !Path :: new ( s) . exists ( ) {
97+ Err ( Error :: new ( format ! ( "`{dep}` path `{s}` does not exist" ) ) ) ?;
98+ }
99+ let command = SysCommand :: new ( "cp" )
100+ . arg ( "-rf" )
101+ . arg ( s)
102+ . arg ( path. join ( path_mod) . join ( dep) . to_str ( ) . unwrap ( ) )
103+ . status ( ) ;
104+ if !command. unwrap ( ) . success ( ) {
105+ Err ( Error :: new ( format ! ( "Copying {} failed" , dep, ) ) ) ?;
109106 }
110107 }
111108
@@ -179,7 +176,7 @@ pub fn run(sess: &Session, path: &Path, matches: &ArgMatches) -> Result<()> {
179176 let mut new_str = String :: new ( ) ;
180177 if local_file_str. contains ( "overrides:" ) {
181178 let split = local_file_str. split ( '\n' ) ;
182- let test = split. clone ( ) . last ( ) . unwrap ( ) . is_empty ( ) ;
179+ let test = split. clone ( ) . next_back ( ) . unwrap ( ) . is_empty ( ) ;
183180 for i in split {
184181 if i. contains ( dep) {
185182 new_str. push ( '#' ) ;
0 commit comments