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 committed Mar 27, 2024
1 parent 679b6a8 commit c681784
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
14 changes: 14 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,16 @@ 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)
return err
}

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

for _, r := range candidates {
err = r.ApplyPatches()
if err != nil {
return err
}
}

return nil
}

Expand Down
18 changes: 17 additions & 1 deletion 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 @@ -215,6 +216,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 Down
14 changes: 14 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,15 @@ 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 {
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 c681784

Please sign in to comment.