mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
chore: two small refactors in git module (#10109)
Move the function to the repository struct. There is no need to have it as a separate function, move it to the Repository struct. Add extra unit tests. --- Remove a field from a struct. It has nothing to do with git, it is not the right place to have that field in the git `Tag` struct. Get this value when it's converted to the API struct. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10109 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
2ea5a8d22b
commit
6ca1656f93
9 changed files with 90 additions and 51 deletions
|
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package git
|
||||
|
|
|
|||
|
|
@ -326,17 +326,6 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
|
||||
func GetLatestCommitTime(ctx context.Context, repoPath string) (time.Time, error) {
|
||||
cmd := NewCommand(ctx, "for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
|
||||
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repoPath})
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
commitTime := strings.TrimSpace(stdout)
|
||||
return time.Parse("Mon Jan _2 15:04:05 2006 -0700", commitTime)
|
||||
}
|
||||
|
||||
// DivergeObject represents commit count diverging commits
|
||||
type DivergeObject struct {
|
||||
Ahead int
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"forgejo.org/modules/cache"
|
||||
"forgejo.org/modules/log"
|
||||
|
|
@ -670,3 +671,14 @@ func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
|
|||
|
||||
return MustIDFromString(string(sha)), nil
|
||||
}
|
||||
|
||||
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
|
||||
func (repo *Repository) GetLatestCommitTime() (time.Time, error) {
|
||||
cmd := NewCommand(repo.Ctx, "for-each-ref", "--sort=-committerdate", "--count=1", "--format=%(committerdate)", BranchPrefix)
|
||||
stdout, _, err := cmd.RunStdString(&RunOpts{Dir: repo.Path})
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
commitTime := strings.TrimSpace(stdout)
|
||||
return time.Parse("Mon Jan _2 15:04:05 2006 -0700", commitTime)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -236,3 +236,41 @@ func TestGetCommitsFromIDs(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetLatestCommitTime(t *testing.T) {
|
||||
t.Run("repo1", func(t *testing.T) {
|
||||
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare"))
|
||||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
lct, err := repo.GetLatestCommitTime()
|
||||
require.NoError(t, err)
|
||||
// Time is Sun Nov 13 16:40:14 2022 +0100
|
||||
// which is the time of commit
|
||||
// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
|
||||
assert.EqualValues(t, 1668354014, lct.Unix())
|
||||
})
|
||||
|
||||
t.Run("repo1_sha256", func(t *testing.T) {
|
||||
skipIfSHA256NotSupported(t)
|
||||
|
||||
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare_sha256"))
|
||||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
lct, err := repo.GetLatestCommitTime()
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, 1698676906, lct.Unix())
|
||||
})
|
||||
|
||||
t.Run("repo3_notes", func(t *testing.T) {
|
||||
repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo3_notes"))
|
||||
require.NoError(t, err)
|
||||
defer repo.Close()
|
||||
|
||||
lct, err := repo.GetLatestCommitTime()
|
||||
require.NoError(t, err)
|
||||
// Time is of refs/heads/master and not of refs/notes/commits
|
||||
assert.EqualValues(t, 1567767909, lct.Unix())
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,16 +19,6 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGetLatestCommitTime(t *testing.T) {
|
||||
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
||||
lct, err := GetLatestCommitTime(DefaultContext, bareRepo1Path)
|
||||
require.NoError(t, err)
|
||||
// Time is Sun Nov 13 16:40:14 2022 +0100
|
||||
// which is the time of commit
|
||||
// ce064814f4a0d337b333e646ece456cd39fab612 (refs/heads/master)
|
||||
assert.EqualValues(t, 1668354014, lct.Unix())
|
||||
}
|
||||
|
||||
func TestRepoIsEmpty(t *testing.T) {
|
||||
emptyRepo2Path := filepath.Join(testReposDir, "repo2_empty")
|
||||
repo, err := openRepositoryWithDefaultContext(emptyRepo2Path)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// Copyright 2015 The Gogs Authors. All rights reserved.
|
||||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package git
|
||||
|
|
@ -7,7 +8,6 @@ import (
|
|||
"bytes"
|
||||
"strings"
|
||||
|
||||
api "forgejo.org/modules/structs"
|
||||
"forgejo.org/modules/util"
|
||||
)
|
||||
|
||||
|
|
@ -20,14 +20,13 @@ const (
|
|||
|
||||
// Tag represents a Git tag.
|
||||
type Tag struct {
|
||||
Name string
|
||||
ID ObjectID
|
||||
Object ObjectID // The id of this commit object
|
||||
Type string
|
||||
Tagger *Signature
|
||||
Message string
|
||||
Signature *ObjectSignature
|
||||
ArchiveDownloadCount *api.TagArchiveDownloadCount
|
||||
Name string
|
||||
ID ObjectID
|
||||
Object ObjectID // The id of this commit object
|
||||
Type string
|
||||
Tagger *Signature
|
||||
Message string
|
||||
Signature *ObjectSignature
|
||||
}
|
||||
|
||||
// Commit return the commit of the tag reference
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue