From 40e99ea010b3b0be241be0e1365a70d33777bcf2 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 24 Dec 2019 03:33:52 +0100 Subject: [API] Extend contents with dates (#9464) * extend CommitTree func * make sure Date NOT nil * spell corection Co-Authored-By: zeripath * add TEST Co-authored-by: zeripath Co-authored-by: Lunny Xiao --- modules/repofiles/delete.go | 8 +++++++- modules/repofiles/temp_repo.go | 10 +++++++--- modules/repofiles/update.go | 15 ++++++++++++++- modules/structs/repo_commit.go | 12 ++++++++++++ modules/structs/repo_file.go | 5 +++-- 5 files changed, 43 insertions(+), 7 deletions(-) (limited to 'modules') diff --git a/modules/repofiles/delete.go b/modules/repofiles/delete.go index 95b0804025..43937c49e1 100644 --- a/modules/repofiles/delete.go +++ b/modules/repofiles/delete.go @@ -23,6 +23,7 @@ type DeleteRepoFileOptions struct { SHA string Author *IdentityOptions Committer *IdentityOptions + Dates *CommitDateOptions } // DeleteRepoFile deletes a file in the given repository @@ -168,7 +169,12 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo } // Now commit the tree - commitHash, err := t.CommitTree(author, committer, treeHash, message) + var commitHash string + if opts.Dates != nil { + commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer) + } else { + commitHash, err = t.CommitTree(author, committer, treeHash, message) + } if err != nil { return nil, err } diff --git a/modules/repofiles/temp_repo.go b/modules/repofiles/temp_repo.go index 6bd775d9d2..f9ea4ba155 100644 --- a/modules/repofiles/temp_repo.go +++ b/modules/repofiles/temp_repo.go @@ -188,7 +188,11 @@ func (t *TemporaryUploadRepository) GetLastCommitByRef(ref string) (string, erro // CommitTree creates a commit from a given tree for the user with provided message func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, treeHash string, message string) (string, error) { - commitTimeStr := time.Now().Format(time.RFC3339) + return t.CommitTreeWithDate(author, committer, treeHash, message, time.Now(), time.Now()) +} + +// CommitTreeWithDate creates a commit from a given tree for the user with provided message +func (t *TemporaryUploadRepository) CommitTreeWithDate(author, committer *models.User, treeHash string, message string, authorDate, committerDate time.Time) (string, error) { authorSig := author.NewGitSig() committerSig := committer.NewGitSig() @@ -201,10 +205,10 @@ func (t *TemporaryUploadRepository) CommitTree(author, committer *models.User, t env := append(os.Environ(), "GIT_AUTHOR_NAME="+authorSig.Name, "GIT_AUTHOR_EMAIL="+authorSig.Email, - "GIT_AUTHOR_DATE="+commitTimeStr, + "GIT_AUTHOR_DATE="+authorDate.Format(time.RFC3339), "GIT_COMMITTER_NAME="+committerSig.Name, "GIT_COMMITTER_EMAIL="+committerSig.Email, - "GIT_COMMITTER_DATE="+commitTimeStr, + "GIT_COMMITTER_DATE="+committerDate.Format(time.RFC3339), ) messageBytes := new(bytes.Buffer) diff --git a/modules/repofiles/update.go b/modules/repofiles/update.go index 4d2f1d5f04..8a95b4422c 100644 --- a/modules/repofiles/update.go +++ b/modules/repofiles/update.go @@ -10,6 +10,7 @@ import ( "fmt" "path" "strings" + "time" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/cache" @@ -31,6 +32,12 @@ type IdentityOptions struct { Email string } +// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE +type CommitDateOptions struct { + Author time.Time + Committer time.Time +} + // UpdateRepoFileOptions holds the repository file update options type UpdateRepoFileOptions struct { LastCommitID string @@ -44,6 +51,7 @@ type UpdateRepoFileOptions struct { IsNewFile bool Author *IdentityOptions Committer *IdentityOptions + Dates *CommitDateOptions } func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string, bool) { @@ -371,7 +379,12 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up } // Now commit the tree - commitHash, err := t.CommitTree(author, committer, treeHash, message) + var commitHash string + if opts.Dates != nil { + commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer) + } else { + commitHash, err = t.CommitTree(author, committer, treeHash, message) + } if err != nil { return nil, err } diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go index 9cde2873d4..088ccdf5af 100644 --- a/modules/structs/repo_commit.go +++ b/modules/structs/repo_commit.go @@ -5,6 +5,10 @@ package structs +import ( + "time" +) + // Identity for a person's identity like an author or committer type Identity struct { Name string `json:"name" binding:"MaxSize(100)"` @@ -42,3 +46,11 @@ type Commit struct { Committer *User `json:"committer"` Parents []*CommitMeta `json:"parents"` } + +// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE +type CommitDateOptions struct { + // swagger:strfmt date-time + Author time.Time `json:"author"` + // swagger:strfmt date-time + Committer time.Time `json:"committer"` +} diff --git a/modules/structs/repo_file.go b/modules/structs/repo_file.go index cb836e2e23..c34923e389 100644 --- a/modules/structs/repo_file.go +++ b/modules/structs/repo_file.go @@ -14,8 +14,9 @@ type FileOptions struct { // new_branch (optional) will make a new branch from `branch` before creating the file NewBranchName string `json:"new_branch" binding:"GitRefName;MaxSize(100)"` // `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used) - Author Identity `json:"author"` - Committer Identity `json:"committer"` + Author Identity `json:"author"` + Committer Identity `json:"committer"` + Dates CommitDateOptions `json:"dates"` } // CreateFileOptions options for creating files -- cgit v1.2.3