/
repo.go
165 lines (133 loc) · 3.51 KB
/
repo.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package model
import (
"path/filepath"
"github.com/kamontat/gitgo/exception"
"github.com/kamontat/go-error-manager"
"github.com/kamontat/go-log-manager"
git "gopkg.in/src-d/go-git.v4"
)
// Repo is git repository object for gitgo.
type Repo struct {
isSetup bool
path string
repo *git.Repository
worktree *git.Worktree
gitCommand *GitCommand
}
// NewRepo will return new repository with current path.
// you must call setup for load git repository to memory.
func NewRepo() *Repo {
return CustomRepo(".")
}
// CustomRepo will return repo of custom path
func CustomRepo(path string) *Repo {
management := manager.StartResultManager()
return &Repo{
isSetup: false,
path: management.Execute1ParametersB(filepath.Abs, path).GetResult(),
gitCommand: Git(),
repo: nil,
}
}
// Setup will load git repository to memory.
// If any error occurred, exit with code 5.
func (r *Repo) Setup() {
if r.isSetup {
return
}
om.Log.ToVerbose("Repository", "initial path "+r.path)
result, err := git.PlainOpen(r.path)
e.ShowAndExit(e.Error(e.IsInitial, err))
if err == nil {
r.repo = result
r.worktree, err = r.repo.Worktree()
e.ShowAndExit(e.Error(e.IsInitial, err))
r.isSetup = result != nil && r.worktree != nil
}
}
// GetRawGitRepository will return git.Repository of this Repo, can be nil
func (r *Repo) GetRawGitRepository() *git.Repository {
r.Setup()
if r.isSetup {
return r.repo
}
return nil
}
// GetGitRepository will return git.Repository of this Repo
func (r *Repo) GetGitRepository() *manager.ResultWrapper {
return manager.Wrap(r.GetRawGitRepository())
}
// GetRawWorktree is getter to get worktree, this method can return nil value
func (r *Repo) GetRawWorktree() *git.Worktree {
r.Setup()
if r.isSetup {
return r.worktree
}
return nil
}
// GetWorktree is getter method, which get git.Worktree from git.Repository.
func (r *Repo) GetWorktree() *manager.ResultWrapper {
return manager.Wrap(r.GetRawWorktree())
}
// Status will return *git.Status.
func (r *Repo) Status() *manager.ResultWrapper {
resultWrapper := r.GetWorktree()
return resultWrapper.UnwrapNext(func(i interface{}) interface{} {
if r.Throw().CanBeThrow() {
return nil
}
status, err := i.(*git.Worktree).Status()
if err == nil {
return status
}
return nil
})
}
// Add get array of filepath, and return ErrManager.
// anyway, It's will run os.Exit with code 10 if any error occurred.
func (r *Repo) Add(filepath []string) (t *manager.Throwable) {
t = r.Throw()
if t.CanBeThrow() {
return
}
worktree := r.GetRawWorktree()
for _, f := range filepath {
worktree.Add(f)
}
return
}
// AddAll will run git add -A command in cli.
func (r *Repo) AddAll() *manager.Throwable {
if t := r.Throw(); t.CanBeThrow() {
return t
}
t := r.gitCommand.Exec("add", "-A").Throw()
manager := manager.NewR()
for _, e := range t.ListErrors() {
manager.Save("", e)
}
return manager.Throw()
}
func (r *Repo) GetBranch() *Branch {
ref, err := r.repo.Head()
e.ShowAndExit(e.Error(e.IsInitial, err))
return &Branch{
KeyList: (&List{}).Setup("branches"),
Repository: r.repo,
Worktree: r.GetRawWorktree(),
HEAD: ref,
}
}
// GetCommit will return Commit object.
func (r *Repo) GetCommit() *Commit {
return &Commit{
KeyList: (&List{}).Setup("commits"),
throwable: r.Throw(),
}
}
func (r *Repo) Throw() *manager.Throwable {
if !r.isSetup {
return e.ErrorMessage(e.IsInitial, "This repository is not setup yet, or have error while setting.")
}
return manager.NewE().Throw()
}