2019-03-27 17:33:00 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2019-12-14 06:21:06 +08:00
|
|
|
"bytes"
|
2021-09-22 13:38:34 +08:00
|
|
|
"io"
|
2019-03-27 17:33:00 +08:00
|
|
|
"path/filepath"
|
2021-12-23 16:32:29 +08:00
|
|
|
"strings"
|
2019-03-27 17:33:00 +08:00
|
|
|
"testing"
|
|
|
|
|
2020-08-12 04:05:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-17 20:34:35 +08:00
|
|
|
|
2019-03-27 17:33:00 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetFormatPatch(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch")
|
2020-08-12 04:05:34 +08:00
|
|
|
defer util.RemoveAll(clonedPath)
|
2019-03-27 17:33:00 +08:00
|
|
|
assert.NoError(t, err)
|
2021-12-23 16:32:29 +08:00
|
|
|
repo, err := OpenRepository(clonedPath)
|
2019-11-13 15:01:19 +08:00
|
|
|
defer repo.Close()
|
2021-12-23 16:32:29 +08:00
|
|
|
assert.NoError(t, err)
|
2019-12-14 06:21:06 +08:00
|
|
|
rd := &bytes.Buffer{}
|
|
|
|
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
|
2019-03-27 17:33:00 +08:00
|
|
|
assert.NoError(t, err)
|
2021-09-22 13:38:34 +08:00
|
|
|
patchb, err := io.ReadAll(rd)
|
2019-03-27 17:33:00 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
patch := string(patchb)
|
|
|
|
assert.Regexp(t, "^From 8d92fc95", patch)
|
|
|
|
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
|
|
|
|
}
|
2021-12-23 16:32:29 +08:00
|
|
|
|
|
|
|
func TestReadPatch(t *testing.T) {
|
|
|
|
// Ensure we can read the patch files
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
repo, err := OpenRepository(bareRepo1Path)
|
|
|
|
defer repo.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// This patch doesn't exist
|
|
|
|
noFile, err := repo.ReadPatchCommit(0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// This patch is an empty one (sometimes it's a 404)
|
|
|
|
noCommit, err := repo.ReadPatchCommit(1)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// This patch is legit and should return a commit
|
|
|
|
oldCommit, err := repo.ReadPatchCommit(2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Empty(t, noFile)
|
|
|
|
assert.Empty(t, noCommit)
|
|
|
|
assert.Len(t, oldCommit, 40)
|
|
|
|
assert.True(t, oldCommit == "6e8e2a6f9efd71dbe6917816343ed8415ad696c3")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWritePullHead(t *testing.T) {
|
|
|
|
// Ensure we can write SHA1 head corresponding to PR and open them
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
repo, err := OpenRepository(bareRepo1Path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer repo.Close()
|
|
|
|
// Try to open non-existing Pull
|
|
|
|
_, err = repo.ReadPullHead(0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// Write a fake sha1 with only 40 zeros
|
|
|
|
newCommit := strings.Repeat("0", 40)
|
|
|
|
err = repo.WritePullHead(1, newCommit)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
headFile := filepath.Join(repo.Path, "refs/pull/1/head")
|
|
|
|
// Remove file after the test
|
|
|
|
defer util.Remove(headFile)
|
|
|
|
assert.FileExists(t, headFile)
|
|
|
|
// Read the file created
|
|
|
|
headContents, err := repo.ReadPullHead(1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, string(headContents), 40)
|
|
|
|
assert.True(t, string(headContents) == newCommit)
|
|
|
|
}
|