Skip to content

Commit 49064a4

Browse files
committed
extend CommitTree func
1 parent 2f9564f commit 49064a4

File tree

8 files changed

+93
-7
lines changed

8 files changed

+93
-7
lines changed

modules/repofiles/delete.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type DeleteRepoFileOptions struct {
2323
SHA string
2424
Author *IdentityOptions
2525
Committer *IdentityOptions
26+
Dates *CommitDateOptions
2627
}
2728

2829
// DeleteRepoFile deletes a file in the given repository
@@ -168,7 +169,7 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo
168169
}
169170

170171
// Now commit the tree
171-
commitHash, err := t.CommitTree(author, committer, treeHash, message)
172+
commitHash, err := t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Commiter)
172173
if err != nil {
173174
return nil, err
174175
}

modules/repofiles/temp_repo.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, erro
188188

189189
// CommitTree creates a commit from a given tree for the user with provided message
190190
func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, treeHash string, message string) (string, error) {
191-
commitTimeStr := time.Now().Format(time.RFC3339)
191+
return t.CommitTreeWithDate(author, committer, treeHash, message, time.Now(), time.Now())
192+
}
193+
194+
// CommitTreeWithDate creates a commit from a given tree for the user with provided message
195+
func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models.User, treeHash string, message string, authorDate, commiterDate time.Time) (string, error) {
192196
authorSig := author.NewGitSig()
193197
committerSig := committer.NewGitSig()
194198

@@ -201,10 +205,10 @@ func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, t
201205
env := append(os.Environ(),
202206
"GIT_AUTHOR_NAME="+authorSig.Name,
203207
"GIT_AUTHOR_EMAIL="+authorSig.Email,
204-
"GIT_AUTHOR_DATE="+commitTimeStr,
208+
"GIT_AUTHOR_DATE="+authorDate.Format(time.RFC3339),
205209
"GIT_COMMITTER_NAME="+committerSig.Name,
206210
"GIT_COMMITTER_EMAIL="+committerSig.Email,
207-
"GIT_COMMITTER_DATE="+commitTimeStr,
211+
"GIT_COMMITTER_DATE="+commiterDate.Format(time.RFC3339),
208212
)
209213

210214
messageBytes := new(bytes.Buffer)

modules/repofiles/update.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"path"
1212
"strings"
13+
"time"
1314

1415
"code.gitea.io/gitea/models"
1516
"code.gitea.io/gitea/modules/cache"
@@ -31,6 +32,12 @@ type IdentityOptions struct {
3132
Email string
3233
}
3334

35+
// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
36+
type CommitDateOptions struct {
37+
Author time.Time
38+
Commiter time.Time
39+
}
40+
3441
// UpdateRepoFileOptions holds the repository file update options
3542
type UpdateRepoFileOptions struct {
3643
LastCommitID string
@@ -44,6 +51,7 @@ type UpdateRepoFileOptions struct {
4451
IsNewFile bool
4552
Author *IdentityOptions
4653
Committer *IdentityOptions
54+
Dates *CommitDateOptions
4755
}
4856

4957
func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string, bool) {
@@ -371,7 +379,7 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
371379
}
372380

373381
// Now commit the tree
374-
commitHash, err := t.CommitTree(author, committer, treeHash, message)
382+
commitHash, err := t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Commiter)
375383
if err != nil {
376384
return nil, err
377385
}

modules/structs/repo_commit.go

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
package structs
77

8+
import (
9+
"time"
10+
)
11+
812
// Identity for a person's identity like an author or committer
913
type Identity struct {
1014
Name string `json:"name" binding:"MaxSize(100)"`
@@ -42,3 +46,11 @@ type Commit struct {
4246
Committer *User `json:"committer"`
4347
Parents []*CommitMeta `json:"parents"`
4448
}
49+
50+
// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
51+
type CommitDateOptions struct {
52+
// swagger:strfmt date-time
53+
Author time.Time `json:"author"`
54+
// swagger:strfmt date-time
55+
Commiter time.Time `json:"commiter"`
56+
}

modules/structs/repo_file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type FileOptions struct {
1414
// new_branch (optional) will make a new branch from `branch` before creating the file
1515
NewBranchName string `json:"new_branch" binding:"GitRefName;MaxSize(100)"`
1616
// `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
17-
Author Identity `json:"author"`
18-
Committer Identity `json:"committer"`
17+
Author Identity `json:"author"`
18+
Committer Identity `json:"committer"`
19+
Dates CommitDateOptions `json:"dates"`
1920
}
2021

2122
// CreateFileOptions options for creating files

routers/api/v1/repo/file.go

+31
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package repo
88
import (
99
"encoding/base64"
1010
"net/http"
11+
"time"
1112

1213
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/context"
@@ -213,6 +214,16 @@ func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
213214
Name: apiOpts.Author.Name,
214215
Email: apiOpts.Author.Email,
215216
},
217+
Dates: &repofiles.CommitDateOptions{
218+
Author: apiOpts.Dates.Author,
219+
Commiter: apiOpts.Dates.Author,
220+
},
221+
}
222+
if opts.Dates.Author.IsZero() {
223+
opts.Dates.Author = time.Now()
224+
}
225+
if opts.Dates.Commiter.IsZero() {
226+
opts.Dates.Commiter = time.Now()
216227
}
217228

218229
if opts.Message == "" {
@@ -277,6 +288,16 @@ func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
277288
Name: apiOpts.Author.Name,
278289
Email: apiOpts.Author.Email,
279290
},
291+
Dates: &repofiles.CommitDateOptions{
292+
Author: apiOpts.Dates.Author,
293+
Commiter: apiOpts.Dates.Author,
294+
},
295+
}
296+
if opts.Dates.Author.IsZero() {
297+
opts.Dates.Author = time.Now()
298+
}
299+
if opts.Dates.Commiter.IsZero() {
300+
opts.Dates.Commiter = time.Now()
280301
}
281302

282303
if opts.Message == "" {
@@ -364,6 +385,16 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
364385
Name: apiOpts.Author.Name,
365386
Email: apiOpts.Author.Email,
366387
},
388+
Dates: &repofiles.CommitDateOptions{
389+
Author: apiOpts.Dates.Author,
390+
Commiter: apiOpts.Dates.Author,
391+
},
392+
}
393+
if opts.Dates.Author.IsZero() {
394+
opts.Dates.Author = time.Now()
395+
}
396+
if opts.Dates.Commiter.IsZero() {
397+
opts.Dates.Commiter = time.Now()
367398
}
368399

369400
if opts.Message == "" {

routers/api/v1/swagger/options.go

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ type swaggerParameterBodies struct {
118118
// in:body
119119
DeleteFileOptions api.DeleteFileOptions
120120

121+
// in:body
122+
CommitDateOptions api.CommitDateOptions
123+
121124
// in:body
122125
RepoTopicOptions api.RepoTopicOptions
123126
}

templates/swagger/v1_json.tmpl

+26
Original file line numberDiff line numberDiff line change
@@ -8273,6 +8273,23 @@
82738273
},
82748274
"x-go-package": "code.gitea.io/gitea/modules/structs"
82758275
},
8276+
"CommitDateOptions": {
8277+
"description": "CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE",
8278+
"type": "object",
8279+
"properties": {
8280+
"author": {
8281+
"type": "string",
8282+
"format": "date-time",
8283+
"x-go-name": "Author"
8284+
},
8285+
"commiter": {
8286+
"type": "string",
8287+
"format": "date-time",
8288+
"x-go-name": "Commiter"
8289+
}
8290+
},
8291+
"x-go-package": "code.gitea.io/gitea/modules/structs"
8292+
},
82768293
"CommitMeta": {
82778294
"type": "object",
82788295
"title": "CommitMeta contains meta information of a commit in terms of API.",
@@ -8414,6 +8431,9 @@
84148431
"type": "string",
84158432
"x-go-name": "Content"
84168433
},
8434+
"dates": {
8435+
"$ref": "#/definitions/CommitDateOptions"
8436+
},
84178437
"message": {
84188438
"description": "message (optional) for the commit of this file. if not supplied, a default message will be used",
84198439
"type": "string",
@@ -8972,6 +8992,9 @@
89728992
"committer": {
89738993
"$ref": "#/definitions/Identity"
89748994
},
8995+
"dates": {
8996+
"$ref": "#/definitions/CommitDateOptions"
8997+
},
89758998
"message": {
89768999
"description": "message (optional) for the commit of this file. if not supplied, a default message will be used",
89779000
"type": "string",
@@ -11303,6 +11326,9 @@
1130311326
"type": "string",
1130411327
"x-go-name": "Content"
1130511328
},
11329+
"dates": {
11330+
"$ref": "#/definitions/CommitDateOptions"
11331+
},
1130611332
"from_path": {
1130711333
"description": "from_path (optional) is the path of the original file which will be moved/renamed to the path in the URL",
1130811334
"type": "string",

0 commit comments

Comments
 (0)