6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-08 07:34:18 +00:00

[WIP] Replace git2go with go-git

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-26 21:35:57 +02:00
parent 632fb4a9c8
commit 616b476ce1
7 changed files with 284 additions and 2 deletions

125
new/helpers_test.go Normal file
View File

@@ -0,0 +1,125 @@
package new
import (
"testing"
"time"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/pkg/errors"
//"github.com/go-git/go-git/v5"
)
func checkFatal(t *testing.T, err error) {
if err != nil {
t.Fatalf("%+v", err)
}
}
//func newTempDir(t *testing.T) string {
// dir, err := ioutil.TempDir("", "git-get-repo-")
// checkFatal(t, errors.Wrap(err, "Failed creating test repo directory"))
//
// // Automatically remove repo when test is over
// t.Cleanup(func() {
// err := os.RemoveAll(dir)
// if err != nil {
// t.Errorf("failed cleaning up repo")
// }
// })
//
// return dir
//}
func newTestRepo(t *testing.T) *git.Repository {
fs := memfs.New()
storage := memory.NewStorage()
repo, err := git.Init(storage, fs)
checkFatal(t, errors.Wrap(err, "Failed initializing a temp repo"))
return repo
}
func createFile(t *testing.T, repo *git.Repository, name string) {
wt, err := repo.Worktree()
checkFatal(t, errors.Wrap(err, "Failed getting worktree"))
file, err := wt.Filesystem.Create(name)
checkFatal(t, errors.Wrap(err, "Failed creating a file"))
_, err = file.Write([]byte("I'm a file"))
checkFatal(t, errors.Wrap(err, "Failed writing a file"))
}
func stageFile(t *testing.T, repo *git.Repository, name string) {
wt, err := repo.Worktree()
checkFatal(t, errors.Wrap(err, "Failed getting worktree"))
_, err = wt.Add(name)
checkFatal(t, errors.Wrap(err, "Failed adding file to index"))
}
func createCommit(t *testing.T, repo *git.Repository, msg string) {
wt, err := repo.Worktree()
checkFatal(t, errors.Wrap(err, "Failed getting worktree"))
opts := &git.CommitOptions{
Author: &object.Signature{
Name: "Some Guy",
Email: "someguy@example.com",
When: time.Date(2000, 01, 01, 16, 00, 00, 0, time.UTC),
},
}
_, err = wt.Commit(msg, opts)
checkFatal(t, errors.Wrap(err, "Failed creating commit"))
}
//
//func createBranch(t *testing.T, repo *git.Repository, name string) *git.Branch {
// head, err := repo.Head()
// checkFatal(t, errors.Wrap(err, "Failed getting repo head"))
//
// commit, err := repo.LookupCommit(head.Target())
// checkFatal(t, errors.Wrap(err, "Failed getting commit id from head"))
//
// branch, err := repo.CreateBranch(name, commit, false)
// checkFatal(t, errors.Wrap(err, "Failed creating branch"))
//
// return branch
//}
//
//func checkoutBranch(t *testing.T, repo *git.Repository, name string) {
// branch, err := repo.LookupBranch(name, git.BranchAll)
//
// // If branch can't be found, let's check if it's a remote branch
// if branch == nil {
// branch, err = repo.LookupBranch("origin/"+name, git.BranchAll)
// }
// checkFatal(t, errors.Wrap(err, "Failed looking up branch"))
//
// // If branch is remote, we need to create a local one first
// if branch.IsRemote() {
// commit, err := repo.LookupCommit(branch.Target())
// checkFatal(t, errors.Wrap(err, "Failed looking up commit"))
//
// localBranch, err := repo.CreateBranch(name, commit, false)
// checkFatal(t, errors.Wrap(err, "Failed creating local branch"))
//
// err = localBranch.SetUpstream("origin/" + name)
// checkFatal(t, errors.Wrap(err, "Failed setting upstream"))
// }
//
// err = repo.SetHead("refs/heads/" + name)
// checkFatal(t, errors.Wrap(err, "Failed setting head"))
//
// options := &git.CheckoutOpts{
// Strategy: git.CheckoutForce,
// }
// err = repo.CheckoutHead(options)
// checkFatal(t, errors.Wrap(err, "Failed checking out tree"))
//}

8
new/repo.go Normal file
View File

@@ -0,0 +1,8 @@
package new
import "github.com/go-git/go-git/v5"
type Repo struct {
repo *git.Repository
Status *RepoStatus
}

76
new/repo_test.go Normal file
View File

@@ -0,0 +1,76 @@
package new
import "testing"
func TestRepoEmpty(t *testing.T) {
repo := newTestRepo(t)
wt, err := repo.Worktree()
checkFatal(t, err)
status, err := wt.Status()
if !status.IsClean() {
t.Errorf("Empty repo should be clean")
}
}
func TestRepoWithUntrackedFile(t *testing.T) {
repo := newTestRepo(t)
createFile(t, repo, "file")
wt, err := repo.Worktree()
checkFatal(t, err)
status, err := wt.Status()
if status.IsClean() {
t.Errorf("Repo with untracked file should not be clean")
}
if !status.IsUntracked("file") {
t.Errorf("New file should be untracked")
}
}
func TestRepoWithStagedFile(t *testing.T) {
repo := newTestRepo(t)
createFile(t, repo, "file")
stageFile(t, repo, "file")
wt, err := repo.Worktree()
checkFatal(t, err)
status, err := wt.Status()
if status.IsClean() {
t.Errorf("Repo with staged file should not be clean")
}
if status.IsUntracked("file") {
t.Errorf("Staged file should not be untracked")
}
}
func TestRepoWithSingleCommit(t *testing.T) {
repo := newTestRepo(t)
createFile(t, repo, "file")
stageFile(t, repo, "file")
createCommit(t, repo, "Initial commit")
wt, err := repo.Worktree()
checkFatal(t, err)
status, err := wt.Status()
if !status.IsClean() {
t.Errorf("Repo with committed file should be clean")
}
if status.IsUntracked("file") {
t.Errorf("Committed file should not be untracked")
}
}
func TestStatusWithModifiedFile(t *testing.T) {
//todo modified but not staged
}
func TestStatusWithUntrackedButIgnoredFile(t *testing.T) {
//todo
}

48
new/status.go Normal file
View File

@@ -0,0 +1,48 @@
package new
import (
"github.com/go-git/go-git/v5"
"github.com/pkg/errors"
)
type RepoStatus struct {
HasUntrackedFiles bool
HasUncommittedChanges bool
Branches map[string]BranchStatus
}
type BranchStatus struct {
Name string
IsRemote bool
HasUpstream bool
NeedsPull bool
NeedsPush bool
Ahead int
Behind int
}
func (r *Repo) LoadStatus() error {
wt, err := r.repo.Worktree()
if err != nil {
return errors.Wrap(err, "Failed getting worktree")
}
status, err := wt.Status()
if err != nil {
return errors.Wrap(err, "Failed getting worktree status")
}
r.Status.HasUncommittedChanges = !status.IsClean()
r.Status.HasUntrackedFiles = hasUntracked(status)
return nil
}
// hasUntracked returns true if there's any untracked file in the worktree
func hasUntracked(status git.Status) bool {
for _, fs := range status {
if fs.Worktree == git.Untracked {
return true
}
}
return false
}