Skip to content

Commit

Permalink
feat: Add shallow clone functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ErickKramer committed May 23, 2024
1 parent 2447fe8 commit 39dfff5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ Usage:
rv import <optional path> [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)
```

Expand Down
16 changes: 9 additions & 7 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,35 @@ 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)

},
}

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)
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
19 changes: 13 additions & 6 deletions test/git_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions utils/git_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 39dfff5

Please sign in to comment.