2019-03-27 12:33:00 +03:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-27 12:33:00 +03:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2019-12-14 01:21:06 +03:00
|
|
|
"bytes"
|
2021-09-22 08:38:34 +03:00
|
|
|
"io"
|
2019-03-27 12:33:00 +03:00
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-27 12:33:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetFormatPatch(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-09-04 18:14:53 +03:00
|
|
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 22:13:41 +03:00
|
|
|
repo, err := openRepositoryWithDefaultContext(clonedPath)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
2019-11-13 10:01:19 +03:00
|
|
|
defer repo.Close()
|
2022-01-29 15:41:44 +03:00
|
|
|
|
2019-12-14 01:21:06 +03:00
|
|
|
rd := &bytes.Buffer{}
|
|
|
|
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:38:34 +03:00
|
|
|
patchb, err := io.ReadAll(rd)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-27 12:33:00 +03:00
|
|
|
patch := string(patchb)
|
|
|
|
assert.Regexp(t, "^From 8d92fc95", patch)
|
|
|
|
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
|
|
|
|
}
|
2021-12-23 11:32:29 +03:00
|
|
|
|
|
|
|
func TestReadPatch(t *testing.T) {
|
|
|
|
// Ensure we can read the patch files
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 22:13:41 +03:00
|
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-23 11:32:29 +03:00
|
|
|
defer repo.Close()
|
|
|
|
// This patch doesn't exist
|
|
|
|
noFile, err := repo.ReadPatchCommit(0)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
|
2021-12-23 11:32:29 +03:00
|
|
|
// This patch is an empty one (sometimes it's a 404)
|
|
|
|
noCommit, err := repo.ReadPatchCommit(1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
|
2021-12-23 11:32:29 +03:00
|
|
|
// This patch is legit and should return a commit
|
|
|
|
oldCommit, err := repo.ReadPatchCommit(2)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-23 11:32:29 +03:00
|
|
|
|
|
|
|
assert.Empty(t, noFile)
|
|
|
|
assert.Empty(t, noCommit)
|
|
|
|
assert.Len(t, oldCommit, 40)
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.Equal(t, "6e8e2a6f9efd71dbe6917816343ed8415ad696c3", oldCommit)
|
2021-12-23 11:32:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadWritePullHead(t *testing.T) {
|
|
|
|
// Ensure we can write SHA1 head corresponding to PR and open them
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-01-29 15:41:44 +03:00
|
|
|
|
|
|
|
// As we are writing we should clone the repository first
|
2022-09-04 18:14:53 +03:00
|
|
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 22:13:41 +03:00
|
|
|
repo, err := openRepositoryWithDefaultContext(clonedPath)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
2021-12-23 11:32:29 +03:00
|
|
|
defer repo.Close()
|
2022-01-29 15:41:44 +03:00
|
|
|
|
2021-12-23 11:32:29 +03:00
|
|
|
// Try to open non-existing Pull
|
2021-12-23 16:44:00 +03:00
|
|
|
_, err = repo.GetRefCommitID(PullPrefix + "0/head")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.Error(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
|
2021-12-23 11:32:29 +03:00
|
|
|
// Write a fake sha1 with only 40 zeros
|
2021-12-23 16:44:00 +03:00
|
|
|
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
|
|
|
|
err = repo.SetReference(PullPrefix+"1/head", newCommit)
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:32:29 +03:00
|
|
|
// Read the file created
|
2021-12-23 16:44:00 +03:00
|
|
|
headContents, err := repo.GetRefCommitID(PullPrefix + "1/head")
|
2022-01-29 15:41:44 +03:00
|
|
|
if err != nil {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-01-29 15:41:44 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-20 13:02:49 +03:00
|
|
|
assert.Len(t, headContents, 40)
|
2024-07-30 22:41:10 +03:00
|
|
|
assert.Equal(t, newCommit, headContents)
|
2022-01-29 15:41:44 +03:00
|
|
|
|
|
|
|
// Remove file after the test
|
|
|
|
err = repo.RemoveReference(PullPrefix + "1/head")
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2021-12-23 11:32:29 +03:00
|
|
|
}
|
2023-08-04 05:53:15 +03:00
|
|
|
|
|
|
|
func TestGetCommitFilesChanged(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
repo, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2023-08-04 05:53:15 +03:00
|
|
|
defer repo.Close()
|
|
|
|
|
2024-03-12 07:21:27 +03:00
|
|
|
objectFormat, err := repo.GetObjectFormat()
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2024-03-12 07:21:27 +03:00
|
|
|
|
2023-08-04 05:53:15 +03:00
|
|
|
testCases := []struct {
|
|
|
|
base, head string
|
|
|
|
files []string
|
|
|
|
}{
|
|
|
|
{
|
2024-03-12 07:21:27 +03:00
|
|
|
objectFormat.EmptyObjectID().String(),
|
2023-08-04 05:53:15 +03:00
|
|
|
"95bb4d39648ee7e325106df01a621c530863a653",
|
|
|
|
[]string{"file1.txt"},
|
|
|
|
},
|
|
|
|
{
|
2024-03-12 07:21:27 +03:00
|
|
|
objectFormat.EmptyObjectID().String(),
|
2023-08-04 05:53:15 +03:00
|
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
|
|
[]string{"file2.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"95bb4d39648ee7e325106df01a621c530863a653",
|
|
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
|
|
[]string{"file2.txt"},
|
|
|
|
},
|
|
|
|
{
|
2024-03-12 07:21:27 +03:00
|
|
|
objectFormat.EmptyTree().String(),
|
2023-08-04 05:53:15 +03:00
|
|
|
"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
|
|
|
|
[]string{"file1.txt", "file2.txt"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
changedFiles, err := repo.GetFilesChangedBetween(tc.base, tc.head)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2023-08-04 05:53:15 +03:00
|
|
|
assert.ElementsMatch(t, tc.files, changedFiles)
|
|
|
|
}
|
|
|
|
}
|