2017-02-21 10:02:10 -05:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-21 10:02:10 -05:00
|
|
|
|
2022-06-12 10:51:54 -05:00
|
|
|
package git
|
2017-02-21 10:02:10 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-15 04:51:28 -05:00
|
|
|
"context"
|
2017-02-21 10:02:10 -05:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-09-14 03:16:22 -05:00
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-09-14 03:16:22 -05:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 09:46:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-02-21 10:02:10 -05:00
|
|
|
)
|
|
|
|
|
2017-10-25 19:49:16 -05:00
|
|
|
// DeletedBranch struct
|
|
|
|
type DeletedBranch struct {
|
2019-08-15 09:46:21 -05:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
Name string `xorm:"UNIQUE(s) NOT NULL"`
|
|
|
|
Commit string `xorm:"UNIQUE(s) NOT NULL"`
|
|
|
|
DeletedByID int64 `xorm:"INDEX"`
|
2021-11-24 04:49:20 -05:00
|
|
|
DeletedBy *user_model.User `xorm:"-"`
|
2019-08-15 09:46:21 -05:00
|
|
|
DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
|
2023-01-16 03:00:22 -05:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(DeletedBranch))
|
|
|
|
db.RegisterModel(new(RenamedBranch))
|
|
|
|
}
|
|
|
|
|
2017-10-25 19:49:16 -05:00
|
|
|
// AddDeletedBranch adds a deleted branch to the database
|
2023-01-08 22:50:54 -05:00
|
|
|
func AddDeletedBranch(ctx context.Context, repoID int64, branchName, commit string, deletedByID int64) error {
|
2017-10-25 19:49:16 -05:00
|
|
|
deletedBranch := &DeletedBranch{
|
2021-12-09 20:27:50 -05:00
|
|
|
RepoID: repoID,
|
2017-10-25 19:49:16 -05:00
|
|
|
Name: branchName,
|
|
|
|
Commit: commit,
|
|
|
|
DeletedByID: deletedByID,
|
|
|
|
}
|
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
_, err := db.GetEngine(ctx).Insert(deletedBranch)
|
2021-09-19 06:49:59 -05:00
|
|
|
return err
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeletedBranches returns all the deleted branches
|
2023-01-08 22:50:54 -05:00
|
|
|
func GetDeletedBranches(ctx context.Context, repoID int64) ([]*DeletedBranch, error) {
|
2017-10-25 19:49:16 -05:00
|
|
|
deletedBranches := make([]*DeletedBranch, 0)
|
2023-01-08 22:50:54 -05:00
|
|
|
return deletedBranches, db.GetEngine(ctx).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches)
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeletedBranchByID get a deleted branch by its ID
|
2023-01-08 22:50:54 -05:00
|
|
|
func GetDeletedBranchByID(ctx context.Context, repoID, id int64) (*DeletedBranch, error) {
|
2020-06-17 12:50:11 -05:00
|
|
|
deletedBranch := &DeletedBranch{}
|
2023-01-08 22:50:54 -05:00
|
|
|
has, err := db.GetEngine(ctx).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch)
|
2017-10-25 19:49:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return deletedBranch, nil
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
// RemoveDeletedBranchByID removes a deleted branch from the database
|
2023-01-08 22:50:54 -05:00
|
|
|
func RemoveDeletedBranchByID(ctx context.Context, repoID, id int64) (err error) {
|
2017-10-25 19:49:16 -05:00
|
|
|
deletedBranch := &DeletedBranch{
|
2021-12-09 20:27:50 -05:00
|
|
|
RepoID: repoID,
|
2017-10-25 19:49:16 -05:00
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
if affected, err := db.GetEngine(ctx).Delete(deletedBranch); err != nil {
|
2017-10-25 19:49:16 -05:00
|
|
|
return err
|
|
|
|
} else if affected != 1 {
|
|
|
|
return fmt.Errorf("remove deleted branch ID(%v) failed", id)
|
|
|
|
}
|
|
|
|
|
2021-09-19 06:49:59 -05:00
|
|
|
return nil
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadUser loads the user that deleted the branch
|
2021-11-24 04:49:20 -05:00
|
|
|
// When there's no user found it returns a user_model.NewGhostUser
|
2022-12-02 21:48:26 -05:00
|
|
|
func (deletedBranch *DeletedBranch) LoadUser(ctx context.Context) {
|
|
|
|
user, err := user_model.GetUserByID(ctx, deletedBranch.DeletedByID)
|
2017-10-25 19:49:16 -05:00
|
|
|
if err != nil {
|
2021-11-24 04:49:20 -05:00
|
|
|
user = user_model.NewGhostUser()
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
deletedBranch.DeletedBy = user
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
// RemoveDeletedBranchByName removes all deleted branches
|
2023-01-08 22:50:54 -05:00
|
|
|
func RemoveDeletedBranchByName(ctx context.Context, repoID int64, branch string) error {
|
|
|
|
_, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
|
2019-12-27 14:17:37 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-25 19:49:16 -05:00
|
|
|
// RemoveOldDeletedBranches removes old deleted branches
|
2020-05-16 18:31:38 -05:00
|
|
|
func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
|
2019-12-15 04:51:28 -05:00
|
|
|
// Nothing to do for shutdown or terminate
|
2017-10-25 19:49:16 -05:00
|
|
|
log.Trace("Doing: DeletedBranchesCleanup")
|
|
|
|
|
2020-05-16 18:31:38 -05:00
|
|
|
deleteBefore := time.Now().Add(-olderThan)
|
2023-01-08 22:50:54 -05:00
|
|
|
_, err := db.GetEngine(ctx).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
|
2017-10-25 19:49:16 -05:00
|
|
|
if err != nil {
|
2019-04-02 02:48:31 -05:00
|
|
|
log.Error("DeletedBranchesCleanup: %v", err)
|
2017-10-25 19:49:16 -05:00
|
|
|
}
|
|
|
|
}
|
2021-10-08 12:03:04 -05:00
|
|
|
|
|
|
|
// RenamedBranch provide renamed branch log
|
|
|
|
// will check it when a branch can't be found
|
|
|
|
type RenamedBranch struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindRenamedBranch check if a branch was renamed
|
2023-01-08 22:50:54 -05:00
|
|
|
func FindRenamedBranch(ctx context.Context, repoID int64, from string) (branch *RenamedBranch, exist bool, err error) {
|
2021-10-08 12:03:04 -05:00
|
|
|
branch = &RenamedBranch{
|
|
|
|
RepoID: repoID,
|
|
|
|
From: from,
|
|
|
|
}
|
2023-01-08 22:50:54 -05:00
|
|
|
exist, err = db.GetEngine(ctx).Get(branch)
|
2021-10-08 12:03:04 -05:00
|
|
|
|
2022-06-20 05:02:49 -05:00
|
|
|
return branch, exist, err
|
2021-10-08 12:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenameBranch rename a branch
|
2023-01-08 22:50:54 -05:00
|
|
|
func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
|
|
|
|
ctx, committer, err := db.TxContext(ctx)
|
2021-11-21 10:41:00 -05:00
|
|
|
if err != nil {
|
2021-10-08 12:03:04 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-11-21 10:41:00 -05:00
|
|
|
defer committer.Close()
|
2021-10-08 12:03:04 -05:00
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
sess := db.GetEngine(ctx)
|
2021-10-08 12:03:04 -05:00
|
|
|
// 1. update default branch if needed
|
|
|
|
isDefault := repo.DefaultBranch == from
|
|
|
|
if isDefault {
|
|
|
|
repo.DefaultBranch = to
|
|
|
|
_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Update protected branch if needed
|
2023-01-16 03:00:22 -05:00
|
|
|
protectedBranch, err := GetProtectedBranchRuleByName(ctx, repo.ID, from)
|
2021-10-08 12:03:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if protectedBranch != nil {
|
2023-01-16 03:00:22 -05:00
|
|
|
protectedBranch.RuleName = to
|
2021-10-08 12:03:04 -05:00
|
|
|
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-16 03:00:22 -05:00
|
|
|
} else {
|
|
|
|
protected, err := IsBranchProtected(ctx, repo.ID, from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if protected {
|
|
|
|
return ErrBranchIsProtected
|
|
|
|
}
|
2021-10-08 12:03:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Update all not merged pull request base branch name
|
2022-06-12 10:51:54 -05:00
|
|
|
_, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?",
|
2021-10-08 12:03:04 -05:00
|
|
|
repo.ID, from, false).
|
2023-07-04 22:41:32 -05:00
|
|
|
Update(map[string]any{"base_branch": to})
|
2021-10-08 12:03:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. do git action
|
|
|
|
if err = gitAction(isDefault); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. insert renamed branch record
|
|
|
|
renamedBranch := &RenamedBranch{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
2021-11-21 10:41:00 -05:00
|
|
|
err = db.Insert(ctx, renamedBranch)
|
2021-10-08 12:03:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 10:41:00 -05:00
|
|
|
return committer.Commit()
|
2021-10-08 12:03:04 -05:00
|
|
|
}
|