patch.go 2.92 KB
Newer Older
1
2
3
4
5
6
package directives

import (
	"bytes"
	"errors"
	"fmt"
7
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
8
9
10
11
12
13
	srpmprocpb "git.rockylinux.org/release-engineering/public/srpmproc/pb"
	"github.com/bluekeyes/go-gitdiff/gitdiff"
	"github.com/go-git/go-git/v5"
	"log"
)

14
func patch(cfg *srpmprocpb.Cfg, _ *data.ProcessData, _ *data.ModeData, patchTree *git.Worktree, pushTree *git.Worktree) error {
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	for _, patch := range cfg.Patch {
		patchFile, err := patchTree.Filesystem.Open(patch.File)
		if err != nil {
			return errors.New(fmt.Sprintf("COULD_NOT_OPEN_PATCH_FILE:%s", patch.File))
		}
		files, _, err := gitdiff.Parse(patchFile)
		if err != nil {
			log.Printf("could not parse patch file: %v", err)
			return errors.New(fmt.Sprintf("COULD_NOT_PARSE_PATCH_FILE:%s", patch.File))
		}

		for _, patchedFile := range files {
			srcPath := patchedFile.NewName
			if !patch.Strict {
				srcPath = checkAddPrefix(patchedFile.NewName)
			}
			var output bytes.Buffer
			if !patchedFile.IsDelete && !patchedFile.IsNew {
				patchSubjectFile, err := pushTree.Filesystem.Open(srcPath)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_OPEN_PATCH_SUBJECT:%s", srcPath))
				}

				err = gitdiff.NewApplier(patchSubjectFile).ApplyFile(&output, patchedFile)
				if err != nil {
					log.Printf("could not apply patch: %v", err)
					return errors.New(fmt.Sprintf("COULD_NOT_APPLY_PATCH_WITH_SUBJECT:%s", srcPath))
				}
			}

			oldName := patchedFile.OldName
			if !patch.Strict {
				oldName = checkAddPrefix(patchedFile.OldName)
			}
			_ = pushTree.Filesystem.Remove(oldName)
			_ = pushTree.Filesystem.Remove(srcPath)

			if patchedFile.IsNew {
				newFile, err := pushTree.Filesystem.Create(srcPath)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_CREATE_NEW_FILE:%s", srcPath))
				}
				err = gitdiff.NewApplier(newFile).ApplyFile(&output, patchedFile)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_APPLY_PATCH_TO_NEW_FILE:%s", srcPath))
				}
				_, err = newFile.Write(output.Bytes())
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_WRITE_TO_NEW_FILE:%s", srcPath))
				}
				_, err = pushTree.Add(srcPath)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_ADD_NEW_FILE_TO_GIT:%s", srcPath))
				}
			} else if !patchedFile.IsDelete {
				newFile, err := pushTree.Filesystem.Create(srcPath)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_CREATE_POST_PATCH_FILE:%s", srcPath))
				}
				_, err = newFile.Write(output.Bytes())
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_WRITE_POST_PATCH_FILE:%s", srcPath))
				}
				_, err = pushTree.Add(srcPath)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_ADD_POST_PATCH_FILE_TO_GIT:%s", srcPath))
				}
			} else {
				_, err = pushTree.Remove(oldName)
				if err != nil {
					return errors.New(fmt.Sprintf("COULD_NOT_REMOVE_FILE_FROM_GIT:%s", oldName))
				}
			}
		}
	}

	return nil
}