diff --git a/patch.Make.go b/patch.Make.go index a71dff2..5341cb8 100644 --- a/patch.Make.go +++ b/patch.Make.go @@ -1,10 +1,12 @@ package forgepb import ( + "errors" "fmt" "os" "path/filepath" + "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -15,6 +17,7 @@ func (f *Forge) MakeDevelPatchSet() (*Patchs, error) { return nil, err } defer os.RemoveAll(dir) // clean up + pset.TmpDir = dir all := f.Repos.SortByGoPath() for all.Scan() { @@ -28,19 +31,84 @@ func (f *Forge) MakeDevelPatchSet() (*Patchs, error) { if userb == "" { continue } - return f.makePatchSetNew(develb, userb) + pset.StartBranchName = develb + pset.EndBranchName = userb + err := pset.makePatchSetNew(repo) + if err != nil { + return nil, err + } } return pset, nil } -func (f *Forge) makePatchSetNew(fromBranch string, toBranch string) (*Patchs, error) { - return nil, nil +func (f *Forge) MakeMasterPatchSet() (*Patchs, error) { + pset := new(Patchs) + dir, err := os.MkdirTemp("", "forge") + if err != nil { + return nil, err + } + defer os.RemoveAll(dir) // clean up + pset.TmpDir = dir + + all := f.Repos.SortByGoPath() + for all.Scan() { + repo := all.Next() + startb := repo.GetMasterBranchName() + endb := repo.GetUserBranchName() + + if startb == "" { + continue + } + if endb == "" { + continue + } + // log.Info("repo", repo.GoPath, startb, "..", endb) + pset.StartBranchName = startb + pset.EndBranchName = endb + err := pset.makePatchSetNew(repo) + if err != nil { + return nil, err + } + } + return pset, nil } -var pset *Patchs +func (pset *Patchs) makePatchSetNew(repo *gitpb.Repo) error { + startBranch := pset.StartBranchName + endBranch := pset.EndBranchName + repoDir := filepath.Join(pset.TmpDir, repo.GoPath) + err := os.MkdirAll(repoDir, 0755) + if err != nil { + return err + } + + // git format-patch branch1..branch2 + cmd := []string{"git", "format-patch", "-o", repoDir, startBranch + ".." + endBranch} + 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 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 errors.New(fmt.Sprintf("git returned %d", r.Exit)) + } + if len(r.Stdout) == 0 { + // git created no files to add + return nil + } + + return pset.addPatchFiles(repoDir) +} + +// var pset *Patchs func (f *Forge) MakePatchSet() (*Patchs, error) { - pset = new(Patchs) + pset := new(Patchs) dir, err := os.MkdirTemp("", "forge") if err != nil { return nil, err @@ -85,13 +153,13 @@ func (f *Forge) MakePatchSet() (*Patchs, error) { continue } - addPatchFiles(repoDir) + pset.addPatchFiles(repoDir) } return pset, nil } // process each file in pDir/ -func addPatchFiles(pDir string) error { +func (p *Patchs) 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 { @@ -115,7 +183,7 @@ func addPatchFiles(pDir string) error { patch := new(Patch) patch.Filename = path patch.Data = data - pset.Patchs = append(pset.Patchs, patch) + p.Patchs = append(p.Patchs, patch) // log.Info("ADDED PATCH FILE", path) return nil }) diff --git a/patch.proto b/patch.proto index a35bca6..2e463b5 100644 --- a/patch.proto +++ b/patch.proto @@ -17,8 +17,14 @@ message Patchs { // `autogenpb:marshal` string uuid = 1; // `autogenpb:uuid:0703df95-6a38-4422-994b-c55d3d6001f9` // todo: add file support string version = 2; // could be used for protobuf schema change violations? repeated Patch Patchs = 3; - string name = 4; // could be used for protobuf schema change violations? - string comment = 5; // could be used for protobuf schema change violations? - string gitAuthor = 6; // could be used for protobuf schema change violations? - google.protobuf.Timestamp ctime = 7; // the git commit timestamp of the version + string name = 4; // + string comment = 5; // + string gitAuthorName = 6; // + string gitAuthorEmail = 7; // + google.protobuf.Timestamp ctime = 8; // create time of this patchset + string tmpDir = 9; // temp dir + string startBranchName = 10; // + string endBranchName = 11; // + string startBranchHash = 12; // + string endBranchHash = 13; // }