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