diff --git a/routers/hat/hat.go b/routers/hat/hat.go index d7f4b5b..8ac5d76 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -163,14 +163,14 @@ func Routers() *web.Route { }) m.Get("/commits_slice", repo.GetAllCommitsSliceByTime) m.Get("/recent_commits", context.ReferencesGitRepo(), repo.GetRecentCommits) - // m.Get("/compare/*", reqRepoReader(unit_model.TypeCode), repo.CompareDiff) - m.Group("/compare/{shaFrom}...{shaTo}", func() { - m.Get("", repo.CompareDiff) - m.Group("/files", func() { - m.Get("", repo.CompareFiles) - m.Get("/*", repo.CompareFilesByPath) - }) - }, reqRepoReader(unit_model.TypeCode)) + m.Get("/compare/*", reqRepoReader(unit_model.TypeCode), repo.CompareDiff) + // m.Group("/compare/{shaFrom}...{shaTo}", func() { + // m.Get("", repo.CompareDiff) + // m.Group("/files", func() { + // m.Get("", repo.CompareFiles) + // m.Get("/*", repo.CompareFilesByPath) + // }) + // }, reqRepoReader(unit_model.TypeCode)) m.Group("/pulls", func() { m.Group("/{index}", func() { m.Combo("").Get(repo.GetPullRequest). diff --git a/routers/hat/repo/repo.go b/routers/hat/repo/repo.go index 0ad12cf..8dbc896 100644 --- a/routers/hat/repo/repo.go +++ b/routers/hat/repo/repo.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/gitdiff" hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" @@ -98,44 +99,155 @@ type CompareCommit struct { } func CompareDiff(ctx *context.APIContext) { - // headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx) - _, _, headGitRepo, compareInfo, _, _ := ParseCompareInfo(ctx) + isFiles := ctx.FormString("isFiles") + if isFiles == "" { + // headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx) + _, _, headGitRepo, compareInfo, _, _ := ParseCompareInfo(ctx) - if ctx.Written() { - return - } - defer headGitRepo.Close() - - // _ = PrepareComapreDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch) - - result := make([]CompareCommit, 0) - for _, commit := range compareInfo.Commits { - compareCommit := CompareCommit{ - Commit: commit, - Sha: commit.ID.String(), + if ctx.Written() { + return } - for _, p := range commit.Parents { - compareCommit.ParentShas = append(compareCommit.ParentShas, p.String()) + defer headGitRepo.Close() + + // _ = PrepareComapreDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch) + + result := make([]CompareCommit, 0) + for _, commit := range compareInfo.Commits { + compareCommit := CompareCommit{ + Commit: commit, + Sha: commit.ID.String(), + } + for _, p := range commit.Parents { + compareCommit.ParentShas = append(compareCommit.ParentShas, p.String()) + } + result = append(result, compareCommit) } - result = append(result, compareCommit) + + different := struct { + Commits []CompareCommit + Diff interface{} + CommitsCount int + Latestsha string + FilesCount int + }{ + Commits: result, + // Diff: ctx.Data["Diff"], + } + + different.CommitsCount = len(compareInfo.Commits) + different.Latestsha = compareInfo.HeadCommitID + different.FilesCount = compareInfo.NumFiles + + ctx.JSON(http.StatusOK, different) + } else { + if ctx.FormString("filepath") == "" { + headUser, headRepo, headGitRepo, compareInfo, _, headBranch := ParseCompareInfo(ctx) + if ctx.Written() { + return + } + defer headGitRepo.Close() + + headCommitID := headBranch + + repoPath := repo_model.RepoPath(headUser.Name, headRepo.Name) + + gitRepo, _ := gitea_git.OpenRepository(ctx, repoPath) + defer gitRepo.Close() + + diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ + BeforeCommitID: compareInfo.MergeBase, + AfterCommitID: headCommitID, + SkipTo: ctx.FormString("skip-to"), + MaxLines: setting.Git.MaxGitDiffLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: -1, + WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.FormString("whitespace")), + }) + + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetDiff", err) + return + } + + listOptions := utils.GetListOptions(ctx) + + totalNumberOfFiles := diff.NumFiles + totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize))) + + start, end := listOptions.GetStartEnd() + + if end > totalNumberOfFiles { + end = totalNumberOfFiles + } + + lenFiles := end - start + if lenFiles < 0 { + lenFiles = 0 + } + + apiFiles := make([]*hat_api.ChangedFile, 0, lenFiles) + for i := start; i < end; i++ { + apiFiles = append(apiFiles, hat_convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, headCommitID)) + } + + ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) + + ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) + ctx.RespHeader().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") + + ctx.JSON(http.StatusOK, struct { + Files []*hat_api.ChangedFile `json:"files"` + TotalAddition int `json:"total_addition"` + TotalDeletion int `json:"total_deletion"` + }{ + Files: apiFiles, + TotalAddition: diff.TotalAddition, + TotalDeletion: diff.TotalDeletion, + }) + } else { + path := util.PathEscapeSegments(ctx.FormString("filepath")) + headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := ParseCompareInfo(ctx) + if ctx.Written() { + return + } + defer headGitRepo.Close() + + _ = PrepareComapreDiff(ctx, headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch, path) + + result := make([]CompareCommit, 0) + for _, commit := range compareInfo.Commits { + compareCommit := CompareCommit{ + Commit: commit, + Sha: commit.ID.String(), + } + for _, p := range commit.Parents { + compareCommit.ParentShas = append(compareCommit.ParentShas, p.String()) + } + result = append(result, compareCommit) + } + + different := struct { + Commits []CompareCommit + Diff interface{} + CommitsCount int + Latestsha string + }{ + Commits: result, + Diff: ctx.Data["Diff"], + } + + different.CommitsCount = len(compareInfo.Commits) + different.Latestsha = compareInfo.HeadCommitID + + ctx.JSON(http.StatusOK, different) + } + } - different := struct { - Commits []CompareCommit - Diff interface{} - CommitsCount int - Latestsha string - FilesCount int - }{ - Commits: result, - // Diff: ctx.Data["Diff"], - } - - different.CommitsCount = len(compareInfo.Commits) - different.Latestsha = compareInfo.HeadCommitID - different.FilesCount = compareInfo.NumFiles - - ctx.JSON(http.StatusOK, different) } func CompareFiles(ctx *context.APIContext) { @@ -255,17 +367,25 @@ func ParseCompareInfo(ctx *context.APIContext) (*user_model.User, *repo_model.Re headRepo *repo_model.Repository headBranch string isSameRepo bool + infoPath string err error directComparison bool ) - shaFrom := ctx.Params("shaFrom") - shaTo := ctx.Params("shaTo") + infoPath = ctx.Params("*") var infos []string - if shaFrom == "" || shaTo == "" { + if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - infos = []string{shaFrom, shaTo} + infos = strings.SplitN(infoPath, "...", 2) + if len(infos) != 2 { + if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { + directComparison = true + ctx.Data["PageIsComparePull"] = false + } else { + infos = []string{baseRepo.DefaultBranch, infoPath} + } + } } ctx.Data["BaseName"] = baseRepo.OwnerName