mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-30 13:56:09 +01:00
f9cfd6ce5b
This PR replaces all string refName as a type `git.RefName` to make the code more maintainable. Fix #15367 Replaces #23070 It also fixed a bug that tags are not sync because `git remote --prune origin` will not remove local tags if remote removed. We in fact should use `git fetch --prune --tags origin` but not `git remote update origin` to do the sync. Some answer from ChatGPT as ref. > If the git fetch --prune --tags command is not working as expected, there could be a few reasons why. Here are a few things to check: > >Make sure that you have the latest version of Git installed on your system. You can check the version by running git --version in your terminal. If you have an outdated version, try updating Git and see if that resolves the issue. > >Check that your Git repository is properly configured to track the remote repository's tags. You can check this by running git config --get-all remote.origin.fetch and verifying that it includes +refs/tags/*:refs/tags/*. If it does not, you can add it by running git config --add remote.origin.fetch "+refs/tags/*:refs/tags/*". > >Verify that the tags you are trying to prune actually exist on the remote repository. You can do this by running git ls-remote --tags origin to list all the tags on the remote repository. > >Check if any local tags have been created that match the names of tags on the remote repository. If so, these local tags may be preventing the git fetch --prune --tags command from working properly. You can delete local tags using the git tag -d command. --------- Co-authored-by: delvh <dev.lh@web.de>
190 lines
5.1 KiB
Go
190 lines
5.1 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// BranchPrefix base dir of the branch information file store on git
|
|
const BranchPrefix = "refs/heads/"
|
|
|
|
// IsReferenceExist returns true if given reference exists in the repository.
|
|
func IsReferenceExist(ctx context.Context, repoPath, name string) bool {
|
|
_, _, err := NewCommand(ctx, "show-ref", "--verify").AddDashesAndList(name).RunStdString(&RunOpts{Dir: repoPath})
|
|
return err == nil
|
|
}
|
|
|
|
// IsBranchExist returns true if given branch exists in the repository.
|
|
func IsBranchExist(ctx context.Context, repoPath, name string) bool {
|
|
return IsReferenceExist(ctx, repoPath, BranchPrefix+name)
|
|
}
|
|
|
|
// Branch represents a Git branch.
|
|
type Branch struct {
|
|
Name string
|
|
Path string
|
|
|
|
gitRepo *Repository
|
|
}
|
|
|
|
// GetHEADBranch returns corresponding branch of HEAD.
|
|
func (repo *Repository) GetHEADBranch() (*Branch, error) {
|
|
if repo == nil {
|
|
return nil, fmt.Errorf("nil repo")
|
|
}
|
|
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
|
return nil, fmt.Errorf("invalid HEAD branch: %v", stdout)
|
|
}
|
|
|
|
return &Branch{
|
|
Name: stdout[len(BranchPrefix):],
|
|
Path: stdout,
|
|
gitRepo: repo,
|
|
}, nil
|
|
}
|
|
|
|
// SetDefaultBranch sets default branch of repository.
|
|
func (repo *Repository) SetDefaultBranch(name string) error {
|
|
_, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").AddDynamicArguments(BranchPrefix + name).RunStdString(&RunOpts{Dir: repo.Path})
|
|
return err
|
|
}
|
|
|
|
// GetDefaultBranch gets default branch of repository.
|
|
func (repo *Repository) GetDefaultBranch() (string, error) {
|
|
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
stdout = strings.TrimSpace(stdout)
|
|
if !strings.HasPrefix(stdout, BranchPrefix) {
|
|
return "", errors.New("the HEAD is not a branch: " + stdout)
|
|
}
|
|
return strings.TrimPrefix(stdout, BranchPrefix), nil
|
|
}
|
|
|
|
// GetBranch returns a branch by it's name
|
|
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
|
|
if !repo.IsBranchExist(branch) {
|
|
return nil, ErrBranchNotExist{branch}
|
|
}
|
|
return &Branch{
|
|
Path: repo.Path,
|
|
Name: branch,
|
|
gitRepo: repo,
|
|
}, nil
|
|
}
|
|
|
|
// GetBranchesByPath returns a branch by it's path
|
|
// if limit = 0 it will not limit
|
|
func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) {
|
|
gitRepo, err := OpenRepository(ctx, path)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
return gitRepo.GetBranches(skip, limit)
|
|
}
|
|
|
|
// GetBranchCommitID returns a branch commit ID by its name
|
|
func GetBranchCommitID(ctx context.Context, path, branch string) (string, error) {
|
|
gitRepo, err := OpenRepository(ctx, path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer gitRepo.Close()
|
|
|
|
return gitRepo.GetBranchCommitID(branch)
|
|
}
|
|
|
|
// GetBranches returns a slice of *git.Branch
|
|
func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) {
|
|
brs, countAll, err := repo.GetBranchNames(skip, limit)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
branches := make([]*Branch, len(brs))
|
|
for i := range brs {
|
|
branches[i] = &Branch{
|
|
Path: repo.Path,
|
|
Name: brs[i],
|
|
gitRepo: repo,
|
|
}
|
|
}
|
|
|
|
return branches, countAll, nil
|
|
}
|
|
|
|
// DeleteBranchOptions Option(s) for delete branch
|
|
type DeleteBranchOptions struct {
|
|
Force bool
|
|
}
|
|
|
|
// DeleteBranch delete a branch by name on repository.
|
|
func (repo *Repository) DeleteBranch(name string, opts DeleteBranchOptions) error {
|
|
cmd := NewCommand(repo.Ctx, "branch")
|
|
|
|
if opts.Force {
|
|
cmd.AddArguments("-D")
|
|
} else {
|
|
cmd.AddArguments("-d")
|
|
}
|
|
|
|
cmd.AddDashesAndList(name)
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
|
|
|
return err
|
|
}
|
|
|
|
// CreateBranch create a new branch
|
|
func (repo *Repository) CreateBranch(branch, oldbranchOrCommit string) error {
|
|
cmd := NewCommand(repo.Ctx, "branch")
|
|
cmd.AddDashesAndList(branch, oldbranchOrCommit)
|
|
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
|
|
|
return err
|
|
}
|
|
|
|
// AddRemote adds a new remote to repository.
|
|
func (repo *Repository) AddRemote(name, url string, fetch bool) error {
|
|
cmd := NewCommand(repo.Ctx, "remote", "add")
|
|
if fetch {
|
|
cmd.AddArguments("-f")
|
|
}
|
|
cmd.AddDynamicArguments(name, url)
|
|
|
|
_, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
|
return err
|
|
}
|
|
|
|
// RemoveRemote removes a remote from repository.
|
|
func (repo *Repository) RemoveRemote(name string) error {
|
|
_, _, err := NewCommand(repo.Ctx, "remote", "rm").AddDynamicArguments(name).RunStdString(&RunOpts{Dir: repo.Path})
|
|
return err
|
|
}
|
|
|
|
// GetCommit returns the head commit of a branch
|
|
func (branch *Branch) GetCommit() (*Commit, error) {
|
|
return branch.gitRepo.GetBranchCommit(branch.Name)
|
|
}
|
|
|
|
// RenameBranch rename a branch
|
|
func (repo *Repository) RenameBranch(from, to string) error {
|
|
_, _, err := NewCommand(repo.Ctx, "branch", "-m").AddDynamicArguments(from, to).RunStdString(&RunOpts{Dir: repo.Path})
|
|
return err
|
|
}
|