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