diff --git a/backend/main.go b/backend/main.go index 35496b58..d53991e2 100644 --- a/backend/main.go +++ b/backend/main.go @@ -262,6 +262,7 @@ func main() { authGroup.GET("/git/branches", routes.GetGitRemoteBranches) // 获取 Git 分支 authGroup.GET("/git/public-key", routes.GetGitSshPublicKey) // 获取 SSH 公钥 authGroup.GET("/git/commits", routes.GetGitCommits) // 获取 Git Commits + authGroup.POST("/git/checkout", routes.PostGitCheckout) // 获取 Git Commits } } diff --git a/backend/routes/git.go b/backend/routes/git.go index f4e64307..23685b05 100644 --- a/backend/routes/git.go +++ b/backend/routes/git.go @@ -53,3 +53,30 @@ func GetGitCommits(c *gin.Context) { Data: commits, }) } + +func PostGitCheckout(c *gin.Context) { + type ReqBody struct { + SpiderId string `json:"spider_id"` + Hash string `json:"hash"` + } + var reqBody ReqBody + if err := c.ShouldBindJSON(&reqBody); err != nil { + } + if reqBody.SpiderId == "" || !bson.IsObjectIdHex(reqBody.SpiderId) { + HandleErrorF(http.StatusInternalServerError, c, "invalid request") + return + } + spider, err := model.GetSpider(bson.ObjectIdHex(reqBody.SpiderId)) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + if err := services.GitCheckout(spider, reqBody.Hash); err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + }) +} diff --git a/backend/services/git.go b/backend/services/git.go index dba78194..18d679de 100644 --- a/backend/services/git.go +++ b/backend/services/git.go @@ -360,6 +360,7 @@ func GetGitSshPublicKey() string { return string(content) } +// 获取Git分支 func GetGitBranches(s model.Spider) (branches []GitBranch, err error) { // 打开 repo repo, err := git.PlainOpen(s.Src) @@ -387,6 +388,7 @@ func GetGitBranches(s model.Spider) (branches []GitBranch, err error) { return branches, nil } +// 获取Git Tags func GetGitTags(s model.Spider) (tags []GitTag, err error) { // 打开 repo repo, err := git.PlainOpen(s.Src) @@ -414,11 +416,13 @@ func GetGitTags(s model.Spider) (tags []GitTag, err error) { return tags, nil } -func GetHeadHash(repo *git.Repository) string { +// 获取Git Head Hash +func GetGitHeadHash(repo *git.Repository) string { head, _ := repo.Head() return head.Hash().String() } +// 获取Git远端分支 func GetGitRemoteBranches(s model.Spider) (branches []GitBranch, err error) { // 打开 repo repo, err := git.PlainOpen(s.Src) @@ -453,6 +457,7 @@ func GetGitRemoteBranches(s model.Spider) (branches []GitBranch, err error) { return branches, err } +// 获取Git Commits func GetGitCommits(s model.Spider) (commits []GitCommit, err error) { // 打开 repo repo, err := git.PlainOpen(s.Src) @@ -502,7 +507,7 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) { Author: commit.Author.Name, Email: commit.Author.Email, Ts: commit.Author.When, - IsHead: commit.Hash.String() == GetHeadHash(repo), + IsHead: commit.Hash.String() == GetGitHeadHash(repo), Branches: branchesDict[commit.Hash.String()], RemoteBranches: remoteBranchesDict[commit.Hash.String()], Tags: tagsDict[commit.Hash.String()], @@ -517,3 +522,35 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) { return commits, nil } + +func GitCheckout(s model.Spider, hash string) (err error) { + // 打开 repo + repo, err := git.PlainOpen(s.Src) + if err != nil { + log.Error(err.Error()) + debug.PrintStack() + return err + } + + // 获取worktree + wt, err := repo.Worktree() + if err != nil { + log.Error(err.Error()) + debug.PrintStack() + return err + } + + // Checkout + if err := wt.Checkout(&git.CheckoutOptions{ + Hash: plumbing.NewHash(hash), + Create: false, + Force: true, + Keep: false, + }); err != nil { + log.Error(err.Error()) + debug.PrintStack() + return err + } + + return nil +} diff --git a/frontend/src/components/Settings/GitSettings.vue b/frontend/src/components/Settings/GitSettings.vue index 9efc2807..048f2df6 100644 --- a/frontend/src/components/Settings/GitSettings.vue +++ b/frontend/src/components/Settings/GitSettings.vue @@ -173,7 +173,7 @@ {{c.author}} ({{c.email}}) -