@@ -384,71 +384,95 @@ fn get_cumulative_pr_files(
384384pub fn checkout (
385385 clone_url : & str ,
386386 clone_path : & str ,
387- base_branch : Option < & str > ,
387+ branch : Option < & str > ,
388388 pr_branch : Option < & str > ,
389389) -> Result < ( ) , Box < dyn std:: error:: Error > > {
390- // Clone the repository
390+ // Clone the repository; use the specified branch or default branch if `branch` is None
391391 let mut clone_cmd = Command :: new ( "git" ) ;
392392 clone_cmd. arg ( "clone" ) . arg ( clone_url) . arg ( clone_path) ;
393-
394- if let Some ( branch) = base_branch {
395- clone_cmd. arg ( "--branch" ) . arg ( branch) ;
393+ if let Some ( branch_name) = branch {
394+ clone_cmd. arg ( "--branch" ) . arg ( branch_name) ;
396395 }
397-
398396 let output = clone_cmd. output ( ) ?;
399397 if !output. status . success ( ) {
400398 let error_msg = String :: from_utf8_lossy ( & output. stderr ) ;
401399 return Err ( format ! ( "Failed to clone repository: {}" , error_msg) . into ( ) ) ;
402400 }
403401
402+ // Set the working directory to the cloned path
404403 let cloned_path = Path :: new ( clone_path) . canonicalize ( ) ?;
405404 env:: set_current_dir ( & cloned_path) ?;
406405
407- // Get the list of changed files
408- let changed_files = match ( base_branch, pr_branch) {
409- ( Some ( base) , Some ( pr) ) => {
410- let fetch_output = Command :: new ( "git" )
411- . args ( & [ "fetch" , "origin" , pr] )
412- . output ( ) ?;
413- if !fetch_output. status . success ( ) {
414- let error_msg = String :: from_utf8_lossy ( & fetch_output. stderr ) ;
415- return Err ( format ! ( "Failed to fetch PR branch: {}" , error_msg) . into ( ) ) ;
416- }
417- get_cumulative_pr_files ( Some ( base) , Some ( & format ! ( "origin/{}" , pr) ) ) ?
418- }
419- ( Some ( base) , None ) => {
420- let output = Command :: new ( "git" )
421- . args ( & [ "ls-tree" , "-r" , "--name-only" , base] )
422- . output ( ) ?;
423- if !output. status . success ( ) {
424- let error_msg = String :: from_utf8_lossy ( & output. stderr ) ;
425- return Err ( format ! ( "Failed to list files in base branch: {}" , error_msg) . into ( ) ) ;
426- }
406+ // Configure Git user for commits in this repository
407+ Command :: new ( "git" )
408+ . args ( & [ "config" , "user.email" , "[email protected] " ] ) 409+ . output ( ) ?;
410+ Command :: new ( "git" )
411+ . args ( & [ "config" , "user.name" , "CI Bot" ] )
412+ . output ( ) ?;
427413
428- String :: from_utf8_lossy ( & output. stdout )
429- . lines ( )
430- . map ( String :: from)
431- . collect ( )
414+ // If a pr_branch is provided, fetch it as a local branch and compare with the base branch
415+ if let Some ( pr_branch_name) = pr_branch {
416+ // Fetch the PR branch and create a local branch
417+ let fetch_output = Command :: new ( "git" )
418+ . args ( & [
419+ "fetch" ,
420+ "origin" ,
421+ & format ! ( "{}:{}" , pr_branch_name, pr_branch_name) ,
422+ ] )
423+ . output ( ) ?;
424+ if !fetch_output. status . success ( ) {
425+ let error_msg = String :: from_utf8_lossy ( & fetch_output. stderr ) ;
426+ return Err ( format ! (
427+ "Failed to fetch PR branch '{}': {}" ,
428+ pr_branch_name, error_msg
429+ )
430+ . into ( ) ) ;
432431 }
433- ( None , _) => {
434- return Err ( "At least base_branch must be specified." . into ( ) ) ;
432+
433+ // Perform a diff between `branch` (or the default branch) and `pr_branch`
434+ let base_branch = branch. unwrap_or ( "HEAD" ) ;
435+ let diff_output = Command :: new ( "git" )
436+ . args ( & [ "diff" , "--name-only" , base_branch, pr_branch_name] )
437+ . output ( ) ?;
438+
439+ if !diff_output. status . success ( ) {
440+ let error_msg = String :: from_utf8_lossy ( & diff_output. stderr ) ;
441+ return Err ( format ! ( "Failed to diff branches: {}" , error_msg) . into ( ) ) ;
435442 }
436- } ;
437443
438- let mut file_commit_map: HashMap < String , String > = HashMap :: new ( ) ;
439- for file in & changed_files {
440- file_commit_map. insert ( file. clone ( ) , "PR-final" . to_string ( ) ) ;
441- }
444+ // Parse the diff output
445+ let changed_files = String :: from_utf8_lossy ( & diff_output. stdout )
446+ . lines ( )
447+ . map ( String :: from)
448+ . collect :: < Vec < String > > ( ) ;
442449
443- println ! ( "Changed files:\n {:?}" , changed_files) ;
450+ println ! (
451+ "Changed files in PR branch '{}': {:?}" ,
452+ pr_branch_name, changed_files
453+ ) ;
454+ } else {
455+ // If no PR branch, list all files in the base branch
456+ let list_output = Command :: new ( "git" )
457+ . args ( & [ "ls-tree" , "-r" , "--name-only" , "HEAD" ] )
458+ . output ( ) ?;
444459
445- // Now proceed with deletion based on the changed files
446- let files_str = changed_files. join ( "\n " ) ;
447- delete_except ( & files_str, & cloned_path) ?;
460+ if !list_output. status . success ( ) {
461+ let error_msg = String :: from_utf8_lossy ( & list_output. stderr ) ;
462+ return Err ( format ! ( "Failed to list files in base branch: {}" , error_msg) . into ( ) ) ;
463+ }
448464
449- delete_empty_directories ( & cloned_path) ?;
465+ let files = String :: from_utf8_lossy ( & list_output. stdout )
466+ . lines ( )
467+ . map ( String :: from)
468+ . collect :: < Vec < String > > ( ) ;
450469
451- save_commit_map ( & file_commit_map) ?;
470+ println ! (
471+ "Files in branch '{}': {:?}" ,
472+ branch. unwrap_or( "default branch" ) ,
473+ files
474+ ) ;
475+ }
452476
453477 Ok ( ( ) )
454478}
0 commit comments