summaryrefslogtreecommitdiff
path: root/reloadCheckDirty.go
blob: 69b6e520e527db8347bcbbaa748d72caeb06c129 (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
package gitpb

// runs git, parses output
// types faster than you can

import (
	"fmt"
	"strings"
	"time"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func (repo *Repo) NoteChange(s string) {
	log.Warn("NoteChange()", s)
}

// just return the current value
func (repo *Repo) IsDirty() bool {
	return repo.Dirty
}

func (repo *Repo) SmartSetState(newstate string) bool {
	if newstate == repo.State {
		// nothing changed
		return false
	}
	repo.State = newstate
	return true
}

// returns true if the repo is dirty
// runs os.Exec('git') every time
func (repo *Repo) CheckDirty() bool {
	cmd := []string{"git", "status", "--porcelain"}
	r := shell.PathRunLog(repo.FullPath, cmd, INFO)
	if r.Error != nil {
		log.Warn("CheckDirty() status cmd =", cmd)
		out := strings.Join(r.Stdout, "\n")
		log.Warn("CheckDirty() status out =", out)
		log.Warn("CheckDirty() status err =", r.Error)
		log.Error(r.Error, "CheckDirty() git status error")
		repo.NoteChange("git status is in error " + fmt.Sprint(r.Error))
		repo.Dirty = true
		repo.State = "dirty"
		return true
	}
	// dirty if anything but go.mod and go.sum
	var bad bool = false
	for _, line := range r.Stdout {
		parts := strings.Fields(line)
		if len(parts) == 2 {
			switch parts[1] {
			case "go.mod":
			case "go.sum":
			default:
				bad = true
			}
		} else {
			bad = true
		}
	}
	repo.DirtyList = r.Stdout

	pbnow := timestamppb.New(time.Now())
	repo.Times.LastDirty = pbnow
	repo.Dirty = bad
	if bad {
		return repo.SmartSetState("dirty")
	} else {
		return repo.SmartSetState("")
	}
	return bad
}