Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newt: Add external repos patches support #550

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newt/cli/project_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func upgradeRunCmd(cmd *cobra.Command, args []string) {
interfaces.SetProject(proj)

proj.GetPkgRepos()
proj.SetGitEnvVariables()

pred := makeRepoPredicate(args)
if err := proj.UpgradeIf(
Expand Down
24 changes: 24 additions & 0 deletions newt/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type Downloader interface {
// If such a commit exists, it is returned. Otherwise, "" is returned.
LatestRc(path string, base string) (string, error)

// Applies patches provided inside "patches" directory.
// If no patch is provided function does nothing
ApplyPatches(path string, patches []string) error

// Returns the branch that contains the YAML control files; this option
// allows implementers to override "master" as the main branch.
MainBranch() string
Expand Down Expand Up @@ -451,6 +455,26 @@ func (gd *GenericDownloader) Checkout(repoDir string, commit string) error {
return err
}

func (gd *GenericDownloader) ApplyPatches(repoDir string, patches []string) error {
cmd := []string{
"am",
}
cmd = append(cmd, patches...)

_, err := executeGitCommand(repoDir, cmd, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm should we even try to execute 'git am' if no patches were provided?

if err != nil {
// Abort git am if applying patches failed
cmd = []string{
"am",
"--abort",
}
executeGitCommand(repoDir, cmd, true)

return err
}
return nil
}

// Update one submodule tree in a repo (under path)
func (gd *GenericDownloader) UpdateSubmodule(path string, submodule string) error {
cmd := []string{
Expand Down
10 changes: 10 additions & 0 deletions newt/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,16 @@ func (inst *Installer) Upgrade(candidates []*repo.Repo, force bool,
r.Name(), destVer.String())
}

for _, r := range candidates {
err = r.ApplyPatches()
if err != nil {
util.StatusMessage(util.VERBOSITY_DEFAULT,
"Applying patches in repository %s failed\n", r.Name())

return err
}
}

return nil
}

Expand Down
32 changes: 30 additions & 2 deletions newt/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
var globalProject *Project = nil

const PROJECT_FILE_NAME = "project.yml"
const PATCHES_DIR = "patches"

var ignoreSearchDirs []string = []string{
"bin",
Expand Down Expand Up @@ -95,7 +96,7 @@ func initProject(dir string, download bool) error {

if download {
err = globalProject.UpgradeIf(newtutil.NewtForce, newtutil.NewtAsk,
func(r *repo.Repo) bool { return !r.IsExternal(r.Path()) })
func(r *repo.Repo) bool { return !r.IsExternal(r.Path()) })
if err != nil {
return err
}
Expand Down Expand Up @@ -182,7 +183,6 @@ func (proj *Project) isRepoAdded(r *repo.Repo) bool {
}

func (proj *Project) GetPkgRepos() error {

for _, pkgList := range proj.packages {
for _, pkg := range *pkgList {
if pkg.PkgConfig().HasKey("repository") {
Expand Down Expand Up @@ -215,6 +215,21 @@ func (proj *Project) GetPkgRepos() error {
}
proj.rootRepoReqs[repoName] = verReq
}

if _, err := os.Stat(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name()); os.IsNotExist(err) {
continue
} else {
dirEntries, err := os.ReadDir(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name())
if err != nil {
return err
}

for _, e := range dirEntries {
if strings.HasSuffix(e.Name(), ".patch") {
r.AddPatch(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name() + "/" + e.Name())
}
}
}
}
}
}
Expand All @@ -223,6 +238,19 @@ func (proj *Project) GetPkgRepos() error {
return nil
}

func (proj *Project) SetGitEnvVariables() error {
err := os.Setenv("GIT_COMMITTER_NAME", "newt")
if err != nil {
return err
}

err = os.Setenv("GIT_COMMITTER_EMAIL", "dev@mynewt.apache.org")
if err != nil {
return err
}
return nil
}

func (proj *Project) Path() string {
return proj.BasePath
}
Expand Down
18 changes: 18 additions & 0 deletions newt/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const REPO_DEFAULT_PERMS = 0755

const REPO_FILE_NAME = "repository.yml"
const REPOS_DIR = "repos"
const PATCHES_DIR = "patches"

type Repo struct {
name string
Expand Down Expand Up @@ -76,6 +77,10 @@ type Repo struct {

hasSubmodules bool
submodules []string

// Used with external repos. If package that adds external repository provides patches for it,
// the paths to them are going to be stored here.
patches []string
}

type RepoDependency struct {
Expand Down Expand Up @@ -124,6 +129,19 @@ func (r *Repo) Downloader() downloader.Downloader {
return r.downloader
}

func (r *Repo) AddPatch(path string) {
r.patches = append(r.patches, path)
}

func (r *Repo) ApplyPatches() error {
if len(r.patches) == 0 {
return nil
}

err := r.Downloader().ApplyPatches(r.Path(), r.patches)
return err
}

func (repo *Repo) FilteredSearchList(
curPath string, searchedMap map[string]struct{}) ([]string, error) {

Expand Down
Loading