mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	* 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
		
			
				
	
	
		
			151 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			151 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2016 The Gogs Authors. All rights reserved.
 | |
| // Copyright 2019 The Gitea Authors. All rights reserved.
 | |
| // Use of this source code is governed by a MIT-style
 | |
| // license that can be found in the LICENSE file.
 | |
| 
 | |
| package models
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 
 | |
| 	"code.gitea.io/gitea/modules/git"
 | |
| 	"code.gitea.io/gitea/modules/log"
 | |
| )
 | |
| 
 | |
| // CanCreateBranch returns true if repository meets the requirements for creating new branches.
 | |
| func (repo *Repository) CanCreateBranch() bool {
 | |
| 	return !repo.IsMirror
 | |
| }
 | |
| 
 | |
| // GetBranch returns a branch by its name
 | |
| func (repo *Repository) GetBranch(branch string) (*git.Branch, error) {
 | |
| 	gitRepo, err := git.OpenRepository(repo.RepoPath())
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return gitRepo.GetBranch(branch)
 | |
| }
 | |
| 
 | |
| // GetBranches returns all the branches of a repository
 | |
| func (repo *Repository) GetBranches() ([]*git.Branch, error) {
 | |
| 	return git.GetBranchesByPath(repo.RepoPath())
 | |
| }
 | |
| 
 | |
| // CheckBranchName validates branch name with existing repository branches
 | |
| func (repo *Repository) CheckBranchName(name string) error {
 | |
| 	gitRepo, err := git.OpenRepository(repo.RepoPath())
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	branches, err := repo.GetBranches()
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	for _, branch := range branches {
 | |
| 		if branch.Name == name {
 | |
| 			return ErrBranchAlreadyExists{branch.Name}
 | |
| 		} else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
 | |
| 			(len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
 | |
| 			return ErrBranchNameConflict{branch.Name}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if _, err := gitRepo.GetTag(name); err == nil {
 | |
| 		return ErrTagAlreadyExists{name}
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // CreateNewBranch creates a new repository branch
 | |
| func (repo *Repository) CreateNewBranch(doer *User, oldBranchName, branchName string) (err error) {
 | |
| 	// Check if branch name can be used
 | |
| 	if err := repo.CheckBranchName(branchName); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
 | |
| 		return fmt.Errorf("OldBranch: %s does not exist. Cannot create new branch from this", oldBranchName)
 | |
| 	}
 | |
| 
 | |
| 	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)
 | |
| 	}
 | |
| 
 | |
| 	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)
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // CreateNewBranchFromCommit creates a new repository branch
 | |
| func (repo *Repository) CreateNewBranchFromCommit(doer *User, commit, branchName string) (err error) {
 | |
| 	// 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)
 | |
| 
 | |
| 	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)
 | |
| 	}
 | |
| 
 | |
| 	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, 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)
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 |