Skip to content

Commit 606ff44

Browse files
committed
Add support for SmPL overlay patches
This adds support for SmPL cocci patches.
1 parent fa83de6 commit 606ff44

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

newt/downloader/downloader.go

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ func (gd *GenericDownloader) Checkout(repoDir string, commit string) error {
461461
return err
462462
}
463463

464-
func (gd *GenericDownloader) ApplyPatches(repoDir string, patches []string) error {
464+
func ApplyPatch(repoDir string, patch string) error {
465465
cmd := []string{
466466
"am",
467467
}
468-
cmd = append(cmd, patches...)
468+
cmd = append(cmd, patch)
469469

470470
_, err := executeGitCommand(repoDir, cmd, true)
471471
if err != nil {
@@ -481,6 +481,77 @@ func (gd *GenericDownloader) ApplyPatches(repoDir string, patches []string) erro
481481
return nil
482482
}
483483

484+
func spatchPath() (string, error) {
485+
spatchPath, err := exec.LookPath("spatch")
486+
if err != nil {
487+
return "", util.NewNewtError(fmt.Sprintf("Can't find spatch binary: %s\n",
488+
err.Error()))
489+
}
490+
491+
return filepath.ToSlash(spatchPath), nil
492+
}
493+
494+
func executeSpatchCommand(dir string, cmd []string, logCmd bool) ([]byte, error) {
495+
wd, err := os.Getwd()
496+
if err != nil {
497+
return nil, util.NewNewtError(err.Error())
498+
}
499+
500+
sp, err := spatchPath()
501+
if err != nil {
502+
return nil, err
503+
}
504+
505+
if err := os.Chdir(dir); err != nil {
506+
return nil, util.ChildNewtError(err)
507+
}
508+
509+
defer os.Chdir(wd)
510+
511+
spatchCmd := []string{sp}
512+
spatchCmd = append(spatchCmd, cmd...)
513+
output, err := util.ShellCommandLimitDbgOutput(spatchCmd, nil, logCmd, -1)
514+
if err != nil {
515+
return nil, err
516+
}
517+
518+
return output, nil
519+
}
520+
521+
func ApplyCocci(repoDir string, cocci string) error {
522+
cmd := []string{
523+
"--sp-file",
524+
cocci,
525+
"--in-place",
526+
"--include-headers",
527+
repoDir,
528+
}
529+
530+
_, err := executeSpatchCommand(repoDir, cmd, true)
531+
if err != nil {
532+
return err
533+
}
534+
return nil
535+
}
536+
537+
func (gd *GenericDownloader) ApplyPatches(repoDir string, patches []string) error {
538+
for _, patch := range patches {
539+
if strings.HasSuffix(patch, ".patch") {
540+
err := ApplyPatch(repoDir, patch)
541+
if err != nil {
542+
return err
543+
}
544+
} else if strings.HasSuffix(patch, ".cocci") {
545+
err := ApplyCocci(repoDir, patch)
546+
if err != nil {
547+
return err
548+
}
549+
}
550+
}
551+
552+
return nil
553+
}
554+
484555
// Update one submodule tree in a repo (under path)
485556
func (gd *GenericDownloader) UpdateSubmodule(path string, submodule string) error {
486557
cmd := []string{

newt/project/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (proj *Project) GetPkgRepos() error {
256256
}
257257

258258
for _, e := range dirEntries {
259-
if strings.HasSuffix(e.Name(), ".patch") {
259+
if strings.HasSuffix(e.Name(), ".patch") || strings.HasSuffix(e.Name(), ".cocci") {
260260
r.AddPatch(pkg.BasePath() + "/" + PATCHES_DIR + "/" + r.Name() + "/" + e.Name())
261261
}
262262
}

0 commit comments

Comments
 (0)