summaryrefslogtreecommitdiff
path: root/doPatch.go
blob: cf89202ad14d0ba0224d2f4d8e223774ff2be373 (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
package main

import (
	"path/filepath"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"
)

func doPatch() error {
	if argv.Patch.Submit != "" {
		_, err := me.forge.SubmitDevelPatchSet(argv.Patch.Submit)
		if err != nil {
			return err
		}
		return nil
	}

	if argv.Patch.List != nil {
		return doPatchList()
	}

	// if no option is given to patch, list out the
	// repos that have patches ready in them
	findReposWithPatches()
	if me.found.Len() == 0 {
		log.Info("you currently have no patches in your user branches")
		return nil
	}
	me.forge.PrintHumanTable(me.found)
	return nil
}

func doPatchList() error {
	psets, err := me.forge.GetPatchesets()
	if err != nil {
		log.Info("Get Patchsets failed", err)
		return err
	}
	log.Info("got psets len", len(psets.Patchsets))
	all := psets.SortByName()
	for all.Scan() {
		pset := all.Next()
		log.Info("pset name =", pset.Name)
		dumpPatchset(pset)
	}

	return nil
}

// returns bad if patches can not be applied
func dumpPatchset(pset *forgepb.Patchset) bool {
	log.Info("applyPatches() NAME", pset.Name)
	log.Info("applyPatches() COMMENT", pset.Comment)
	log.Info("applyPatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
	log.Info("applyPatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
	log.Info("applyPatches() Branch Name", pset.GetStartBranchName())
	log.Info("applyPatches() Start Hash", pset.GetStartBranchHash())

	var count int
	var bad int
	all := pset.Patches.SortByFilename()
	for all.Scan() {
		p := all.Next()
		if IsValidPatch(p) {
			// ok
		} else {
			pset.State = "BROKEN"
			bad += 1
		}
		count += 1
	}
	log.Info("pset has", count, "total patches, ", bad, "bad patches")
	if bad == 0 {
		return true
	}
	return false
}

func IsValidPatch(p *forgepb.Patch) bool {
	basepath, filename := filepath.Split(p.Filename)
	repo := me.forge.FindByGoPath(basepath)
	if argv.Verbose {
		log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	}
	if repo == nil {
		log.Info("can not apply patch! repo not found", basepath, filename)
		return false
	}
	if repo.DevelHash() != p.StartHash {
		log.Info("can not apply patch! devel hash mismatch", basepath, filename)
		return false
	}
	if repo.DevelHash() == p.StartHash {
		log.Info("local devel hash:", repo.DevelHash(), "matches patch hash", p.StartHash, "and can be applied")
	}
	log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	for _, line := range p.Files {
		log.Info("\t", line)
	}
	return true
}