|
6 | 6 | package repo
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "math" |
| 10 | + "strconv" |
9 | 11 | "time"
|
10 | 12 |
|
11 | 13 | "code.gitea.io/gitea/models"
|
@@ -120,3 +122,204 @@ func GetSingleCommit(ctx *context.APIContext) {
|
120 | 122 | Parents: apiParents,
|
121 | 123 | })
|
122 | 124 | }
|
| 125 | + |
| 126 | +// GetAllCommits get all commits via |
| 127 | +func GetAllCommits(ctx *context.APIContext) { |
| 128 | + // swagger:operation GET /repos/{owner}/{repo}/git/commits repository repoGetAllCommits |
| 129 | + // --- |
| 130 | + // summary: Get a list of all commits from a repository |
| 131 | + // produces: |
| 132 | + // - application/json |
| 133 | + // parameters: |
| 134 | + // - name: owner |
| 135 | + // in: path |
| 136 | + // description: owner of the repo |
| 137 | + // type: string |
| 138 | + // required: true |
| 139 | + // - name: repo |
| 140 | + // in: path |
| 141 | + // description: name of the repo |
| 142 | + // type: string |
| 143 | + // required: true |
| 144 | + // - name: sha |
| 145 | + // in: query |
| 146 | + // description: SHA or branch to start listing commits from (usually 'master') |
| 147 | + // type: string |
| 148 | + // - name: page |
| 149 | + // in: query |
| 150 | + // description: page number of requested commits |
| 151 | + // type: integer |
| 152 | + // responses: |
| 153 | + // "200": |
| 154 | + // "$ref": "#/responses/CommitList" |
| 155 | + // "404": |
| 156 | + // "$ref": "#/responses/notFound" |
| 157 | + |
| 158 | + gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath()) |
| 159 | + if err != nil { |
| 160 | + ctx.ServerError("OpenRepository", err) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + page := ctx.QueryInt("page") |
| 165 | + if page <= 0 { |
| 166 | + page = 1 |
| 167 | + } |
| 168 | + |
| 169 | + sha := ctx.Query("sha") |
| 170 | + |
| 171 | + var baseCommit *git.Commit |
| 172 | + if len(sha) == 0 { |
| 173 | + // no sha supplied - use default branch |
| 174 | + head, err := gitRepo.GetHEADBranch() |
| 175 | + if err != nil { |
| 176 | + ctx.ServerError("GetHEADBranch", err) |
| 177 | + return |
| 178 | + } |
| 179 | + baseCommit, err = gitRepo.GetBranchCommit(head.Name) |
| 180 | + if err != nil { |
| 181 | + ctx.ServerError("GetCommit", err) |
| 182 | + return |
| 183 | + } |
| 184 | + } else { |
| 185 | + // get commit specified by sha |
| 186 | + baseCommit, err = gitRepo.GetCommit(sha) |
| 187 | + if err != nil { |
| 188 | + ctx.ServerError("GetCommit", err) |
| 189 | + return |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // Total commit count |
| 194 | + commitsCountTotal, err := baseCommit.CommitsCount() |
| 195 | + if err != nil { |
| 196 | + ctx.ServerError("GetCommitsCount", err) |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(git.CommitsRangeSize))) |
| 201 | + |
| 202 | + // Query commits |
| 203 | + commits, err := baseCommit.CommitsByRange(page) |
| 204 | + if err != nil { |
| 205 | + ctx.ServerError("CommitsByRange", err) |
| 206 | + return |
| 207 | + } |
| 208 | + |
| 209 | + userCache := make(map[string]*models.User) |
| 210 | + |
| 211 | + apiCommits := make([]*api.Commit, commits.Len()) |
| 212 | + |
| 213 | + i := 0 |
| 214 | + for commitPointer := commits.Front(); commitPointer != nil; commitPointer = commitPointer.Next() { |
| 215 | + commit := commitPointer.Value.(*git.Commit) |
| 216 | + |
| 217 | + var apiAuthor, apiCommitter *api.User |
| 218 | + |
| 219 | + // Retrieve author and committer information |
| 220 | + cacheAuthor, ok := userCache[commit.Author.Email] |
| 221 | + if ok { |
| 222 | + apiAuthor = cacheAuthor.APIFormat() |
| 223 | + } else { |
| 224 | + author, err := models.GetUserByEmail(commit.Author.Email) |
| 225 | + if err != nil && !models.IsErrUserNotExist(err) { |
| 226 | + ctx.ServerError("Get user by author email", err) |
| 227 | + return |
| 228 | + } else if err == nil { |
| 229 | + apiAuthor = author.APIFormat() |
| 230 | + userCache[commit.Author.Email] = author |
| 231 | + } |
| 232 | + } |
| 233 | + cacheCommitter, ok := userCache[commit.Committer.Email] |
| 234 | + if ok { |
| 235 | + apiCommitter = cacheCommitter.APIFormat() |
| 236 | + } else { |
| 237 | + committer, err := models.GetUserByEmail(commit.Committer.Email) |
| 238 | + if err != nil && !models.IsErrUserNotExist(err) { |
| 239 | + ctx.ServerError("Get user by committer email", err) |
| 240 | + return |
| 241 | + } else if err == nil { |
| 242 | + apiCommitter = committer.APIFormat() |
| 243 | + userCache[commit.Committer.Email] = committer |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + // Retrieve parent(s) of the commit |
| 248 | + apiParents := make([]*api.CommitMeta, commit.ParentCount()) |
| 249 | + for i := 0; i < commit.ParentCount(); i++ { |
| 250 | + sha, _ := commit.ParentID(i) |
| 251 | + apiParents[i] = &api.CommitMeta{ |
| 252 | + URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(), |
| 253 | + SHA: sha.String(), |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + // Create json struct |
| 258 | + apiCommits[i] = &api.Commit{ |
| 259 | + CommitMeta: &api.CommitMeta{ |
| 260 | + URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + commit.ID.String(), |
| 261 | + SHA: commit.ID.String(), |
| 262 | + }, |
| 263 | + HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(), |
| 264 | + RepoCommit: &api.RepoCommit{ |
| 265 | + URL: setting.AppURL + ctx.Link[1:], |
| 266 | + Author: &api.CommitUser{ |
| 267 | + Identity: api.Identity{ |
| 268 | + Name: commit.Committer.Name, |
| 269 | + Email: commit.Committer.Email, |
| 270 | + }, |
| 271 | + Date: commit.Author.When.Format(time.RFC3339), |
| 272 | + }, |
| 273 | + Committer: &api.CommitUser{ |
| 274 | + Identity: api.Identity{ |
| 275 | + Name: commit.Committer.Name, |
| 276 | + Email: commit.Committer.Email, |
| 277 | + }, |
| 278 | + Date: commit.Committer.When.Format(time.RFC3339), |
| 279 | + }, |
| 280 | + Message: commit.Summary(), |
| 281 | + Tree: &api.CommitMeta{ |
| 282 | + URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(), |
| 283 | + SHA: commit.ID.String(), |
| 284 | + }, |
| 285 | + }, |
| 286 | + Author: apiAuthor, |
| 287 | + Committer: apiCommitter, |
| 288 | + Parents: apiParents, |
| 289 | + } |
| 290 | + |
| 291 | + i++ |
| 292 | + } |
| 293 | + |
| 294 | + ctx.SetLinkHeader(int(commitsCountTotal), git.CommitsRangeSize) |
| 295 | + |
| 296 | + ctx.Header().Set("X-Page", strconv.Itoa(page)) |
| 297 | + ctx.Header().Set("X-PerPage", strconv.Itoa(git.CommitsRangeSize)) |
| 298 | + ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10)) |
| 299 | + ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount)) |
| 300 | + ctx.Header().Set("X-HasMore", strconv.FormatBool(page < pageCount)) |
| 301 | + |
| 302 | + ctx.JSON(200, &apiCommits) |
| 303 | +} |
| 304 | + |
| 305 | +// CommitList |
| 306 | +// swagger:response CommitList |
| 307 | +type swaggerCommitList struct { |
| 308 | + // The current page |
| 309 | + Page int `json:"X-Page"` |
| 310 | + |
| 311 | + // Commits per page |
| 312 | + PerPage int `json:"X-PerPage"` |
| 313 | + |
| 314 | + // Total commit count |
| 315 | + Total int `json:"X-Total"` |
| 316 | + |
| 317 | + // Total number of pages |
| 318 | + PageCount int `json:"X-PageCount"` |
| 319 | + |
| 320 | + // True if there is another page |
| 321 | + HasMore bool `json:"X-HasMore"` |
| 322 | + |
| 323 | + //in: body |
| 324 | + Body []api.Commit `json:"body"` |
| 325 | +} |
0 commit comments