| 
 | 1 | +// Copyright 2025 The Gitea Authors. All rights reserved.  | 
 | 2 | +// SPDX-License-Identifier: MIT  | 
 | 3 | + | 
 | 4 | +package gitrepo  | 
 | 5 | + | 
 | 6 | +import (  | 
 | 7 | +	"context"  | 
 | 8 | +	"io"  | 
 | 9 | +	"time"  | 
 | 10 | + | 
 | 11 | +	"code.gitea.io/gitea/modules/git"  | 
 | 12 | +	giturl "code.gitea.io/gitea/modules/git/url"  | 
 | 13 | +	"code.gitea.io/gitea/modules/globallock"  | 
 | 14 | +)  | 
 | 15 | + | 
 | 16 | +func GetGitConfig(ctx context.Context, repo Repository, key string) (string, error) {  | 
 | 17 | +	result, _, err := git.NewCommand("config", "--get").  | 
 | 18 | +		AddDynamicArguments(key).  | 
 | 19 | +		RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})  | 
 | 20 | +	if err != nil {  | 
 | 21 | +		return "", err  | 
 | 22 | +	}  | 
 | 23 | +	if len(result) > 0 {  | 
 | 24 | +		result = result[:len(result)-1] // remove trailing newline  | 
 | 25 | +	}  | 
 | 26 | +	return result, nil  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +func getRepoConfigLockKey(repoStoragePath string) string {  | 
 | 30 | +	return "repo-config:" + repoStoragePath  | 
 | 31 | +}  | 
 | 32 | + | 
 | 33 | +// AddGitConfig add a git configuration key to a specific value for the given repository.  | 
 | 34 | +func AddGitConfig(ctx context.Context, repo Repository, key, value string) error {  | 
 | 35 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 36 | +	if err != nil {  | 
 | 37 | +		return err  | 
 | 38 | +	}  | 
 | 39 | +	defer releaser()  | 
 | 40 | + | 
 | 41 | +	_, _, err = git.NewCommand("config", "--add").  | 
 | 42 | +		AddDynamicArguments(key, value).  | 
 | 43 | +		RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})  | 
 | 44 | +	return err  | 
 | 45 | +}  | 
 | 46 | + | 
 | 47 | +// UpdateGitConfig updates a git configuration key to a specific value for the given repository.  | 
 | 48 | +// If the key does not exist, it will be created.  | 
 | 49 | +// If the key exists, it will be updated to the new value.  | 
 | 50 | +func UpdateGitConfig(ctx context.Context, repo Repository, key, value string) (string, error) {  | 
 | 51 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 52 | +	if err != nil {  | 
 | 53 | +		return "", err  | 
 | 54 | +	}  | 
 | 55 | +	defer releaser()  | 
 | 56 | + | 
 | 57 | +	value, _, err1 := git.NewCommand("config").  | 
 | 58 | +		AddDynamicArguments(key, value).  | 
 | 59 | +		RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})  | 
 | 60 | +	return value, err1  | 
 | 61 | +}  | 
 | 62 | + | 
 | 63 | +func AddGitRemote(ctx context.Context, repo Repository, remoteName, remoteURL string, options ...string) error {  | 
 | 64 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 65 | +	if err != nil {  | 
 | 66 | +		return err  | 
 | 67 | +	}  | 
 | 68 | +	defer releaser()  | 
 | 69 | + | 
 | 70 | +	cmd := git.NewCommand("remote", "add")  | 
 | 71 | +	if len(options) > 0 {  | 
 | 72 | +		cmd.AddDynamicArguments(options...)  | 
 | 73 | +	}  | 
 | 74 | +	_, _, err = cmd.  | 
 | 75 | +		AddDynamicArguments(remoteName, remoteURL).  | 
 | 76 | +		RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})  | 
 | 77 | +	return err  | 
 | 78 | +}  | 
 | 79 | + | 
 | 80 | +func RemoveGitRemote(ctx context.Context, repo Repository, remoteName string) error {  | 
 | 81 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 82 | +	if err != nil {  | 
 | 83 | +		return err  | 
 | 84 | +	}  | 
 | 85 | +	defer releaser()  | 
 | 86 | + | 
 | 87 | +	cmd := git.NewCommand("remote", "rm").AddDynamicArguments(remoteName)  | 
 | 88 | +	_, _, err = cmd.RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})  | 
 | 89 | +	return err  | 
 | 90 | +}  | 
 | 91 | + | 
 | 92 | +// GetRemoteURL returns the url of a specific remote of the repository.  | 
 | 93 | +func GetRemoteURL(ctx context.Context, repo Repository, remoteName string) (*giturl.GitURL, error) {  | 
 | 94 | +	addr, err := git.GetRemoteAddress(ctx, repoPath(repo), remoteName)  | 
 | 95 | +	if err != nil {  | 
 | 96 | +		return nil, err  | 
 | 97 | +	}  | 
 | 98 | +	return giturl.ParseGitURL(addr)  | 
 | 99 | +}  | 
 | 100 | + | 
 | 101 | +// PruneRemote prunes the remote branches that no longer exist in the remote repository.  | 
 | 102 | +func PruneRemote(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error {  | 
 | 103 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 104 | +	if err != nil {  | 
 | 105 | +		return err  | 
 | 106 | +	}  | 
 | 107 | +	defer releaser()  | 
 | 108 | + | 
 | 109 | +	return git.NewCommand("remote", "prune").AddDynamicArguments(remoteName).  | 
 | 110 | +		Run(ctx, &git.RunOpts{  | 
 | 111 | +			Timeout: timeout,  | 
 | 112 | +			Dir:     repoPath(repo),  | 
 | 113 | +			Stdout:  stdout,  | 
 | 114 | +			Stderr:  stderr,  | 
 | 115 | +		})  | 
 | 116 | +}  | 
 | 117 | + | 
 | 118 | +func UpdateRemotePrune(ctx context.Context, repo Repository, remoteName string, timeout time.Duration, stdout, stderr io.Writer) error {  | 
 | 119 | +	releaser, err := globallock.Lock(ctx, getRepoConfigLockKey(repo.RelativePath()))  | 
 | 120 | +	if err != nil {  | 
 | 121 | +		return err  | 
 | 122 | +	}  | 
 | 123 | +	defer releaser()  | 
 | 124 | + | 
 | 125 | +	return git.NewCommand("remote", "update", "--prune").AddDynamicArguments(remoteName).  | 
 | 126 | +		Run(ctx, &git.RunOpts{  | 
 | 127 | +			Timeout: timeout,  | 
 | 128 | +			Dir:     repoPath(repo),  | 
 | 129 | +			Stdout:  stdout,  | 
 | 130 | +			Stderr:  stderr,  | 
 | 131 | +		})  | 
 | 132 | +}  | 
0 commit comments