You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ut...@apache.org on 2021/05/04 21:14:46 UTC

[mynewt-newt] branch master updated: Allow working on repos that lack a "master" branch

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fd9df1e  Allow working on repos that lack a "master" branch
fd9df1e is described below

commit fd9df1ed93ac1692d0b2bbc96b43f5bef81d14c8
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Tue May 4 07:40:59 2021 -0300

    Allow working on repos that lack a "master" branch
    
    Add a new key option `branch:` that can be configured for git/github
    downloaders, so a user can override the default of "master", and
    `newt` is gonna look for the yaml files on the given branch, eg:
    
    ```
    repository.my_development_repo:
        type: git
        url: git@github.com:my_org/my_development_repo.git
        branch: dev
        vers: 0-dev
    ```
    
    This will clone the repo using "dev" as the default branch.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 newt/downloader/downloader.go | 38 ++++++++++++++++++++++++++++++++------
 newt/repo/repo.go             |  5 +++--
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go
index 2230db1..9dd6d05 100644
--- a/newt/downloader/downloader.go
+++ b/newt/downloader/downloader.go
@@ -93,6 +93,10 @@ type Downloader interface {
 	//
 	// If such a commit exists, it is returned.  Otherwise, "" is returned.
 	LatestRc(path string, base string) (string, error)
+
+	// Returns the branch that contains the YAML control files; this option
+	// allows implementers to override "master" as the main branch.
+	MainBranch() string
 }
 
 type Commit struct {
@@ -117,6 +121,7 @@ type GithubDownloader struct {
 	Server string
 	User   string
 	Repo   string
+	Branch string
 
 	// Login for private repos.
 	Login string
@@ -131,7 +136,8 @@ type GithubDownloader struct {
 
 type GitDownloader struct {
 	GenericDownloader
-	Url string
+	Url    string
+	Branch string
 }
 
 type LocalDownloader struct {
@@ -851,8 +857,7 @@ func (gd *GithubDownloader) setRemoteAuth(path string) error {
 }
 
 func (gd *GithubDownloader) Clone(commit string, dstPath string) error {
-	// Currently only the master branch is supported.
-	branch := "master"
+	branch := gd.MainBranch()
 
 	url, publicUrl := gd.remoteUrls()
 
@@ -908,6 +913,14 @@ func (gd *GithubDownloader) FixupOrigin(path string) error {
 	return gd.setOriginUrl(path, publicUrl)
 }
 
+func (gd *GithubDownloader) MainBranch() string {
+	if gd.Branch != "" {
+		return gd.Branch
+	} else {
+		return "master"
+	}
+}
+
 func NewGithubDownloader() *GithubDownloader {
 	return &GithubDownloader{}
 }
@@ -934,8 +947,7 @@ func (gd *GitDownloader) FetchFile(
 }
 
 func (gd *GitDownloader) Clone(commit string, dstPath string) error {
-	// Currently only the master branch is supported.
-	branch := "master"
+	branch := gd.MainBranch()
 
 	util.StatusMessage(util.VERBOSITY_DEFAULT,
 		"Downloading repository %s (commit: %s)\n", gd.Url, commit)
@@ -985,6 +997,14 @@ func (gd *GitDownloader) FixupOrigin(path string) error {
 	return setRemoteUrl(path, "origin", gd.Url, true)
 }
 
+func (gd *GitDownloader) MainBranch() string {
+	if gd.Branch != "" {
+		return gd.Branch
+	} else {
+		return "master"
+	}
+}
+
 func NewGitDownloader() *GitDownloader {
 	return &GitDownloader{}
 }
@@ -1005,7 +1025,7 @@ func (ld *LocalDownloader) FetchFile(
 
 func (ld *LocalDownloader) Fetch(path string) error {
 	os.RemoveAll(path)
-	return ld.Clone("master", path)
+	return ld.Clone(ld.MainBranch(), path)
 }
 
 func (ld *LocalDownloader) Checkout(path string, commit string) error {
@@ -1032,6 +1052,10 @@ func (ld *LocalDownloader) FixupOrigin(path string) error {
 	return nil
 }
 
+func (gd *LocalDownloader) MainBranch() string {
+	return "master"
+}
+
 func NewLocalDownloader() *LocalDownloader {
 	return &LocalDownloader{}
 }
@@ -1051,6 +1075,7 @@ func LoadDownloader(repoName string, repoVars map[string]string) (
 		gd.Server = repoVars["server"]
 		gd.User = repoVars["user"]
 		gd.Repo = repoVars["repo"]
+		gd.Branch = repoVars["branch"]
 
 		// The project.yml file can contain github access tokens and
 		// authentication credentials, but this file is probably world-readable
@@ -1080,6 +1105,7 @@ func LoadDownloader(repoName string, repoVars map[string]string) (
 	case "git":
 		gd := NewGitDownloader()
 		gd.Url = repoVars["url"]
+		gd.Branch = repoVars["branch"]
 		if gd.Url == "" {
 			return nil, loadError("repo \"%s\" missing required field \"url\"",
 				repoName)
diff --git a/newt/repo/repo.go b/newt/repo/repo.go
index 5f1ccce..09e0456 100644
--- a/newt/repo/repo.go
+++ b/newt/repo/repo.go
@@ -366,7 +366,8 @@ func (r *Repo) UpdateDesc() (bool, error) {
 func (r *Repo) EnsureExists() error {
 	// Clone the repo if it doesn't exist.
 	if !r.CheckExists() {
-		if err := r.downloadRepo("master"); err != nil {
+		branch := r.downloader.MainBranch()
+		if err := r.downloadRepo(branch); err != nil {
 			return err
 		}
 	}
@@ -401,7 +402,7 @@ func (r *Repo) downloadFile(commit string, srcPath string) (string, error) {
 }
 
 func (r *Repo) downloadRepositoryYml() error {
-	if _, err := r.downloadFile("master", REPO_FILE_NAME); err != nil {
+	if _, err := r.downloadFile(r.downloader.MainBranch(), REPO_FILE_NAME); err != nil {
 		return err
 	}