2016-08-06 23:40:59 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2020-08-18 11:25:31 -05:00
|
|
|
"bytes"
|
2016-08-07 01:33:06 -05:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2020-08-18 11:25:31 -05:00
|
|
|
"strings"
|
2016-08-06 23:40:59 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2020-08-18 11:25:31 -05:00
|
|
|
|
2022-12-09 14:43:11 -06:00
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp/packet"
|
2016-08-06 23:40:59 -05:00
|
|
|
)
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
// Tests
|
|
|
|
|
2022-02-24 07:27:26 -06:00
|
|
|
func TestRebaseInMemoryWithConflict(t *testing.T) {
|
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
seedTestRepo(t, repo)
|
|
|
|
|
2024-12-16 17:50:57 -06:00
|
|
|
// Create two branches with common history, where both modify "common-file"
|
|
|
|
// in a conflicting way.
|
|
|
|
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
|
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
checkFatal(t, createBranch(repo, "branch-a"))
|
|
|
|
checkFatal(t, createBranch(repo, "branch-b"))
|
|
|
|
|
|
|
|
checkFatal(t, repo.SetHead("refs/heads/branch-a"))
|
2024-12-16 17:50:57 -06:00
|
|
|
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
|
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
|
|
|
|
checkFatal(t, repo.SetHead("refs/heads/branch-b"))
|
2024-12-16 17:50:57 -06:00
|
|
|
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
|
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
|
|
|
|
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
|
2024-12-16 17:50:57 -06:00
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference)
|
2024-12-16 17:50:57 -06:00
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
|
2024-12-16 17:50:57 -06:00
|
|
|
// We then rebase "branch-b" onto "branch-a" in-memory, which should result
|
|
|
|
// in a conflict.
|
|
|
|
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
|
|
|
|
checkFatal(t, err)
|
2022-02-24 07:27:26 -06:00
|
|
|
|
2024-12-16 17:50:57 -06:00
|
|
|
_, err = rebase.Next()
|
2022-02-24 07:27:26 -06:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
2024-12-16 17:50:57 -06:00
|
|
|
index, err := rebase.InmemoryIndex()
|
2022-02-24 07:27:26 -06:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
2024-12-16 17:50:57 -06:00
|
|
|
// We simply resolve the conflict and commit the rebase.
|
|
|
|
if !index.HasConflicts() {
|
|
|
|
t.Fatal("expected index to have conflicts")
|
|
|
|
}
|
|
|
|
|
|
|
|
conflict, err := index.Conflict("common-file")
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
resolvedEntry := *conflict.Our
|
|
|
|
resolvedEntry.Id = resolvedBlobID
|
|
|
|
checkFatal(t, index.Add(&resolvedEntry))
|
|
|
|
checkFatal(t, index.RemoveConflict("common-file"))
|
|
|
|
|
|
|
|
var commitID Oid
|
|
|
|
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
|
|
|
|
checkFatal(t, rebase.Finish())
|
|
|
|
|
|
|
|
// And then assert that we can look up the new merge commit, and that the
|
|
|
|
// "common-file" has the expected contents.
|
|
|
|
commit, err := repo.LookupCommit(&commitID)
|
|
|
|
checkFatal(t, err)
|
|
|
|
if commit.Message() != "rebased message" {
|
|
|
|
t.Fatalf("unexpected commit message %q", commit.Message())
|
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := commit.Tree()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
|
|
|
|
checkFatal(t, err)
|
|
|
|
if string(blob.Contents()) != "resolved contents" {
|
|
|
|
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
|
|
|
|
}
|
2022-02-24 07:27:26 -06:00
|
|
|
}
|
|
|
|
|
2016-08-07 02:00:14 -05:00
|
|
|
func TestRebaseAbort(t *testing.T) {
|
|
|
|
// TEST DATA
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
branchName := "emile"
|
|
|
|
masterCommit := "something"
|
|
|
|
emileCommits := []string{
|
|
|
|
"fou",
|
|
|
|
"barre",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs
|
|
|
|
expectedHistory := []string{
|
|
|
|
"Test rebase, Baby! " + emileCommits[1],
|
|
|
|
"Test rebase, Baby! " + emileCommits[0],
|
|
|
|
"This is a commit\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
repo := createTestRepo(t)
|
2016-08-07 02:31:42 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
2016-08-07 02:00:14 -05:00
|
|
|
seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
// Setup a repo with 2 branches and a different tree
|
2020-12-05 09:23:44 -06:00
|
|
|
err := setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
|
2016-08-07 02:00:14 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
// Create several commits in emile
|
|
|
|
for _, commit := range emileCommits {
|
2020-12-05 09:23:44 -06:00
|
|
|
_, err = commitSomething(repo, commit, commit, commitOptions{})
|
2016-08-07 02:00:14 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check history
|
|
|
|
actualHistory, err := commitMsgsList(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
assertStringList(t, expectedHistory, actualHistory)
|
|
|
|
|
|
|
|
// Rebase onto master
|
2020-08-18 11:25:31 -05:00
|
|
|
rebase, err := performRebaseOnto(repo, "master", nil)
|
2016-08-07 02:00:14 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
defer rebase.Free()
|
|
|
|
|
|
|
|
// Abort rebase
|
|
|
|
rebase.Abort()
|
|
|
|
|
|
|
|
// Check history is still the same
|
|
|
|
actualHistory, err = commitMsgsList(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
assertStringList(t, expectedHistory, actualHistory)
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func TestRebaseNoConflicts(t *testing.T) {
|
|
|
|
// TEST DATA
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
branchName := "emile"
|
|
|
|
masterCommit := "something"
|
|
|
|
emileCommits := []string{
|
|
|
|
"fou",
|
|
|
|
"barre",
|
|
|
|
"ouich",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs
|
|
|
|
expectedHistory := []string{
|
2016-08-07 02:00:14 -05:00
|
|
|
"Test rebase, Baby! " + emileCommits[2],
|
|
|
|
"Test rebase, Baby! " + emileCommits[1],
|
|
|
|
"Test rebase, Baby! " + emileCommits[0],
|
|
|
|
"Test rebase, Baby! " + masterCommit,
|
2016-08-07 01:33:06 -05:00
|
|
|
"This is a commit\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
repo := createTestRepo(t)
|
2016-08-07 02:31:42 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
2016-08-07 01:33:06 -05:00
|
|
|
seedTestRepo(t, repo)
|
|
|
|
|
2016-09-05 23:15:10 -05:00
|
|
|
// Try to open existing rebase
|
2016-10-31 18:57:23 -05:00
|
|
|
oRebase, err := repo.OpenRebase(nil)
|
2016-09-05 23:15:10 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Did not expect to find a rebase in progress")
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
// Setup a repo with 2 branches and a different tree
|
2020-12-05 09:23:44 -06:00
|
|
|
err = setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
|
2016-08-07 01:33:06 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
// Create several commits in emile
|
|
|
|
for _, commit := range emileCommits {
|
2020-12-05 09:23:44 -06:00
|
|
|
_, err = commitSomething(repo, commit, commit, commitOptions{})
|
2016-08-07 01:33:06 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase onto master
|
2020-08-18 11:25:31 -05:00
|
|
|
rebase, err := performRebaseOnto(repo, "master", nil)
|
2016-08-07 02:00:14 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
defer rebase.Free()
|
|
|
|
|
2016-09-05 23:15:10 -05:00
|
|
|
// Open existing rebase
|
2016-10-31 18:57:23 -05:00
|
|
|
oRebase, err = repo.OpenRebase(nil)
|
2016-09-05 23:15:10 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
defer oRebase.Free()
|
|
|
|
if oRebase == nil {
|
|
|
|
t.Fatal("Expected to find an existing rebase in progress")
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:00:14 -05:00
|
|
|
// Finish the rebase properly
|
|
|
|
err = rebase.Finish()
|
2016-08-07 01:33:06 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
2016-09-13 23:42:58 -05:00
|
|
|
// Check no more rebase is in progress
|
2016-10-31 18:57:23 -05:00
|
|
|
oRebase, err = repo.OpenRebase(nil)
|
2016-09-13 23:42:58 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Did not expect to find a rebase in progress")
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
// Check history is in correct order
|
|
|
|
actualHistory, err := commitMsgsList(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
assertStringList(t, expectedHistory, actualHistory)
|
2020-08-18 11:25:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRebaseGpgSigned(t *testing.T) {
|
|
|
|
// TEST DATA
|
|
|
|
|
|
|
|
entity, err := openpgp.NewEntity("Namey mcnameface", "test comment", "test@example.com", nil)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
rebaseOpts, err := DefaultRebaseOptions()
|
2020-08-18 11:25:31 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
signCommitContent := func(commitContent string) (string, string, error) {
|
|
|
|
cipherText := new(bytes.Buffer)
|
|
|
|
err := openpgp.ArmoredDetachSignText(cipherText, entity, strings.NewReader(commitContent), &packet.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return "", "", errors.New("error signing payload")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cipherText.String(), "", nil
|
|
|
|
}
|
2020-12-05 09:23:44 -06:00
|
|
|
rebaseOpts.CommitSigningCallback = signCommitContent
|
2020-08-18 11:25:31 -05:00
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
commitOpts := commitOptions{
|
2020-08-18 11:25:31 -05:00
|
|
|
CommitSigningCallback: signCommitContent,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
branchName := "emile"
|
|
|
|
masterCommit := "something"
|
|
|
|
emileCommits := []string{
|
|
|
|
"fou",
|
|
|
|
"barre",
|
|
|
|
"ouich",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs
|
|
|
|
expectedHistory := []string{
|
|
|
|
"Test rebase, Baby! " + emileCommits[2],
|
|
|
|
"Test rebase, Baby! " + emileCommits[1],
|
|
|
|
"Test rebase, Baby! " + emileCommits[0],
|
|
|
|
"Test rebase, Baby! " + masterCommit,
|
|
|
|
"This is a commit\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
repo := createTestRepo(t)
|
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
seedTestRepoOpt(t, repo, commitOpts)
|
|
|
|
|
|
|
|
// Try to open existing rebase
|
|
|
|
_, err = repo.OpenRebase(nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Did not expect to find a rebase in progress")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a repo with 2 branches and a different tree
|
|
|
|
err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
// Create several commits in emile
|
|
|
|
for _, commit := range emileCommits {
|
|
|
|
_, err = commitSomething(repo, commit, commit, commitOpts)
|
|
|
|
checkFatal(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebase onto master
|
2020-12-05 09:23:44 -06:00
|
|
|
rebase, err := performRebaseOnto(repo, "master", &rebaseOpts)
|
2020-08-18 11:25:31 -05:00
|
|
|
checkFatal(t, err)
|
|
|
|
defer rebase.Free()
|
2016-08-07 01:33:06 -05:00
|
|
|
|
2020-08-18 11:25:31 -05:00
|
|
|
// Finish the rebase properly
|
|
|
|
err = rebase.Finish()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
// Check history is in correct order
|
|
|
|
actualHistory, err := commitMsgsList(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
assertStringList(t, expectedHistory, actualHistory)
|
|
|
|
|
|
|
|
checkAllCommitsSigned(t, entity, repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAllCommitsSigned(t *testing.T, entity *openpgp.Entity, repo *Repository) {
|
|
|
|
head, err := headCommit(repo)
|
|
|
|
checkFatal(t, err)
|
|
|
|
defer head.Free()
|
|
|
|
|
|
|
|
parent := head
|
|
|
|
|
|
|
|
err = checkCommitSigned(t, entity, parent)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
for parent.ParentCount() != 0 {
|
|
|
|
parent = parent.Parent(0)
|
|
|
|
defer parent.Free()
|
|
|
|
|
|
|
|
err = checkCommitSigned(t, entity, parent)
|
|
|
|
checkFatal(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) error {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
signature, signedData, err := commit.ExtractSignature()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("No signature on commit\n%s", commit.ContentToSign())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-09 14:43:11 -06:00
|
|
|
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature), nil)
|
2020-08-18 11:25:31 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Utils
|
2020-12-05 09:23:44 -06:00
|
|
|
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
|
2016-08-07 01:33:06 -05:00
|
|
|
// Create a new branch from master
|
|
|
|
err := createBranch(repo, branchName)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
|
|
|
|
// Create a commit in master
|
2020-12-05 09:23:44 -06:00
|
|
|
_, err = commitSomething(repo, masterCommit, masterCommit, commitOpts)
|
2016-08-07 01:33:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch to emile
|
|
|
|
err = repo.SetHead("refs/heads/" + branchName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check master commit is not in emile branch
|
|
|
|
if entryExists(repo, masterCommit) {
|
|
|
|
return errors.New(masterCommit + " entry should not exist in " + branchName + " branch.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
func performRebaseOnto(repo *Repository, branch string, rebaseOpts *RebaseOptions) (*Rebase, error) {
|
2016-08-07 01:33:06 -05:00
|
|
|
master, err := repo.LookupBranch(branch, BranchLocal)
|
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
defer master.Free()
|
|
|
|
|
|
|
|
onto, err := repo.AnnotatedCommitFromRef(master.Reference)
|
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
defer onto.Free()
|
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
// Init rebase
|
2020-12-05 09:23:44 -06:00
|
|
|
rebase, err := repo.InitRebase(nil, nil, onto, rebaseOpts)
|
2016-08-07 01:33:06 -05:00
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
// Check no operation has been started yet
|
2016-10-31 18:57:23 -05:00
|
|
|
rebaseOperationIndex, err := rebase.CurrentOperationIndex()
|
2017-07-08 13:58:08 -05:00
|
|
|
if rebaseOperationIndex != RebaseNoOperation && err != ErrRebaseNoOperation {
|
2016-08-07 02:31:42 -05:00
|
|
|
return nil, errors.New("No operation should have been started yet")
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
// Iterate in rebase operations regarding operation count
|
|
|
|
opCount := int(rebase.OperationCount())
|
2016-08-07 01:33:06 -05:00
|
|
|
for op := 0; op < opCount; op++ {
|
|
|
|
operation, err := rebase.Next()
|
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
// Check operation index is correct
|
2016-10-31 18:57:23 -05:00
|
|
|
rebaseOperationIndex, err = rebase.CurrentOperationIndex()
|
|
|
|
if int(rebaseOperationIndex) != op {
|
2016-08-07 02:31:42 -05:00
|
|
|
return nil, errors.New("Bad operation index")
|
|
|
|
}
|
|
|
|
if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) {
|
|
|
|
return nil, errors.New("Rebase operations should be equal")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current rebase operation created commit
|
2016-10-31 18:57:23 -05:00
|
|
|
commit, err := repo.LookupCommit(operation.Id)
|
2016-08-07 01:33:06 -05:00
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
defer commit.Free()
|
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
// Apply commit
|
2016-10-31 18:57:23 -05:00
|
|
|
err = rebase.Commit(operation.Id, signature(), signature(), commit.Message())
|
2016-08-07 01:33:06 -05:00
|
|
|
if err != nil {
|
2016-08-07 02:00:14 -05:00
|
|
|
return nil, err
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 02:00:14 -05:00
|
|
|
return rebase, nil
|
2016-08-07 01:33:06 -05:00
|
|
|
}
|
|
|
|
|
2016-08-07 02:31:42 -05:00
|
|
|
func operationsAreEqual(l, r *RebaseOperation) bool {
|
2016-10-31 18:57:23 -05:00
|
|
|
return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String()
|
2016-08-07 02:31:42 -05:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func createBranch(repo *Repository, branch string) error {
|
|
|
|
commit, err := headCommit(repo)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer commit.Free()
|
2016-08-06 23:40:59 -05:00
|
|
|
_, err = repo.CreateBranch(branch, commit, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func signature() *Signature {
|
|
|
|
return &Signature{
|
|
|
|
Name: "Emile",
|
|
|
|
Email: "emile@emile.com",
|
|
|
|
When: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func headCommit(repo *Repository) (*Commit, error) {
|
2016-08-06 23:40:59 -05:00
|
|
|
head, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer head.Free()
|
|
|
|
|
|
|
|
commit, err := repo.LookupCommit(head.Target())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
}
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func headTree(repo *Repository) (*Tree, error) {
|
|
|
|
headCommit, err := headCommit(repo)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer headCommit.Free()
|
|
|
|
|
|
|
|
tree, err := headCommit.Tree()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
func commitSomething(repo *Repository, something, content string, commitOpts commitOptions) (*Oid, error) {
|
2016-08-07 01:33:06 -05:00
|
|
|
headCommit, err := headCommit(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer headCommit.Free()
|
2016-08-06 23:40:59 -05:00
|
|
|
|
|
|
|
index, err := NewIndex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer index.Free()
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
blobOID, err := repo.CreateBlobFromBuffer([]byte(content))
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &IndexEntry{
|
|
|
|
Mode: FilemodeBlob,
|
|
|
|
Id: blobOID,
|
|
|
|
Path: something,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := index.Add(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTreeOID, err := index.WriteTreeTo(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTree, err := repo.LookupTree(newTreeOID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer newTree.Free()
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 02:00:14 -05:00
|
|
|
commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-18 11:25:31 -05:00
|
|
|
if commitOpts.CommitSigningCallback != nil {
|
|
|
|
commit, err := repo.LookupCommit(commit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCallback)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newCommit, err := repo.LookupCommit(oid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
head, err := repo.Head()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = repo.References.Create(
|
|
|
|
head.Name(),
|
|
|
|
newCommit.Id(),
|
|
|
|
true,
|
|
|
|
"repoint to signed commit",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 09:23:44 -06:00
|
|
|
checkoutOpts := &CheckoutOptions{
|
2016-08-06 23:40:59 -05:00
|
|
|
Strategy: CheckoutRemoveUntracked | CheckoutForce,
|
|
|
|
}
|
2020-12-05 09:23:44 -06:00
|
|
|
err = repo.CheckoutIndex(index, checkoutOpts)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return commit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func entryExists(repo *Repository, file string) bool {
|
2016-08-07 01:33:06 -05:00
|
|
|
headTree, err := headTree(repo)
|
2016-08-06 23:40:59 -05:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer headTree.Free()
|
|
|
|
|
2016-08-06 23:40:59 -05:00
|
|
|
_, err = headTree.EntryByPath(file)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func commitMsgsList(repo *Repository) ([]string, error) {
|
|
|
|
head, err := headCommit(repo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-06 23:40:59 -05:00
|
|
|
}
|
2016-08-07 01:33:06 -05:00
|
|
|
defer head.Free()
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
var commits []string
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
parent := head.Parent(0)
|
|
|
|
defer parent.Free()
|
|
|
|
commits = append(commits, head.Message(), parent.Message())
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
for parent.ParentCount() != 0 {
|
|
|
|
parent = parent.Parent(0)
|
|
|
|
defer parent.Free()
|
|
|
|
commits = append(commits, parent.Message())
|
|
|
|
}
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
return commits, nil
|
|
|
|
}
|
2016-08-06 23:40:59 -05:00
|
|
|
|
2016-08-07 01:33:06 -05:00
|
|
|
func assertStringList(t *testing.T, expected, actual []string) {
|
|
|
|
if len(expected) != len(actual) {
|
|
|
|
t.Fatal("Lists are not the same size, expected " + strconv.Itoa(len(expected)) +
|
|
|
|
", got " + strconv.Itoa(len(actual)))
|
|
|
|
}
|
|
|
|
for index, element := range expected {
|
|
|
|
if element != actual[index] {
|
|
|
|
t.Error("Expected element " + strconv.Itoa(index) + " to be " + element + ", got " + actual[index])
|
|
|
|
}
|
2016-08-06 23:40:59 -05:00
|
|
|
}
|
|
|
|
}
|