You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2022/03/17 13:20:18 UTC

[mynewt-newt] 02/03: Add shallow clone option

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newt.git

commit f796e5a38c1582f07fe0e2c363b1af82ee6aa0bd
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Mar 11 15:36:33 2022 +0100

    Add shallow clone option
    
    This adds "--shallow <depth>" command line option and corresponding
    "shallow_clone: <depth>" option to enable shallow copies for git
    repositories.
    
    If set to "0" (default), no shallow copy will be performed. Any other
    positive value will clone up to that number of commits of history. This
    effectively adds "--depth" option to "git clone" and "git fetch"
    commands.
---
 newt/downloader/downloader.go | 25 ++++++++++++++++++++-----
 newt/newt.go                  |  2 ++
 newt/settings/settings.go     |  3 +++
 util/util.go                  |  1 +
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go
index 9dd6d05..8ed2927 100644
--- a/newt/downloader/downloader.go
+++ b/newt/downloader/downloader.go
@@ -766,6 +766,9 @@ func (gd *GithubDownloader) Fetch(repoDir string) error {
 			gd.Repo)
 
 		cmd := []string{"fetch", "--tags"}
+		if util.ShallowCloneDepth > 0 {
+			cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+		}
 		_, err := gd.authenticatedCommand(repoDir, cmd)
 		return err
 	})
@@ -876,10 +879,14 @@ func (gd *GithubDownloader) Clone(commit string, dstPath string) error {
 		"clone",
 		"-b",
 		branch,
-		url,
-		dstPath,
 	}
 
+	if util.ShallowCloneDepth > 0 {
+		cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+	}
+
+	cmd = append(cmd, url, dstPath)
+
 	if util.Verbosity >= util.VERBOSITY_VERBOSE {
 		err = util.ShellInteractiveCommand(cmd, nil, false)
 	} else {
@@ -927,7 +934,11 @@ func NewGithubDownloader() *GithubDownloader {
 
 func (gd *GitDownloader) Fetch(repoDir string) error {
 	return gd.cachedFetch(func() error {
-		_, err := executeGitCommand(repoDir, []string{"fetch", "--tags"}, true)
+		cmd := []string{"fetch", "--tags"}
+		if util.ShallowCloneDepth > 0 {
+			cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+		}
+		_, err := executeGitCommand(repoDir, cmd, true)
 		return err
 	})
 }
@@ -963,10 +974,14 @@ func (gd *GitDownloader) Clone(commit string, dstPath string) error {
 		"clone",
 		"-b",
 		branch,
-		gd.Url,
-		dstPath,
 	}
 
+	if util.ShallowCloneDepth > 0 {
+		cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+	}
+
+	cmd = append(cmd, gd.Url, dstPath)
+
 	if util.Verbosity >= util.VERBOSITY_VERBOSE {
 		err = util.ShellInteractiveCommand(cmd, nil, false)
 	} else {
diff --git a/newt/newt.go b/newt/newt.go
index 76a672a..676a5c7 100644
--- a/newt/newt.go
+++ b/newt/newt.go
@@ -123,6 +123,8 @@ func newtCmd() *cobra.Command {
 		false, "Help for newt commands")
 	newtCmd.PersistentFlags().BoolVarP(&util.EscapeShellCmds, "escape", "",
 		util.EscapeShellCmds, "Apply Windows escapes to shell commands")
+	newtCmd.PersistentFlags().IntVarP(&util.ShallowCloneDepth, "shallow", "",
+		util.ShallowCloneDepth, "Use shallow clone for git repositories up to specified number of commits")
 
 	versHelpText := cli.FormatHelp(`Display the Newt version number`)
 	versHelpEx := "  newt version"
diff --git a/newt/settings/settings.go b/newt/settings/settings.go
index 46be3e2..9b0c5bb 100644
--- a/newt/settings/settings.go
+++ b/newt/settings/settings.go
@@ -53,6 +53,9 @@ func processNewtrc(yc ycfg.YCfg) {
 			util.EscapeShellCmds = b
 		}
 	}
+
+	// default is 0 anyway, no need to initialize first
+	util.ShallowCloneDepth, _ = yc.GetValInt("shallow_clone", nil)
 }
 
 func readNewtrc() ycfg.YCfg {
diff --git a/util/util.go b/util/util.go
index 6d8abbb..95817df 100644
--- a/util/util.go
+++ b/util/util.go
@@ -47,6 +47,7 @@ var PrintShellCmds bool
 var InjectSyscfg string
 var ExecuteShell bool
 var EscapeShellCmds bool
+var ShallowCloneDepth int
 var logFile *os.File
 
 func ParseEqualsPair(v string) (string, string, error) {