forgejo/modules/git/fetch_test.go
Gusted 5b77ddb050
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing-integration / test-mariadb (v10.6) (push) Waiting to run
testing-integration / test-mariadb (v11.8) (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
feat: Use receive.hideRefs (#10015)
In forgejo/forgejo!2834 and forgejo/forgejo!5307 it was made so it's no longer possible to modify and delete internal reference, not having this restriction lead to broken pull requests when people used something like `git push --mirror`. However it now still leads to problem with that command as the git client tries to delete such references. We can solve this by using git's `receive.hideRefs` to make this ref read-only and avoid advertising it when someone does `git push --mirror`.

Resolves forgejo/forgejo#9942

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10015
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
2025-11-10 14:36:15 +01:00

105 lines
3.4 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package git
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFetch(t *testing.T) {
t.Run("SHA1", func(t *testing.T) {
dstDir := t.TempDir()
require.NoError(t, Clone(t.Context(), filepath.Join(testReposDir, "repo1_bare"), dstDir, CloneRepoOptions{}))
repo, err := OpenRepository(t.Context(), dstDir)
require.NoError(t, err)
defer repo.Close()
t.Run("Reference", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "language_stats_repo"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "refs/heads/master")
require.NoError(t, err)
assert.Equal(t, "95d3505f2db273e40be79f84416051ae85e9ea0d", fetchedCommitID)
c, err := repo.getCommit(MustIDFromString(fetchedCommitID))
require.NoError(t, err)
assert.NotNil(t, c)
})
t.Run("CommitID", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "repo6_blame"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376")
require.NoError(t, err)
assert.Equal(t, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", fetchedCommitID)
c, err := repo.getCommit(MustIDFromString(fetchedCommitID))
require.NoError(t, err)
assert.NotNil(t, c)
})
t.Run("Invalid reference", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "repo6_blame"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "refs/heads/does-not-exist")
require.ErrorIs(t, err, ErrRemoteRefNotFound)
assert.Empty(t, fetchedCommitID)
})
})
t.Run("SHA256", func(t *testing.T) {
skipIfSHA256NotSupported(t)
dstDir := t.TempDir()
require.NoError(t, Clone(t.Context(), filepath.Join(testReposDir, "repo1_bare_sha256"), dstDir, CloneRepoOptions{}))
repo, err := OpenRepository(t.Context(), dstDir)
require.NoError(t, err)
defer repo.Close()
t.Run("Reference", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "repo6_blame_sha256"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "refs/heads/main")
require.NoError(t, err)
assert.Equal(t, "e2f5660e15159082902960af0ed74fc144921d2b0c80e069361853b3ece29ba3", fetchedCommitID)
c, err := repo.getCommit(MustIDFromString(fetchedCommitID))
require.NoError(t, err)
assert.NotNil(t, c)
})
t.Run("CommitID", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "repo6_merge_sha256"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "d2e5609f630dd8db500f5298d05d16def282412e3e66ed68cc7d0833b29129a1")
require.NoError(t, err)
assert.Equal(t, "d2e5609f630dd8db500f5298d05d16def282412e3e66ed68cc7d0833b29129a1", fetchedCommitID)
c, err := repo.getCommit(MustIDFromString(fetchedCommitID))
require.NoError(t, err)
assert.NotNil(t, c)
})
t.Run("Invalid reference", func(t *testing.T) {
otherRepoPath, err := filepath.Abs(filepath.Join(testReposDir, "repo6_blame_sha256"))
require.NoError(t, err)
fetchedCommitID, err := repo.Fetch(otherRepoPath, "refs/heads/does-not-exist")
require.ErrorIs(t, err, ErrRemoteRefNotFound)
assert.Empty(t, fetchedCommitID)
})
})
}