mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 11:52:10 +00:00
Remove local clones & make hooks run on merge/edit/upload (#6672)
* Add options to git.Clone to make it more capable * Begin the process of removing the local copy and tidy up * Remove Wiki LocalCopy Checkouts * Remove the last LocalRepo helpers * Remove WithTemporaryFile * Enable push-hooks for these routes * Ensure tests cope with hooks Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove Repository.LocalCopyPath() * Move temporary repo to use the standard temporary path * Fix the tests Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove LocalWikiPath * Fix missing remove Signed-off-by: Andrew Thornton <art27@cantab.net> * Use AppURL for Oauth user link (#6894) * Use AppURL for Oauth user link Fix #6843 * Update oauth.go * Update oauth.go * internal/ssh: ignore env command totally (#6825) * ssh: ignore env command totally * Remove commented code Needed fix described in issue #6889 * Escape the commit message on issues update and title in telegram hook (#6901) * update sdk to latest (#6903) * improve description of branch protection (fix #6886) (#6906) The branch protection description text were not quite accurate. * Fix logging documentation (#6904) * ENABLE_MACARON_REDIRECT should be REDIRECT_MACARON_LOG * Allow DISABLE_ROUTER_LOG to be set in the [log] section * [skip ci] Updated translations via Crowdin * Move sdk structs to modules/structs (#6905) * move sdk structs to moduels/structs * fix tests * fix fmt * fix swagger * fix vendor
This commit is contained in:
parent
34eee25bd4
commit
ce8de35334
33 changed files with 1652 additions and 1417 deletions
|
@ -7,86 +7,11 @@ package models
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
"github.com/Unknwon/com"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
)
|
||||
|
||||
// discardLocalRepoBranchChanges discards local commits/changes of
|
||||
// given branch to make sure it is even to remote branch.
|
||||
func discardLocalRepoBranchChanges(localPath, branch string) error {
|
||||
if !com.IsExist(localPath) {
|
||||
return nil
|
||||
}
|
||||
// No need to check if nothing in the repository.
|
||||
if !git.IsBranchExist(localPath, branch) {
|
||||
return nil
|
||||
}
|
||||
|
||||
refName := "origin/" + branch
|
||||
if err := git.ResetHEAD(localPath, true, refName); err != nil {
|
||||
return fmt.Errorf("git reset --hard %s: %v", refName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DiscardLocalRepoBranchChanges discards the local repository branch changes
|
||||
func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
|
||||
return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
|
||||
}
|
||||
|
||||
// checkoutNewBranch checks out to a new branch from the a branch name.
|
||||
func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
|
||||
if err := git.Checkout(localPath, git.CheckoutOptions{
|
||||
Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
|
||||
Branch: newBranch,
|
||||
OldBranch: oldBranch,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckoutNewBranch checks out a new branch
|
||||
func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
|
||||
return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
|
||||
}
|
||||
|
||||
// deleteLocalBranch deletes a branch from a local repo cache
|
||||
// First checks out default branch to avoid trying to delete the currently checked out branch
|
||||
func deleteLocalBranch(localPath, defaultBranch, deleteBranch string) error {
|
||||
if !com.IsExist(localPath) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !git.IsBranchExist(localPath, deleteBranch) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Must NOT have branch currently checked out
|
||||
// Checkout default branch first
|
||||
if err := git.Checkout(localPath, git.CheckoutOptions{
|
||||
Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
|
||||
Branch: defaultBranch,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("git checkout %s: %v", defaultBranch, err)
|
||||
}
|
||||
|
||||
cmd := git.NewCommand("branch")
|
||||
cmd.AddArguments("-D")
|
||||
cmd.AddArguments(deleteBranch)
|
||||
_, err := cmd.RunInDir(localPath)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteLocalBranch deletes a branch from the local repo
|
||||
func (repo *Repository) DeleteLocalBranch(branchName string) error {
|
||||
return deleteLocalBranch(repo.LocalCopyPath(), repo.DefaultBranch, branchName)
|
||||
}
|
||||
|
||||
// CanCreateBranch returns true if repository meets the requirements for creating new branches.
|
||||
func (repo *Repository) CanCreateBranch() bool {
|
||||
return !repo.IsMirror
|
||||
|
@ -137,29 +62,44 @@ func (repo *Repository) CheckBranchName(name string) error {
|
|||
|
||||
// CreateNewBranch creates a new repository branch
|
||||
func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
|
||||
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
||||
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
||||
|
||||
// Check if branch name can be used
|
||||
if err := repo.CheckBranchName(branchName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
localPath := repo.LocalCopyPath()
|
||||
|
||||
if err = discardLocalRepoBranchChanges(localPath, oldBranchName); err != nil {
|
||||
return fmt.Errorf("discardLocalRepoChanges: %v", err)
|
||||
} else if err = repo.UpdateLocalCopyBranch(oldBranchName); err != nil {
|
||||
return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
|
||||
if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
|
||||
return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
|
||||
}
|
||||
|
||||
if err = repo.CheckoutNewBranch(oldBranchName, branchName); err != nil {
|
||||
return fmt.Errorf("CreateNewBranch: %v", err)
|
||||
basePath, err := CreateTemporaryPath("branch-maker")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer RemoveTemporaryPath(basePath)
|
||||
|
||||
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
Shared: true,
|
||||
}); err != nil {
|
||||
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
||||
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
||||
}
|
||||
|
||||
if err = git.Push(localPath, git.PushOptions{
|
||||
gitRepo, err := git.OpenRepository(basePath)
|
||||
if err != nil {
|
||||
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
||||
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
|
||||
}
|
||||
|
||||
if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
|
||||
log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
|
||||
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
|
||||
}
|
||||
|
||||
if err = git.Push(basePath, git.PushOptions{
|
||||
Remote: "origin",
|
||||
Branch: branchName,
|
||||
Env: PushingEnvironment(doer, repo),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("Push: %v", err)
|
||||
}
|
||||
|
@ -167,62 +107,41 @@ func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName st
|
|||
return nil
|
||||
}
|
||||
|
||||
// updateLocalCopyToCommit pulls latest changes of given commit from repoPath to localPath.
|
||||
// It creates a new clone if local copy does not exist.
|
||||
// This function checks out target commit by default, it is safe to assume subsequent
|
||||
// operations are operating against target commit when caller has confidence for no race condition.
|
||||
func updateLocalCopyToCommit(repoPath, localPath, commit string) error {
|
||||
if !com.IsExist(localPath) {
|
||||
if err := git.Clone(repoPath, localPath, git.CloneRepoOptions{
|
||||
Timeout: time.Duration(setting.Git.Timeout.Clone) * time.Second,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("git clone: %v", err)
|
||||
}
|
||||
} else {
|
||||
_, err := git.NewCommand("fetch", "origin").RunInDir(localPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("git fetch origin: %v", err)
|
||||
}
|
||||
if err := git.ResetHEAD(localPath, true, "HEAD"); err != nil {
|
||||
return fmt.Errorf("git reset --hard HEAD: %v", err)
|
||||
}
|
||||
}
|
||||
if err := git.Checkout(localPath, git.CheckoutOptions{
|
||||
Branch: commit,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("git checkout %s: %v", commit, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateLocalCopyToCommit makes sure local copy of repository is at given commit.
|
||||
func (repo *Repository) updateLocalCopyToCommit(commit string) error {
|
||||
return updateLocalCopyToCommit(repo.RepoPath(), repo.LocalCopyPath(), commit)
|
||||
}
|
||||
|
||||
// CreateNewBranchFromCommit creates a new repository branch
|
||||
func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
|
||||
repoWorkingPool.CheckIn(com.ToStr(repo.ID))
|
||||
defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
|
||||
|
||||
// Check if branch name can be used
|
||||
if err := repo.CheckBranchName(branchName); err != nil {
|
||||
return err
|
||||
}
|
||||
basePath, err := CreateTemporaryPath("branch-maker")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer RemoveTemporaryPath(basePath)
|
||||
|
||||
localPath := repo.LocalCopyPath()
|
||||
|
||||
if err = repo.updateLocalCopyToCommit(commit); err != nil {
|
||||
return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
|
||||
if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
|
||||
Bare: true,
|
||||
Shared: true,
|
||||
}); err != nil {
|
||||
log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
||||
return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
|
||||
}
|
||||
|
||||
if err = repo.CheckoutNewBranch(commit, branchName); err != nil {
|
||||
return fmt.Errorf("CheckoutNewBranch: %v", err)
|
||||
gitRepo, err := git.OpenRepository(basePath)
|
||||
if err != nil {
|
||||
log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
|
||||
return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
|
||||
}
|
||||
|
||||
if err = git.Push(localPath, git.PushOptions{
|
||||
if err = gitRepo.CreateBranch(branchName, commit); err != nil {
|
||||
log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
|
||||
return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
|
||||
}
|
||||
|
||||
if err = git.Push(basePath, git.PushOptions{
|
||||
Remote: "origin",
|
||||
Branch: branchName,
|
||||
Env: PushingEnvironment(doer, repo),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("Push: %v", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue