jojo/modules/git/fetch.go
Gusted 5b77ddb050 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

51 lines
1.9 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package git
import (
"errors"
"fmt"
"strings"
)
var ErrRemoteRefNotFound = errors.New("unable to find remote ref")
// Fetch executes git-fetch(1) for the repository, it will fetch the refspec
// from the remote into the repository.
//
// Valid remote URLs are denoted in section GIT URLS of git-fetch(1).
//
// The return value, on success, is the object ID of the remote reference. If
// no local reference is given in `refspec` then do not assume it's available in
// the `FETCH_HEAD` reference, it might have been overwritten by the time you
// read or use it.
func (repo *Repository) Fetch(remoteURL, refspec string) (string, error) {
objectFormat, err := repo.GetObjectFormat()
if err != nil {
return "", err
}
stdout, stderr, err := NewCommand(repo.Ctx, "fetch", "--porcelain", "--end-of-options").AddDynamicArguments(remoteURL, refspec).RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
if strings.HasPrefix(stderr, "fatal: couldn't find remote ref ") {
return "", ErrRemoteRefNotFound
}
return "", err
}
localReference, _, ok := strings.Cut(refspec, ":")
if !ok {
localReference = "FETCH_HEAD"
}
// The porcelain format, per section OUTPUT of git-fetch(1), is expected to be:
// <flag><space><old-object-id><space><new-object-id><space><local-reference>\n
// flag is one character.
if expectedLen := 1 + 1 + objectFormat.FullLength() + 1 + objectFormat.FullLength() + 1 + len(localReference) + 1; len(stdout) != expectedLen {
return "", fmt.Errorf("output of git-fetch(1) is unexpected, we expected it to be %d bytes but it is %d bytes. stdout: %s", expectedLen, len(stdout), stdout)
}
// Extract the new objectID.
newObjectID := stdout[1+1+objectFormat.FullLength()+1 : 1+1+objectFormat.FullLength()+1+objectFormat.FullLength()]
return newObjectID, nil
}