diff --git a/README.md b/README.md index d04c1b3..a562dd9 100644 --- a/README.md +++ b/README.md @@ -89,13 +89,12 @@ Usage: rv import [flags] Flags: - -d, --depth-recursive int Regulates how many levels the recursive dependencies wou -ld be cloned. (default -1) + -d, --depth-recursive int Regulates how many levels the recursive dependencies would be cloned. (default -1) -h, --help help for import -i, --input .repos Path to input .repos file - -r, --recursive .repos Recursively search of other .repos file in the cloned re -positories - -s, --skip-if-existing Skip existing repositories + -r, --recursive .repos Recursively search of other .repos file in the cloned repositories + -l, --shallow Clone repositories with a depth of 1 + -s, --skip Skip existing repositories -w, --workers int Number of concurrent workers to use (default 8) ``` diff --git a/cmd/import.go b/cmd/import.go index eeba139..3494138 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -33,18 +33,19 @@ import cycle.`, filePath, _ := cmd.Flags().GetString("input") recursiveFlag, _ := cmd.Flags().GetBool("recursive") skipExisting, _ := cmd.Flags().GetBool("skip") + shallowClone, _ := cmd.Flags().GetBool("shallowClone") depthRecursive, _ := cmd.Flags().GetInt("depth-recursive") numWorkers, _ := cmd.Flags().GetInt("workers") // Import repository files in the given file - validFile := singleCloneSweep(cloningPath, filePath, numWorkers, skipExisting) + validFile := singleCloneSweep(cloningPath, filePath, numWorkers, skipExisting, shallowClone) if !validFile { os.Exit(1) } if !recursiveFlag { os.Exit(0) } - nestedImportClones(cloningPath, filePath, depthRecursive, numWorkers, skipExisting) + nestedImportClones(cloningPath, filePath, depthRecursive, numWorkers, skipExisting, shallowClone) }, } @@ -52,14 +53,15 @@ import cycle.`, func init() { rootCmd.AddCommand(importCmd) + importCmd.Flags().IntP("depth-recursive", "d", -1, "Regulates how many levels the recursive dependencies would be cloned.") importCmd.Flags().StringP("input", "i", "", "Path to input `.repos` file") importCmd.Flags().BoolP("recursive", "r", false, "Recursively search of other `.repos` file in the cloned repositories") - importCmd.Flags().IntP("depth-recursive", "d", -1, "Regulates how many levels the recursive dependencies would be cloned.") importCmd.Flags().BoolP("skip", "s", false, "Skip existing repositories") + importCmd.Flags().BoolP("shallow", "l", false, "Clone repositories with a depth of 1") importCmd.Flags().IntP("workers", "w", 8, "Number of concurrent workers to use") } -func singleCloneSweep(root string, filePath string, numWorkers int, skipExisting bool) bool { +func singleCloneSweep(root string, filePath string, numWorkers int, skipExisting bool, shallowClone bool) bool { utils.PrintSection(fmt.Sprintf("Importing from %s", filePath)) utils.PrintSeparator() config, err := utils.ParseReposFile(filePath) @@ -82,7 +84,7 @@ func singleCloneSweep(root string, filePath string, numWorkers int, skipExisting utils.PrintErrorMsg("Unsupported repository type.\n") results <- false } else { - success := utils.PrintGitClone(job.Repo.URL, job.Repo.Version, job.DirName, skipExisting, false) + success := utils.PrintGitClone(job.Repo.URL, job.Repo.Version, job.DirName, skipExisting, shallowClone, false) results <- success } } @@ -111,7 +113,7 @@ func singleCloneSweep(root string, filePath string, numWorkers int, skipExisting return validFile } -func nestedImportClones(cloningPath string, initialFilePath string, depthRecursive int, numWorkers int, skipExisting bool) { +func nestedImportClones(cloningPath string, initialFilePath string, depthRecursive int, numWorkers int, skipExisting bool, shallowClone bool) { // Recursively import .repos files found clonedReposFiles := map[string]bool{initialFilePath: true} validFiles := true @@ -133,7 +135,7 @@ func nestedImportClones(cloningPath string, initialFilePath string, depthRecursi newReposFileFound := false for _, filePathToClone := range foundReposFiles { if _, ok := clonedReposFiles[filePathToClone]; !ok { - validFiles = singleCloneSweep(cloningPath, filePathToClone, numWorkers, skipExisting) + validFiles = singleCloneSweep(cloningPath, filePathToClone, numWorkers, skipExisting, shallowClone) clonedReposFiles[filePathToClone] = true newReposFileFound = true if !validFiles { diff --git a/test/git_helpers_test.go b/test/git_helpers_test.go index 4fb86bf..c39d4f1 100644 --- a/test/git_helpers_test.go +++ b/test/git_helpers_test.go @@ -78,7 +78,7 @@ func TestGitStatus(t *testing.T) { } func TestGitPull(t *testing.T) { - if utils.GitClone("https://github.com/ros2/demos.git", "rolling", "/tmp/testdata/demos", false, false) != utils.SuccessfullClone { + if utils.GitClone("https://github.com/ros2/demos.git", "rolling", "/tmp/testdata/demos", false, false, false) != utils.SuccessfullClone { t.Errorf("Expected to successfully clone git repository") } msg := utils.PullGitRepo("/tmp/testdata/demos") @@ -107,23 +107,30 @@ func TestCheckGitUrl(t *testing.T) { } func TestCloneGitRepo(t *testing.T) { - if utils.GitClone("https://github.com/ros2/demos.git", "rolling", "/tmp/testdata/demos", false, false) != utils.SuccessfullClone { + if utils.GitClone("https://github.com/ros2/demos.git", "rolling", "/tmp/testdata/demos", false, false, false) != utils.SuccessfullClone { t.Errorf("Expected to successfully clone git repository") } - if utils.GitClone("https://github.com/ros2/ros2cli", "", "/tmp/testdata/ros2cli", false, false) != utils.SuccessfullClone { + if utils.GitClone("https://github.com/ros2/ros2cli", "", "/tmp/testdata/ros2cli", false, false, false) != utils.SuccessfullClone { t.Errorf("Expected to successfully clone git repository") } - if utils.GitClone("https://github.com/ros2/sadasdasd.git", "", "/tmp/testdata/sdasda", false, false) != utils.FailedClone { + if utils.GitClone("https://github.com/ros2/sadasdasd.git", "", "/tmp/testdata/sdasda", false, false, false) != utils.FailedClone { t.Errorf("Expected to fail to clone git repository") } - if utils.GitClone("https://github.com/ros2/demos.git", "", "/tmp/testdata/demos", true, false) != utils.SkippedClone { + if utils.GitClone("https://github.com/ros2/demos.git", "", "/tmp/testdata/demos", true, false, false) != utils.SkippedClone { t.Errorf("Expected to skip to clone git repository") } + if utils.GitClone("https://github.com/ros2/demos.git", "", "/tmp/testdata/demos", false, true, false) != utils.SuccessfullClone { + t.Errorf("Expected to successfully to clone git repository with shallow enabled") + } + count, err := utils.RunGitCmd("/tmp/testdata/demos", "rev-list", nil, []string{"--all", "--count"}...) + if err != nil || strings.TrimSpace(count) != "1" { + t.Errorf("Expected to have a shallow clone of the git repository") + } } func TestGitSwitch(t *testing.T) { repoPath := "/tmp/testdata/switch_test" - if utils.GitClone("https://github.com/ros2/demos.git", "rolling", repoPath, false, false) != utils.SuccessfullClone { + if utils.GitClone("https://github.com/ros2/demos.git", "rolling", repoPath, false, false, false) != utils.SuccessfullClone { t.Errorf("Expected to successfully clone git repository") } output, err := utils.GitSwitch(repoPath, "humble", false, false) diff --git a/utils/git_helpers.go b/utils/git_helpers.go index 0f1847d..f1283e7 100644 --- a/utils/git_helpers.go +++ b/utils/git_helpers.go @@ -150,7 +150,7 @@ func GitSwitch(path string, branch string, createBranch bool, detachHead bool) ( return output, nil } -func GitClone(url string, version string, clonePath string, skipIfExisting bool, enablePrompt bool) int { +func GitClone(url string, version string, clonePath string, skipIfExisting bool, shallowClone bool, enablePrompt bool) int { // Check if clonePath exists if _, err := os.Stat(clonePath); err == nil { @@ -178,6 +178,10 @@ func GitClone(url string, version string, clonePath string, skipIfExisting bool, } else { cmdArgs = []string{url, "--branch", version, clonePath} } + + if shallowClone { + cmdArgs = append(cmdArgs, "--depth", "1") + } _, err := RunGitCmd(".", "clone", envConfig, cmdArgs...) if err != nil { return FailedClone @@ -227,10 +231,10 @@ func PrintCheckGit(path string, url string, version string, enablePrompt bool) b return isURLValid } -func PrintGitClone(url string, version string, path string, skipIfExisting bool, enablePrompt bool) bool { +func PrintGitClone(url string, version string, path string, skipIfExisting bool, shallowClone bool, enablePrompt bool) bool { var cloneMsg string var cloneSuccessful bool - statusClone := GitClone(url, version, path, skipIfExisting, enablePrompt) + statusClone := GitClone(url, version, path, skipIfExisting, shallowClone, enablePrompt) switch statusClone { case SuccessfullClone: cloneMsg = fmt.Sprintf("Successfully cloned git repository '%s' with version '%s'\n", url, version)