mirror of
https://github.com/grdl/git-get.git
synced 2026-02-12 00:58:59 +00:00
Add helper functions for creating test repos
Also add a rough todo list of features in readme
This commit is contained in:
14
README.md
14
README.md
@@ -31,3 +31,17 @@ How to build with `libgit2` statically linked into a single executable without d
|
|||||||
go build -i --tags static
|
go build -i --tags static
|
||||||
go test --tags static
|
go test --tags static
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
Show repo status:
|
||||||
|
- uncommitted changes
|
||||||
|
- untracked files
|
||||||
|
- push needed (master, branch1, branch2 etc.)
|
||||||
|
- pull needed
|
||||||
|
- upstream missing
|
||||||
|
- stashes?
|
||||||
|
- state (merging, rebasing, conflict, cherry picking etc)
|
||||||
|
- ahead/behind?
|
||||||
|
- submodules?
|
||||||
|
|
||||||
|
|||||||
136
git-get_test.go
136
git-get_test.go
@@ -1,33 +1,143 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/libgit2/git2go/v30"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
git "github.com/libgit2/git2go/v30"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTempRepo(t *testing.T) *git.Repository {
|
const (
|
||||||
|
ReadmeFile = "README.md"
|
||||||
|
ReadmeContent = "I'm a readme file\n"
|
||||||
|
CommitterName = "Some Guy"
|
||||||
|
CommitterEmail = "someguy@example.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cleanupRepo(t *testing.T, repo *git.Repository) {
|
||||||
|
err := os.RemoveAll(repo.Workdir())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed cleaning up repo %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTempRepo() (*git.Repository, error) {
|
||||||
dir, err := ioutil.TempDir("", "test-repo-")
|
dir, err := ioutil.TempDir("", "test-repo-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't create a temp repo directory: %s", err.Error())
|
return nil, fmt.Errorf("failed creating a temp repo %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_ = os.RemoveAll(dir)
|
|
||||||
})
|
|
||||||
|
|
||||||
repo, err := git.InitRepository(dir, false)
|
repo, err := git.InitRepository(dir, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Couldn't init a temp repo: %s", err.Error())
|
return nil, fmt.Errorf("failed initializing a temp repo %s", err.Error())
|
||||||
}
|
}
|
||||||
return repo
|
|
||||||
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTempRepo(t *testing.T) {
|
func newTempRepoWithUntracked() (*git.Repository, error) {
|
||||||
repo := createTempRepo(t)
|
repo, err := newTempRepo()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if repo.IsBare() {
|
err = ioutil.WriteFile(path.Join(repo.Workdir(), ReadmeFile), []byte(ReadmeContent), 0644)
|
||||||
t.Errorf("Repository %s should not be bare", repo.Path())
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed writing a file %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTempRepoWithStaged() (*git.Repository, error) {
|
||||||
|
repo, err := newTempRepoWithUntracked()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
index, err := repo.Index()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed getting repo index %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.AddByPath(ReadmeFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed adding file to index %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.Write()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed writing index %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTempRepoWithCommit() (*git.Repository, error) {
|
||||||
|
repo, err := newTempRepoWithStaged()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
index, err := repo.Index()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed creating a temp repo %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
treeId, err := index.WriteTree()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed building tree from index %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
tree, err := repo.LookupTree(treeId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed looking up tree %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
signature := &git.Signature{
|
||||||
|
Name: CommitterName,
|
||||||
|
Email: CommitterEmail,
|
||||||
|
When: time.Date(2000, 01, 01, 16, 00, 00, 0, time.UTC),
|
||||||
|
}
|
||||||
|
message := "Initial commit"
|
||||||
|
|
||||||
|
_, err = repo.CreateCommit("HEAD", signature, signature, message, tree)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed creating commit %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return repo, nil
|
||||||
|
}
|
||||||
|
func TestStatus(t *testing.T) {
|
||||||
|
repo, err := git.OpenRepository("/tmp/testgit")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
defer cleanupRepo(t, repo)
|
||||||
|
|
||||||
|
opts := &git.StatusOptions{
|
||||||
|
Show: git.StatusShowIndexAndWorkdir,
|
||||||
|
Flags: git.StatusOptIncludeUntracked | git.StatusOptIncludeIgnored,
|
||||||
|
}
|
||||||
|
|
||||||
|
status, _ := repo.StatusList(opts)
|
||||||
|
entryCount, _ := status.EntryCount()
|
||||||
|
for i := 0; i < entryCount; i++ {
|
||||||
|
entry, _ := status.ByIndex(i)
|
||||||
|
fmt.Println(entry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreatingRepoWithCommit(t *testing.T) {
|
||||||
|
repo, err := newTempRepoWithCommit()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed creating test repository %s", err.Error())
|
||||||
|
}
|
||||||
|
defer cleanupRepo(t, repo)
|
||||||
|
fmt.Println(repo.Path())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user