Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to get file commit history #17652

Merged
merged 26 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9634f56
Add API to get file comment history
qwerty287 Nov 15, 2021
dc1c3a5
Update swagger description
qwerty287 Nov 15, 2021
f7b15df
Update swagger
qwerty287 Nov 16, 2021
be658d7
Use user cache
qwerty287 Nov 16, 2021
ddc2f7f
Merge branch 'main' into api-file-hist
qwerty287 Nov 16, 2021
b19d8f0
Fix test
qwerty287 Nov 16, 2021
963a718
Merge branch 'main' into api-file-hist
qwerty287 Nov 18, 2021
0f74927
Merge branch 'main' into api-file-hist
qwerty287 Nov 21, 2021
c690801
Update desc
qwerty287 Nov 22, 2021
803bb05
Merge branch 'main' into api-file-hist
qwerty287 Nov 22, 2021
4376f26
Merge branch 'main' into api-file-hist
lunny Nov 23, 2021
0132166
Merge branch 'main' into api-file-hist
qwerty287 Nov 23, 2021
3d2f168
Merge branch 'main' into api-file-hist
qwerty287 Nov 25, 2021
0c1d718
Merge branch 'main' into api-file-hist
qwerty287 Nov 26, 2021
388db13
Merge branch 'main' into api-file-hist
qwerty287 Nov 29, 2021
a27b524
Merge branch 'main' into api-file-hist
qwerty287 Dec 5, 2021
2ee9ac8
Merge branch 'main' into api-file-hist
qwerty287 Dec 12, 2021
f91aba5
Move func to commits.go
qwerty287 Dec 13, 2021
4ebd8c0
Use GitHub-compatible endpoint
qwerty287 Dec 13, 2021
92e3de5
Update swagger
qwerty287 Dec 13, 2021
887addb
fmt
qwerty287 Dec 13, 2021
e1d9fd6
Merge branch 'main' into api-file-hist
qwerty287 Dec 16, 2021
1831fb5
Merge branch 'main' into api-file-hist
qwerty287 Dec 21, 2021
01c8883
Fix empty list
qwerty287 Dec 21, 2021
07f853e
Fix test
qwerty287 Dec 21, 2021
905e814
Merge branch 'main' into api-file-hist
lunny Dec 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions integrations/api_repo_git_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ func TestDownloadCommitDiffOrPatch(t *testing.T) {
resp.Body.String())

}

func TestGetFileHistory(t *testing.T) {
defer prepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
// Login as User2.
session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session)
qwerty287 marked this conversation as resolved.
Show resolved Hide resolved

req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?path=readme.md&token="+token+"&sha=good-sign", user.Name)
resp := session.MakeRequest(t, req, http.StatusOK)

var apiData []api.Commit
DecodeJSON(t, resp, &apiData)

assert.Len(t, apiData, 1)
assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files)
}
79 changes: 55 additions & 24 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ func GetAllCommits(ctx *context.APIContext) {
// in: query
// description: SHA or branch to start listing commits from (usually 'master')
// type: string
// - name: path
// in: query
// description: filepath of a file/dir
// type: string
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// description: page size of results (ignored if used with 'path')
// type: integer
// responses:
// "200":
Expand Down Expand Up @@ -149,46 +153,73 @@ func GetAllCommits(ctx *context.APIContext) {
}

sha := ctx.FormString("sha")
path := ctx.FormString("path")

var (
commitsCountTotal int64
commits []*git.Commit
)

if len(path) == 0 {
var baseCommit *git.Commit
if len(sha) == 0 {
// no sha supplied - use default branch
head, err := gitRepo.GetHEADBranch()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
return
}

baseCommit, err = gitRepo.GetBranchCommit(head.Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
} else {
// get commit specified by sha
baseCommit, err = gitRepo.GetCommit(sha)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
}

var baseCommit *git.Commit
if len(sha) == 0 {
// no sha supplied - use default branch
head, err := gitRepo.GetHEADBranch()
// Total commit count
commitsCountTotal, err = baseCommit.CommitsCount()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetHEADBranch", err)
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
return
}

baseCommit, err = gitRepo.GetBranchCommit(head.Name)
// Query commits
commits, err = baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
return
}
} else {
// get commit specified by sha
baseCommit, err = gitRepo.GetCommit(sha)
if len(sha) == 0 {
sha = ctx.Repo.Repository.DefaultBranch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still use head, err := gitRepo.GetHEADBranch()?

Copy link
Contributor Author

@qwerty287 qwerty287 Dec 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

  1. it always returns an empty list, if without sha or with does not change this (and it doesn't work with pure SHAs or with branch names).
  2. tested it with and it does not work
  3. it worked on a separate endpoint

}

commitsCountTotal, err = gitRepo.FileCommitsCount(sha, path)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
ctx.Error(http.StatusInternalServerError, "FileCommitsCount", err)
return
} else if commitsCountTotal == 0 {
ctx.NotFound("FileCommitsCount", nil)
return
}
}

// Total commit count
commitsCountTotal, err := baseCommit.CommitsCount()
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommitsCount", err)
return
commits, err = gitRepo.CommitsByFileAndRange(sha, path, listOptions.Page)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err)
return
}
}

pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))

// Query commits
commits, err := baseCommit.CommitsByRange(listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CommitsByRange", err)
return
}

userCache := make(map[string]*user_model.User)

apiCommits := make([]*api.Commit, len(commits))
Expand Down
8 changes: 7 additions & 1 deletion templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,12 @@
"name": "sha",
"in": "query"
},
{
"type": "string",
"description": "filepath of a file/dir",
"name": "path",
"in": "query"
},
{
"type": "integer",
"description": "page number of results to return (1-based)",
Expand All @@ -2958,7 +2964,7 @@
},
{
"type": "integer",
"description": "page size of results",
"description": "page size of results (ignored if used with 'path')",
"name": "limit",
"in": "query"
}
Expand Down