summaryrefslogtreecommitdiff
path: root/patch.Make.go
blob: a71dff2609b5fbf7346cb3b00c51d91beff64c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package forgepb

import (
	"fmt"
	"os"
	"path/filepath"

	"go.wit.com/log"
)

func (f *Forge) MakeDevelPatchSet() (*Patchs, error) {
	pset := new(Patchs)
	dir, err := os.MkdirTemp("", "forge")
	if err != nil {
		return nil, err
	}
	defer os.RemoveAll(dir) // clean up

	all := f.Repos.SortByGoPath()
	for all.Scan() {
		repo := all.Next()
		userb := repo.GetUserBranchName()
		develb := repo.GetDevelBranchName()

		if develb == "" {
			continue
		}
		if userb == "" {
			continue
		}
		return f.makePatchSetNew(develb, userb)
	}
	return pset, nil
}

func (f *Forge) makePatchSetNew(fromBranch string, toBranch string) (*Patchs, error) {
	return nil, nil
}

var pset *Patchs

func (f *Forge) MakePatchSet() (*Patchs, error) {
	pset = new(Patchs)
	dir, err := os.MkdirTemp("", "forge")
	if err != nil {
		return nil, err
	}
	defer os.RemoveAll(dir) // clean up

	all := f.Repos.SortByGoPath()
	for all.Scan() {
		repo := all.Next()
		userb := repo.GetUserBranchName()
		develb := repo.GetDevelBranchName()

		if develb == "" {
			continue
		}
		if userb == "" {
			continue
		}

		repoDir := filepath.Join(dir, repo.GoPath)
		err := os.MkdirAll(repoDir, 0755)
		if err != nil {
			return nil, err
		}

		// git format-patch branch1..branch2
		cmd := []string{"git", "format-patch", "-o", repoDir, develb + ".." + userb}
		r := repo.Run(cmd)
		if r.Error != nil {
			log.Info("git format-patch", repo.FullPath)
			log.Info("git format-patch", cmd)
			log.Info("git format-patch error", r.Error)
			return nil, r.Error
		}
		if r.Exit != 0 {
			log.Info("git format-patch", repo.FullPath)
			log.Info("git format-patch", cmd)
			log.Info("git format-patch exit", r.Exit)
			return nil, r.Error
		}
		if len(r.Stdout) == 0 {
			continue
		}

		addPatchFiles(repoDir)
	}
	return pset, nil
}

// process each file in pDir/
func addPatchFiles(pDir string) error {
	// log.Info("ADD PATCH FILES ADDED DIR", pDir)
	var baderr error
	filepath.Walk(pDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			// Handle possible errors, like permission issues
			fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
			baderr = err
			return err
		}

		if info.IsDir() {
			return nil
		}
		// log.Info("TESTING FILE", path)
		data, err := os.ReadFile(path)
		if err != nil {
			log.Info("addPatchFile() failed", path)
			baderr = err
			return err
		}
		patch := new(Patch)
		patch.Filename = path
		patch.Data = data
		pset.Patchs = append(pset.Patchs, patch)
		// log.Info("ADDED PATCH FILE", path)
		return nil
	})
	return baderr
}

// just an example of how to walk only directories
func onlyWalkDirs(pDir string) error {
	log.Info("DIR", pDir)
	// var all []string
	var baderr error
	filepath.WalkDir(pDir, func(path string, d os.DirEntry, err error) error {
		if err != nil {
			// Handle possible errors, like permission issues
			fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
			baderr = err
			return err
		}

		log.Info("TESTING DIR", path)
		if d.IsDir() {
			return filepath.SkipDir
		}
		log.Info("NEVER GETS HERE? WHAT IS THIS?", path)
		return nil
	})
	return baderr
}