parent
d752b6126d
commit
cf8779216a
|
@ -0,0 +1,6 @@
|
|||
package structs
|
||||
|
||||
type RestoreBranchOption struct {
|
||||
BranchID int64 `json:"branch_id"`
|
||||
BranchName string `json:"name"`
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package structs
|
||||
|
||||
import (
|
||||
gitea_api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
type Branch struct {
|
||||
*gitea_api.Branch
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
DeletedUnix int `json:"deleted_unix"`
|
||||
ID int64 `json:"id"`
|
||||
DeletedBy *BranchDeleteUser `json:"deleted_by"`
|
||||
}
|
||||
|
||||
type BranchDeleteUser struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
|
@ -130,6 +130,7 @@ func Routers(ctx gocontext.Context) *web.Route {
|
|||
m.Group("/branches", func() {
|
||||
m.Get("", context.ReferencesGitRepo(), repo.ListBranches)
|
||||
m.Get("/branches_slice", context.ReferencesGitRepo(), repo.ListBranchesSlice)
|
||||
m.Post("/restore", context.ReferencesGitRepo(), bind(hat_api.RestoreBranchOption{}), repo.RestoreBranch)
|
||||
}, reqRepoReader(unit_model.TypeCode))
|
||||
m.Group("/commits", func() {
|
||||
m.Get("/{sha}/diff", repo.GetCommitDiff)
|
||||
|
|
|
@ -4,17 +4,20 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
gitea_git "code.gitea.io/gitea/modules/git"
|
||||
repo_module "code.gitea.io/gitea/modules/repository"
|
||||
gitea_api "code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
|
||||
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
|
||||
hat_branch_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/convert"
|
||||
|
||||
"code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
|
||||
|
||||
|
@ -23,7 +26,7 @@ import (
|
|||
|
||||
func ListBranches(ctx *context.APIContext) {
|
||||
var totalNumOfBranches int64
|
||||
var apiBranches []*gitea_api.Branch
|
||||
var apiBranches []*hat_api.Branch
|
||||
|
||||
searchName := ctx.FormString("name")
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
|
@ -36,7 +39,7 @@ func ListBranches(ctx *context.APIContext) {
|
|||
branchOpts := git_model.FindBranchOptions{
|
||||
ListOptions: listOptions,
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
IsDeletedBranch: util.OptionalBoolFalse,
|
||||
IsDeletedBranch: util.OptionalBoolNone,
|
||||
Keyword: searchName,
|
||||
}
|
||||
|
||||
|
@ -66,9 +69,14 @@ func ListBranches(ctx *context.APIContext) {
|
|||
return
|
||||
}
|
||||
|
||||
apiBranches = make([]*gitea_api.Branch, 0, len(branches))
|
||||
apiBranches = make([]*hat_api.Branch, 0, len(branches))
|
||||
for i := range branches {
|
||||
c, err := ctx.Repo.GitRepo.GetBranchCommit(branches[i].Name)
|
||||
err := branches[i].LoadDeletedBy(ctx)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "LoadDeletedBy", err)
|
||||
return
|
||||
}
|
||||
c, err := ctx.Repo.GitRepo.GetCommit(branches[i].CommitID)
|
||||
if err != nil {
|
||||
// Skip if this branch doesn't exist anymore.
|
||||
if gitea_git.IsErrNotExist(err) {
|
||||
|
@ -80,7 +88,7 @@ func ListBranches(ctx *context.APIContext) {
|
|||
}
|
||||
|
||||
branchProtection := rules.GetFirstMatched(branches[i].Name)
|
||||
apiBranch, err := convert.ToBranch(ctx, ctx.Repo.Repository, branches[i].Name, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
||||
apiBranch, err := hat_branch_convert.ToBranch(ctx, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
||||
return
|
||||
|
@ -189,3 +197,48 @@ func BranchesSliceByProtection(ctx *context.APIContext, branchList []*git.Branch
|
|||
}
|
||||
return branchSlice
|
||||
}
|
||||
|
||||
func RestoreBranch(ctx *context.APIContext) {
|
||||
|
||||
form := web.GetForm(ctx).(*hat_api.RestoreBranchOption)
|
||||
branchID := form.BranchID
|
||||
branchName := form.BranchName
|
||||
|
||||
deletedBranch, err := git_model.GetDeletedBranchByID(ctx, ctx.Repo.Repository.ID, branchID)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetDeletedBranchByID: %v", err)
|
||||
return
|
||||
} else if deletedBranch == nil {
|
||||
ctx.Error(http.StatusInternalServerError, "RestoreBranch: Can't restore branch '%s', as it does not exist", branchName)
|
||||
return
|
||||
}
|
||||
|
||||
if err := gitea_git.Push(ctx, ctx.Repo.Repository.RepoPath(), gitea_git.PushOptions{
|
||||
Remote: ctx.Repo.Repository.RepoPath(),
|
||||
Branch: fmt.Sprintf("%s:%s%s", deletedBranch.CommitID, gitea_git.BranchPrefix, deletedBranch.Name),
|
||||
Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
|
||||
}); err != nil {
|
||||
if strings.Contains(err.Error(), "already exists") {
|
||||
ctx.Error(http.StatusInternalServerError, "RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
|
||||
return
|
||||
}
|
||||
ctx.Error(http.StatusInternalServerError, "RestoreBranch: CreateBranch: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo_service.PushUpdate(
|
||||
&repo_module.PushUpdateOptions{
|
||||
RefFullName: gitea_git.RefNameFromBranch(deletedBranch.Name),
|
||||
OldCommitID: gitea_git.EmptySHA,
|
||||
NewCommitID: deletedBranch.CommitID,
|
||||
PusherID: ctx.Doer.ID,
|
||||
PusherName: ctx.Doer.Name,
|
||||
RepoUserName: ctx.Repo.Owner.Name,
|
||||
RepoName: ctx.Repo.Repository.Name,
|
||||
}); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "RestoreBranch: Update: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusCreated)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
package convert
|
||||
|
||||
import (
|
||||
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
|
||||
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
gitea_convert "code.gitea.io/gitea/services/convert"
|
||||
)
|
||||
|
||||
func ToBranch(ctx *context.APIContext, branch *git_model.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*hat_api.Branch, error) {
|
||||
gitea_api_branch, err := gitea_convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, bp, user, isRepoAdmin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hat_api_branch := &hat_api.Branch{}
|
||||
hat_api_branch.Branch = gitea_api_branch
|
||||
hat_api_branch.IsDeleted = branch.IsDeleted
|
||||
hat_api_branch.DeletedUnix = int(branch.DeletedUnix)
|
||||
hat_api_branch.ID = branch.ID
|
||||
hat_api_branch.DeletedBy = &hat_api.BranchDeleteUser{
|
||||
Name: branch.DeletedBy.Name,
|
||||
Email: branch.DeletedBy.Email,
|
||||
}
|
||||
return hat_api_branch, nil
|
||||
}
|
Loading…
Reference in New Issue