github.com/azunymous/cdx@v0.0.0-20201122180449-fbb46cc4d252/vcs/gogit/vcs_setup_test.go (about)

     1  package gogit
     2  
     3  import (
     4  	"errors"
     5  	"github.com/go-git/go-billy/v5"
     6  	"github.com/go-git/go-git/v5"
     7  	"github.com/go-git/go-git/v5/plumbing/cache"
     8  	"github.com/go-git/go-git/v5/plumbing/object"
     9  	filesystem2 "github.com/go-git/go-git/v5/storage/filesystem"
    10  	"github.com/sirupsen/logrus"
    11  	"time"
    12  )
    13  
    14  // newTestRepo is a test version of NewRepo which creates an in memory repository
    15  func newTestRepo(fs billy.Filesystem) *Repo {
    16  	gr, err := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	return &Repo{gitRepo: gr, log: logrus.New()}
    21  
    22  }
    23  
    24  func createGitRepo(fs billy.Filesystem) {
    25  	_, _ = git.Init(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    26  	createCommit(fs, "example-git-file", "hello world!")
    27  }
    28  
    29  func createCommit(fs billy.Filesystem, filename, msg string) {
    30  	r, _ := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    31  	w, _ := r.Worktree()
    32  	// create a new file inside of the worktree of the project using the go standard library.
    33  	file, _ := fs.Create(filename)
    34  	_, _ = file.Write([]byte(msg))
    35  
    36  	// Adds the new file to the staging area.
    37  	_, _ = w.Add(filename)
    38  
    39  	// Commits the current staging area to the repository, with the new file
    40  	// just created.
    41  	_, _ = w.Commit("example go-git commit", &git.CommitOptions{
    42  		Author: &object.Signature{
    43  			Name:  "John Doe",
    44  			Email: "john@doe.org",
    45  			When:  time.Now(),
    46  		},
    47  	})
    48  }
    49  
    50  func createVersionTag(fs billy.Filesystem, tag string) {
    51  	r, _ := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    52  	revision, _ := r.ResolveRevision("HEAD")
    53  	_, _ = r.CreateTag(tag, *revision, nil)
    54  }
    55  
    56  func createAnnotatedVersionTag(fs billy.Filesystem, tag string) {
    57  	r, _ := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    58  	revision, _ := r.ResolveRevision("HEAD")
    59  	_, _ = r.CreateTag(tag, *revision, &git.CreateTagOptions{Message: " ", Tagger: &object.Signature{
    60  		Name:  "",
    61  		Email: "",
    62  		When:  time.Time{},
    63  	}})
    64  }
    65  
    66  func tagExistsAtHead(fs billy.Filesystem, tag string) error {
    67  	r, _ := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    68  	revision, _ := r.ResolveRevision("HEAD")
    69  	t, err := r.Tag(tag)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	if t.Hash() != *revision {
    74  		return errors.New("tag not at head")
    75  	}
    76  	return nil
    77  }
    78  
    79  func tagDoesNotExist(fs billy.Filesystem, tag string) error {
    80  	r, _ := git.Open(filesystem2.NewStorage(fs, cache.NewObjectLRUDefault()), fs)
    81  	_, err := r.Tag(tag)
    82  	if err == git.ErrTagNotFound {
    83  		return nil
    84  	} else {
    85  		return errors.New("tag exists")
    86  	}
    87  }