Skip to content

Commit 93061ff

Browse files
committed
newt: Add external repos patches support
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.
1 parent fca2b91 commit 93061ff

File tree

5 files changed

+83
-2
lines changed

5 files changed

+83
-2
lines changed

newt/cli/project_cmds.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func upgradeRunCmd(cmd *cobra.Command, args []string) {
131131
interfaces.SetProject(proj)
132132

133133
proj.GetPkgRepos()
134+
proj.SetGitEnvVariables()
134135

135136
pred := makeRepoPredicate(args)
136137
if err := proj.UpgradeIf(

newt/downloader/downloader.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ type Downloader interface {
9797
// If such a commit exists, it is returned. Otherwise, "" is returned.
9898
LatestRc(path string, base string) (string, error)
9999

100+
// Applies patches provided inside "patches" directory.
101+
// If no patch is provided function does nothing
102+
ApplyPatches(path string, patches []string) error
103+
100104
// Returns the branch that contains the YAML control files; this option
101105
// allows implementers to override "master" as the main branch.
102106
MainBranch() string
@@ -451,6 +455,26 @@ func (gd *GenericDownloader) Checkout(repoDir string, commit string) error {
451455
return err
452456
}
453457

458+
func (gd *GenericDownloader) ApplyPatches(repoDir string, patches []string) error {
459+
cmd := []string{
460+
"am",
461+
}
462+
cmd = append(cmd, patches...)
463+
464+
_, err := executeGitCommand(repoDir, cmd, true)
465+
if err != nil {
466+
// Abort git am if applying patches failed
467+
cmd = []string{
468+
"am",
469+
"--abort",
470+
}
471+
executeGitCommand(repoDir, cmd, true)
472+
473+
return err
474+
}
475+
return nil
476+
}
477+
454478
// Update one submodule tree in a repo (under path)
455479
func (gd *GenericDownloader) UpdateSubmodule(path string, submodule string) error {
456480
cmd := []string{

newt/install/install.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,16 @@ func (inst *Installer) Upgrade(candidates []*repo.Repo, force bool,
602602
r.Name(), destVer.String())
603603
}
604604

605+
for _, r := range candidates {
606+
err = r.ApplyPatches()
607+
if err != nil {
608+
util.StatusMessage(util.VERBOSITY_DEFAULT,
609+
"Applying patches in repository %s failed", r.Name())
610+
611+
return err
612+
}
613+
}
614+
605615
return nil
606616
}
607617

newt/project/project.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
var globalProject *Project = nil
4545

4646
const PROJECT_FILE_NAME = "project.yml"
47+
const PATCHES_DIR = "patches"
4748

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

9697
if download {
9798
err = globalProject.UpgradeIf(newtutil.NewtForce, newtutil.NewtAsk,
98-
func(r *repo.Repo) bool { return !r.IsExternal(r.Path()) })
99+
func(r *repo.Repo) bool { return !r.IsExternal(r.Path()) })
99100
if err != nil {
100101
return err
101102
}
@@ -182,7 +183,6 @@ func (proj *Project) isRepoAdded(r *repo.Repo) bool {
182183
}
183184

184185
func (proj *Project) GetPkgRepos() error {
185-
186186
for _, pkgList := range proj.packages {
187187
for _, pkg := range *pkgList {
188188
if pkg.PkgConfig().HasKey("repository") {
@@ -215,6 +215,21 @@ func (proj *Project) GetPkgRepos() error {
215215
}
216216
proj.rootRepoReqs[repoName] = verReq
217217
}
218+
219+
if _, err := os.Stat(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name()); os.IsNotExist(err) {
220+
continue
221+
} else {
222+
dirEntries, err := os.ReadDir(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name())
223+
if err != nil {
224+
return err
225+
}
226+
227+
for _, e := range dirEntries {
228+
if strings.HasSuffix(e.Name(), ".patch") {
229+
r.AddPatch(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name() + "/" + e.Name())
230+
}
231+
}
232+
}
218233
}
219234
}
220235
}
@@ -223,6 +238,19 @@ func (proj *Project) GetPkgRepos() error {
223238
return nil
224239
}
225240

241+
func (proj *Project) SetGitEnvVariables() error {
242+
err := os.Setenv("GIT_COMMITTER_NAME", "newt")
243+
if err != nil {
244+
return err
245+
}
246+
247+
err = os.Setenv("GIT_COMMITTER_EMAIL", "dev@mynewt.apache.org")
248+
if err != nil {
249+
return err
250+
}
251+
return nil
252+
}
253+
226254
func (proj *Project) Path() string {
227255
return proj.BasePath
228256
}

newt/repo/repo.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const REPO_DEFAULT_PERMS = 0755
4444

4545
const REPO_FILE_NAME = "repository.yml"
4646
const REPOS_DIR = "repos"
47+
const PATCHES_DIR = "patches"
4748

4849
type Repo struct {
4950
name string
@@ -76,6 +77,10 @@ type Repo struct {
7677

7778
hasSubmodules bool
7879
submodules []string
80+
81+
// Used with external repos. If package that adds external repository provides patches for it,
82+
// the paths to them are going to be stored here.
83+
patches []string
7984
}
8085

8186
type RepoDependency struct {
@@ -124,6 +129,19 @@ func (r *Repo) Downloader() downloader.Downloader {
124129
return r.downloader
125130
}
126131

132+
func (r *Repo) AddPatch(path string) {
133+
r.patches = append(r.patches, path)
134+
}
135+
136+
func (r *Repo) ApplyPatches() error {
137+
if len(r.patches) == 0 {
138+
return nil
139+
}
140+
141+
err := r.Downloader().ApplyPatches(r.Path(), r.patches)
142+
return err
143+
}
144+
127145
func (repo *Repo) FilteredSearchList(
128146
curPath string, searchedMap map[string]struct{}) ([]string, error) {
129147

0 commit comments

Comments
 (0)