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:16 UTC

[mynewt-newt] branch master updated (1fc18d6 -> 0fddb7c)

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

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


    from 1fc18d6  Do not enclose syscfg string values in parentheses
     new 395c184  Fix overwriting newtrc settings with default value
     new f796e5a  Add shallow clone option
     new 0fddb7c  Create a shallow copy by default

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 newt/downloader/downloader.go | 35 ++++++++++++++++++++++++++++++-----
 newt/newt.go                  | 19 ++++++++++---------
 newt/settings/settings.go     |  7 +++++++
 newt/ycfg/ycfg.go             | 12 ++++++++++++
 util/util.go                  |  1 +
 5 files changed, 60 insertions(+), 14 deletions(-)

[mynewt-newt] 03/03: Create a shallow copy by default

Posted by an...@apache.org.
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 0fddb7cc083ca9404e9dc92ccaffdcd7dbda0d59
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Mar 14 19:45:21 2022 +0100

    Create a shallow copy by default
    
    First clone will be created as shallow by default, unless explicitly set
    by user.
---
 newt/downloader/downloader.go | 10 ++++++++++
 newt/settings/settings.go     |  2 +-
 newt/ycfg/ycfg.go             | 12 ++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go
index 8ed2927..4876a17 100644
--- a/newt/downloader/downloader.go
+++ b/newt/downloader/downloader.go
@@ -883,6 +883,11 @@ func (gd *GithubDownloader) Clone(commit string, dstPath string) error {
 
 	if util.ShallowCloneDepth > 0 {
 		cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+	} else if util.ShallowCloneDepth < 0 {
+		// default for clone, if not set by user, is to do shallow clone
+		cmd = append(cmd, "--depth", "1")
+		util.OneTimeWarning("Creating a shallow clone by default. " +
+			"To unshallow repository later use \"git fetch --unshallow\".")
 	}
 
 	cmd = append(cmd, url, dstPath)
@@ -978,6 +983,11 @@ func (gd *GitDownloader) Clone(commit string, dstPath string) error {
 
 	if util.ShallowCloneDepth > 0 {
 		cmd = append(cmd, "--depth", strconv.Itoa(util.ShallowCloneDepth))
+	} else if util.ShallowCloneDepth < 0 {
+		// default for clone, if not set by user, is to do shallow clone
+		cmd = append(cmd, "--depth", "1")
+		util.OneTimeWarning("Creating a shallow clone by default. " +
+			"To unshallow repository later use \"git fetch --unshallow\".")
 	}
 
 	cmd = append(cmd, gd.Url, dstPath)
diff --git a/newt/settings/settings.go b/newt/settings/settings.go
index 9b0c5bb..ad577f0 100644
--- a/newt/settings/settings.go
+++ b/newt/settings/settings.go
@@ -55,7 +55,7 @@ func processNewtrc(yc ycfg.YCfg) {
 	}
 
 	// default is 0 anyway, no need to initialize first
-	util.ShallowCloneDepth, _ = yc.GetValInt("shallow_clone", nil)
+	util.ShallowCloneDepth, _ = yc.GetValIntDflt("shallow_clone", nil, -1)
 }
 
 func readNewtrc() ycfg.YCfg {
diff --git a/newt/ycfg/ycfg.go b/newt/ycfg/ycfg.go
index 3d6fa4e..f21129a 100644
--- a/newt/ycfg/ycfg.go
+++ b/newt/ycfg/ycfg.go
@@ -549,6 +549,18 @@ func (yc *YCfg) GetValInt(key string, settings map[string]string) (int, error) {
 	}
 }
 
+// GetValIntDflt retrieves the first entry with the specified key and returns its
+// value coerced to an int.  It returns the specified default if no matching entry
+// is found.  The returned error is a set of warnings just as in `Get`.
+func (yc *YCfg) GetValIntDflt(key string, settings map[string]string, dflt int) (int, error) {
+	entry, ok, getErr := yc.GetFirst(key, settings)
+	if !ok {
+		return dflt, getErr
+	} else {
+		return cast.ToInt(entry.Value), getErr
+	}
+}
+
 // GetValBoolDflt retrieves the first entry with the specified key and returns
 // its value coerced to a bool.  It returns the specified default if no
 // matching entry is found.  The returned error is a set of warnings just as in

[mynewt-newt] 01/03: Fix overwriting newtrc settings with default value

Posted by an...@apache.org.
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 395c184039644fd97fd4eccd4dfb7233208aba01
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Mar 11 15:37:57 2022 +0100

    Fix overwriting newtrc settings with default value
    
    newtrc settings are read properly, but then escape_shell value is
    overwritten anyway by command line setting (or a default value) so it
    was not really working as expected. Now newtrc settings are read at the
    very beginning and then default value or value read from newtrc is used
    as a default value for command line flag so it won't be overwritten.
---
 newt/newt.go              | 17 ++++++++---------
 newt/settings/settings.go |  4 ++++
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/newt/newt.go b/newt/newt.go
index 3eb7d35..76a672a 100644
--- a/newt/newt.go
+++ b/newt/newt.go
@@ -20,6 +20,7 @@
 package main
 
 import (
+	"mynewt.apache.org/newt/newt/settings"
 	"os"
 	"runtime"
 
@@ -28,7 +29,6 @@ import (
 	"github.com/spf13/cobra"
 	"mynewt.apache.org/newt/newt/cli"
 	"mynewt.apache.org/newt/newt/newtutil"
-	"mynewt.apache.org/newt/newt/settings"
 	"mynewt.apache.org/newt/util"
 )
 
@@ -39,7 +39,6 @@ var newtVerbose bool
 var newtLogFile string
 var newtNumJobs int
 var newtHelp bool
-var newtEscapeShellCmds bool
 
 func newtDfltNumJobs() int {
 	maxProcs := runtime.GOMAXPROCS(0)
@@ -81,9 +80,6 @@ func newtCmd() *cobra.Command {
 		Long:    newtHelpText,
 		Example: newtHelpEx,
 		PersistentPreRun: func(cmd *cobra.Command, args []string) {
-			// Ensure .newtrc file gets read.
-			settings.Newtrc()
-
 			verbosity := util.VERBOSITY_DEFAULT
 			if newtSilent {
 				verbosity = util.VERBOSITY_SILENT
@@ -93,8 +89,6 @@ func newtCmd() *cobra.Command {
 				verbosity = util.VERBOSITY_VERBOSE
 			}
 
-			util.EscapeShellCmds = newtEscapeShellCmds
-
 			var err error
 			NewtLogLevel, err = log.ParseLevel(logLevelStr)
 			if err != nil {
@@ -127,8 +121,8 @@ func newtCmd() *cobra.Command {
 		newtDfltNumJobs(), "Number of concurrent build jobs")
 	newtCmd.PersistentFlags().BoolVarP(&newtHelp, "help", "h",
 		false, "Help for newt commands")
-	newtCmd.PersistentFlags().BoolVarP(&newtEscapeShellCmds, "escape", "",
-		runtime.GOOS == "windows", "Apply Windows escapes to shell commands")
+	newtCmd.PersistentFlags().BoolVarP(&util.EscapeShellCmds, "escape", "",
+		util.EscapeShellCmds, "Apply Windows escapes to shell commands")
 
 	versHelpText := cli.FormatHelp(`Display the Newt version number`)
 	versHelpEx := "  newt version"
@@ -148,6 +142,11 @@ func newtCmd() *cobra.Command {
 }
 
 func main() {
+	// Ensure newtrc is read before setting up command flags so we can use
+	// values stored in newtrc as defaults for flags. This way newtrc settings
+	// won't be overwritten by default flag values.
+	settings.Newtrc()
+
 	cmd := newtCmd()
 
 	cli.AddBuildCommands(cmd)
diff --git a/newt/settings/settings.go b/newt/settings/settings.go
index 03d0232..46be3e2 100644
--- a/newt/settings/settings.go
+++ b/newt/settings/settings.go
@@ -22,6 +22,7 @@ package settings
 import (
 	"fmt"
 	"os/user"
+	"runtime"
 	"strconv"
 
 	log "github.com/sirupsen/logrus"
@@ -39,6 +40,9 @@ const NEWTRC_FILENAME string = "newtrc.yml"
 var newtrc *ycfg.YCfg
 
 func processNewtrc(yc ycfg.YCfg) {
+	// set default value
+	util.EscapeShellCmds = runtime.GOOS == "windows"
+
 	s, _ := yc.GetValString("escape_shell", nil)
 	if s != "" {
 		b, err := strconv.ParseBool(s)

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

Posted by an...@apache.org.
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) {