Skip to content

Commit

Permalink
newt: Add external repos patches support
Browse files Browse the repository at this point in the history
Now if package that adds external repository contains directory
patches/*external_repo_name*, newt will try to apply patches
inside this directory to the external repo.
  • Loading branch information
m-gorecki authored and kasjer committed Apr 5, 2024
1 parent fca2b91 commit f95aa66
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
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)
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

0 comments on commit f95aa66

Please sign in to comment.