summaryrefslogtreecommitdiff
path: root/doPatch.go
blob: 52a90f1306cb56c42dec5cdc905d6e1edaf46c57 (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"path/filepath"

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

func doPatch() error {
	if err := me.forge.LoadPatchsets(); err != nil {
		log.Info("patches failed to open", err)
		if err := me.forge.SavePatchsets(); err != nil {
			log.Warn("savePatchsets() failed", err)
		}
	}

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

	if argv.Patch.Get != nil {
		if err := me.forge.GetPatches(); err != nil {
			log.Info("Get Patchsets Failed", err)
			return err
		}
		me.forge.Patchsets.PrintTable()
		return nil
	}

	if argv.Patch.Check != nil {
		for pset := range me.forge.Patchsets.IterAll() {
			log.Info(pset.Uuid)
			for patch := range pset.Patches.IterAll() {
				if repo, ok := me.forge.IsPatchApplied(patch); ok {
					log.Info("found patch in repo", repo.Namespace, patch.Filename)
				} else {
					// log.Info("did not find patch", patch.CommitHash, patch.NewHash, patch.Filename)
				}
			}
		}
		// me.forge.Patchsets.PrintTable()
		return nil
	}

	if argv.Patch.List != nil {
		me.forge.Patchsets.PrintTable()
		// return doPatchList()
		return nil
	}

	// always run dirty first
	me.forge.CheckDirty()

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

// returns bad if patches can not be applied
// logic is not great here but it was a first pass
func dumpPatchset(pset *forgepb.Patchset) bool {
	// don't even bother to continue if we already know it's broken
	if pset.State == "BROKEN" {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS BAD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
		return false
	} else {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS GOOD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
	}

	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
}