aboutsummaryrefslogtreecommitdiffstats
path: root/status.go
blob: e789f4acd208c58fe1254bb33483004080915904 (plain) (blame)
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
package git

import "fmt"

// Status current status of a Worktree
type Status map[string]*FileStatus

func (s Status) File(filename string) *FileStatus {
	if _, ok := (s)[filename]; !ok {
		s[filename] = &FileStatus{}
	}

	return s[filename]

}

func (s Status) IsClean() bool {
	for _, status := range s {
		if status.Worktree != Unmodified || status.Staging != Unmodified {
			return false
		}
	}

	return true
}

func (s Status) String() string {
	var names []string
	for name := range s {
		names = append(names, name)
	}

	var output string
	for _, name := range names {
		status := s[name]
		if status.Staging == 0 && status.Worktree == 0 {
			continue
		}

		if status.Staging == Renamed {
			name = fmt.Sprintf("%s -> %s", name, status.Extra)
		}

		output += fmt.Sprintf("%s%s %s\n", status.Staging, status.Worktree, name)
	}

	return output
}

// FileStatus status of a file in the Worktree
type FileStatus struct {
	Staging  StatusCode
	Worktree StatusCode
	Extra    string
}

// StatusCode status code of a file in the Worktree
type StatusCode int8

const (
	Unmodified StatusCode = iota
	Untracked
	Modified
	Added
	Deleted
	Renamed
	Copied
	UpdatedButUnmerged
)

func (c StatusCode) String() string {
	switch c {
	case Unmodified:
		return " "
	case Modified:
		return "M"
	case Added:
		return "A"
	case Deleted:
		return "D"
	case Renamed:
		return "R"
	case Copied:
		return "C"
	case UpdatedButUnmerged:
		return "U"
	case Untracked:
		return "?"
	default:
		return "-"
	}
}