Skip to content

Commit 0363f2d

Browse files
authored
Convert LazyRepo to struct containing repo
1 parent c186190 commit 0363f2d

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

commit.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ func (r *LazyRepo) Commit() (hash plumbing.Hash, msg string, err error) {
2020
return
2121
}
2222

23-
wt, err := r.worktree()
24-
if err != nil {
25-
return
26-
}
27-
28-
hash, err = wt.Commit(msg, &git.CommitOptions{})
23+
hash, err = r.wt.Commit(msg, &git.CommitOptions{})
2924
return
3025
}
3126

lazycommit.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ package lazycommit
55
import "github.com/go-git/go-git/v5"
66

77
// LazyRepo is a wrapper around go-git's Repository for simpler usage.
8-
type LazyRepo git.Repository
8+
type LazyRepo struct {
9+
*git.Repository
10+
wt *git.Worktree
11+
}
912

1013
// OpenRepo opens a repository at the given path.
1114
func OpenRepo(path string) (*LazyRepo, error) {
1215
repo, err := git.PlainOpen(path)
1316
if err != nil {
1417
return nil, err
1518
}
16-
return (*LazyRepo)(repo), nil
19+
wt, err := repo.Worktree()
20+
if err != nil {
21+
return nil, err
22+
}
23+
return &LazyRepo{
24+
Repository: repo,
25+
wt: wt,
26+
}, nil
1727
}
1828

1929
// NoStaged checks if there are no staged changes (added files, changed files, removed files)
@@ -35,23 +45,10 @@ func (r *LazyRepo) NoStaged() (bool, error) {
3545

3646
// StageAll stages all changes in the repository.
3747
func (r *LazyRepo) StageAll() error {
38-
wt, err := (*git.Repository)(r).Worktree()
39-
if err != nil {
40-
return err
41-
}
42-
return wt.AddWithOptions(&git.AddOptions{All: true})
43-
}
44-
45-
// Worktree gets the repo's worktree.
46-
func (r *LazyRepo) worktree() (*git.Worktree, error) {
47-
return (*git.Repository)(r).Worktree()
48+
return r.wt.AddWithOptions(&git.AddOptions{All: true})
4849
}
4950

5051
// Status gets the repo's status.
5152
func (r *LazyRepo) status() (git.Status, error) {
52-
wt, err := r.worktree()
53-
if err != nil {
54-
return nil, err
55-
}
56-
return wt.Status()
53+
return r.wt.Status()
5754
}

0 commit comments

Comments
 (0)