mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-10-30 22:11:07 +00:00 
			
		
		
		
	- `testPatch` is a function that is called to test a pull request and determine the state of the pull request. Checking for merge conflicts, check if the diff is empty and if the pull request modifies any protected files. - The checking for merge conflict and if the diff is empty used git commands that relied on a working tree to correctly functions. Forgejo store repositories in a bare format which do not contain a working tree. This means that a temporary copy was created every time a pull request had to be re-checked and for large repositories involving quite some I/O interaction. - This patch adjusts those codepaths to instead use newer Git plumbing commands that work without requiring a work tree and can thus be used directly on the bare repository. The merge conflict is now done via [`git-merge-tree(1)`](https://git-scm.com/docs/git-merge-tree/) and checking if the diff is empty is done via [`git-diff-tree(1)`](https://git-scm.com/docs/git-diff-tree). - If the function is called to test a patch where the head and base repository are not the same, then [Git alternate](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefalternateobjectdatabaseaalternateobjectdatabase) is used to make the head commit available in the base repository, this done on a per git command basis via the `GIT_ALTERNATE_OBJECT_DIRECTORIES` environment. - As far as I can understand the documentation and the existing code, there's no edge case that the new code cannot handle. It also results in a cleaner codepath, as the existing code did a lot of checking and merging in a more traditional approach that required a lot of (parsing) code, while the new code offloads this to git and has a trivial parser of the output. - Resolves forgejo/forgejo#7701 - Added exhaustive integration testing. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7727 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Otto <otto@codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			56 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2017 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package git
 | |
| 
 | |
| import (
 | |
| 	"path/filepath"
 | |
| 	"testing"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| 	"github.com/stretchr/testify/require"
 | |
| )
 | |
| 
 | |
| func TestGetLatestCommitTime(t *testing.T) {
 | |
| 	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
 | |
| 	lct, err := GetLatestCommitTime(DefaultContext, bareRepo1Path)
 | |
| 	require.NoError(t, err)
 | |
| 	// Time is Sun Nov 13 16:40:14 2022 +0100
 | |
| 	// which is the time of commit
 | |
| 	// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
 | |
| 	assert.EqualValues(t, 1668354014, lct.Unix())
 | |
| }
 | |
| 
 | |
| func TestRepoIsEmpty(t *testing.T) {
 | |
| 	emptyRepo2Path := filepath.Join(testReposDir, "repo2_empty")
 | |
| 	repo, err := openRepositoryWithDefaultContext(emptyRepo2Path)
 | |
| 	require.NoError(t, err)
 | |
| 	defer repo.Close()
 | |
| 	isEmpty, err := repo.IsEmpty()
 | |
| 	require.NoError(t, err)
 | |
| 	assert.True(t, isEmpty)
 | |
| }
 | |
| 
 | |
| func TestRepoGetDivergingCommits(t *testing.T) {
 | |
| 	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
 | |
| 	do, err := GetDivergingCommits(t.Context(), bareRepo1Path, "master", "branch2", nil)
 | |
| 	require.NoError(t, err)
 | |
| 	assert.Equal(t, DivergeObject{
 | |
| 		Ahead:  1,
 | |
| 		Behind: 5,
 | |
| 	}, do)
 | |
| 
 | |
| 	do, err = GetDivergingCommits(t.Context(), bareRepo1Path, "master", "master", nil)
 | |
| 	require.NoError(t, err)
 | |
| 	assert.Equal(t, DivergeObject{
 | |
| 		Ahead:  0,
 | |
| 		Behind: 0,
 | |
| 	}, do)
 | |
| 
 | |
| 	do, err = GetDivergingCommits(t.Context(), bareRepo1Path, "master", "test", nil)
 | |
| 	require.NoError(t, err)
 | |
| 	assert.Equal(t, DivergeObject{
 | |
| 		Ahead:  0,
 | |
| 		Behind: 2,
 | |
| 	}, do)
 | |
| }
 |