Skip to content

Commit f903f76

Browse files
authored
Merge branch 'main' into lfs-parallel-within-batch
2 parents 02d55ff + f2a6df0 commit f903f76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+586
-556
lines changed

modules/git/error.go

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,14 @@
44
package git
55

66
import (
7+
"context"
8+
"errors"
79
"fmt"
810
"strings"
9-
"time"
1011

1112
"code.gitea.io/gitea/modules/util"
1213
)
1314

14-
// ErrExecTimeout error when exec timed out
15-
type ErrExecTimeout struct {
16-
Duration time.Duration
17-
}
18-
19-
// IsErrExecTimeout if some error is ErrExecTimeout
20-
func IsErrExecTimeout(err error) bool {
21-
_, ok := err.(ErrExecTimeout)
22-
return ok
23-
}
24-
25-
func (err ErrExecTimeout) Error() string {
26-
return fmt.Sprintf("execution is timeout [duration: %v]", err.Duration)
27-
}
28-
2915
// ErrNotExist commit not exist error
3016
type ErrNotExist struct {
3117
ID string
@@ -62,21 +48,6 @@ func IsErrBadLink(err error) bool {
6248
return ok
6349
}
6450

65-
// ErrUnsupportedVersion error when required git version not matched
66-
type ErrUnsupportedVersion struct {
67-
Required string
68-
}
69-
70-
// IsErrUnsupportedVersion if some error is ErrUnsupportedVersion
71-
func IsErrUnsupportedVersion(err error) bool {
72-
_, ok := err.(ErrUnsupportedVersion)
73-
return ok
74-
}
75-
76-
func (err ErrUnsupportedVersion) Error() string {
77-
return fmt.Sprintf("Operation requires higher version [required: %s]", err.Required)
78-
}
79-
8051
// ErrBranchNotExist represents a "BranchNotExist" kind of error.
8152
type ErrBranchNotExist struct {
8253
Name string
@@ -185,3 +156,10 @@ func IsErrMoreThanOne(err error) bool {
185156
func (err *ErrMoreThanOne) Error() string {
186157
return fmt.Sprintf("ErrMoreThanOne Error: %v: %s\n%s", err.Err, err.StdErr, err.StdOut)
187158
}
159+
160+
func IsErrCanceledOrKilled(err error) bool {
161+
// When "cancel()" a git command's context, the returned error of "Run()" could be one of them:
162+
// - context.Canceled
163+
// - *exec.ExitError: "signal: killed"
164+
return err != nil && (errors.Is(err, context.Canceled) || err.Error() == "signal: killed")
165+
}

modules/git/repo_attribute.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ func (c *CheckAttributeReader) Run() error {
166166
Stdout: c.stdOut,
167167
Stderr: stdErr,
168168
})
169-
if err != nil && // If there is an error we need to return but:
170-
c.ctx.Err() != err && // 1. Ignore the context error if the context is cancelled or exceeds the deadline (RunWithContext could return c.ctx.Err() which is Canceled or DeadlineExceeded)
171-
err.Error() != "signal: killed" { // 2. We should not pass up errors due to the program being killed
169+
if err != nil && !IsErrCanceledOrKilled(err) {
172170
return fmt.Errorf("failed to run attr-check. Error: %w\nStderr: %s", err, stdErr.String())
173171
}
174172
return nil

modules/repository/repo.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ func pullMirrorReleaseSync(ctx context.Context, repo *repo_model.Repository, git
341341

342342
for _, tag := range updates {
343343
if _, err := db.GetEngine(ctx).Where("repo_id = ? AND lower_tag_name = ?", repo.ID, strings.ToLower(tag.Name)).
344-
Cols("sha1").
344+
Cols("sha1", "created_unix").
345345
Update(&repo_model.Release{
346-
Sha1: tag.Object.String(),
346+
Sha1: tag.Object.String(),
347+
CreatedUnix: timeutil.TimeStamp(tag.Tagger.When.Unix()),
347348
}); err != nil {
348349
return fmt.Errorf("unable to update tag %s for pull-mirror Repo[%d:%s/%s]: %w", tag.Name, repo.ID, repo.OwnerName, repo.Name, err)
349350
}

modules/templates/helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func NewFuncMap() template.FuncMap {
5454
"StringUtils": NewStringUtils,
5555
"SliceUtils": NewSliceUtils,
5656
"JsonUtils": NewJsonUtils,
57+
"DateUtils": NewDateUtils,
5758

5859
// -----------------------------------------------------------------
5960
// svg / avatar / icon / color
@@ -70,7 +71,7 @@ func NewFuncMap() template.FuncMap {
7071
"CountFmt": base.FormatNumberSI,
7172
"TimeSince": timeutil.TimeSince,
7273
"TimeSinceUnix": timeutil.TimeSinceUnix,
73-
"DateTime": timeutil.DateTime,
74+
"DateTime": dateTimeLegacy, // for backward compatibility only, do not use it anymore
7475
"Sec2Time": util.SecToTime,
7576
"LoadTimes": func(startTime time.Time) string {
7677
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"

modules/templates/util_date.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package templates
5+
6+
import (
7+
"context"
8+
"html/template"
9+
"time"
10+
11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/timeutil"
13+
)
14+
15+
type DateUtils struct {
16+
ctx context.Context
17+
}
18+
19+
func NewDateUtils(ctx context.Context) *DateUtils {
20+
return &DateUtils{ctx}
21+
}
22+
23+
// AbsoluteShort renders in "Jan 01, 2006" format
24+
func (du *DateUtils) AbsoluteShort(time any) template.HTML {
25+
return timeutil.DateTime("short", time)
26+
}
27+
28+
// AbsoluteLong renders in "January 01, 2006" format
29+
func (du *DateUtils) AbsoluteLong(time any) template.HTML {
30+
return timeutil.DateTime("short", time)
31+
}
32+
33+
// FullTime renders in "Jan 01, 2006 20:33:44" format
34+
func (du *DateUtils) FullTime(time any) template.HTML {
35+
return timeutil.DateTime("full", time)
36+
}
37+
38+
// ParseLegacy parses the datetime in legacy format, eg: "2016-01-02" in server's timezone.
39+
// It shouldn't be used in new code. New code should use Time or TimeStamp as much as possible.
40+
func (du *DateUtils) ParseLegacy(datetime string) time.Time {
41+
return parseLegacy(datetime)
42+
}
43+
44+
func parseLegacy(datetime string) time.Time {
45+
t, err := time.Parse(time.RFC3339, datetime)
46+
if err != nil {
47+
t, _ = time.ParseInLocation(time.DateOnly, datetime, setting.DefaultUILocation)
48+
}
49+
return t
50+
}
51+
52+
func dateTimeLegacy(format string, datetime any, _ ...string) template.HTML {
53+
if !setting.IsProd || setting.IsInTesting {
54+
panic("dateTimeLegacy is for backward compatibility only, do not use it in new code")
55+
}
56+
if s, ok := datetime.(string); ok {
57+
datetime = parseLegacy(s)
58+
}
59+
return timeutil.DateTime(format, datetime)
60+
}
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
// Copyright 2023 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
package timeutil
4+
package templates
55

66
import (
77
"testing"
88
"time"
99

1010
"code.gitea.io/gitea/modules/setting"
1111
"code.gitea.io/gitea/modules/test"
12+
"code.gitea.io/gitea/modules/timeutil"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
1516

1617
func TestDateTime(t *testing.T) {
1718
testTz, _ := time.LoadLocation("America/New_York")
1819
defer test.MockVariableValue(&setting.DefaultUILocation, testTz)()
20+
defer test.MockVariableValue(&setting.IsInTesting, false)()
21+
22+
du := NewDateUtils(nil)
1923

2024
refTimeStr := "2018-01-01T00:00:00Z"
2125
refDateStr := "2018-01-01"
2226
refTime, _ := time.Parse(time.RFC3339, refTimeStr)
23-
refTimeStamp := TimeStamp(refTime.Unix())
27+
refTimeStamp := timeutil.TimeStamp(refTime.Unix())
2428

25-
assert.EqualValues(t, "-", DateTime("short", nil))
26-
assert.EqualValues(t, "-", DateTime("short", 0))
27-
assert.EqualValues(t, "-", DateTime("short", time.Time{}))
28-
assert.EqualValues(t, "-", DateTime("short", TimeStamp(0)))
29+
assert.EqualValues(t, "-", du.AbsoluteShort(nil))
30+
assert.EqualValues(t, "-", du.AbsoluteShort(0))
31+
assert.EqualValues(t, "-", du.AbsoluteShort(time.Time{}))
32+
assert.EqualValues(t, "-", du.AbsoluteShort(timeutil.TimeStamp(0)))
2933

30-
actual := DateTime("short", "invalid")
31-
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="invalid">invalid</absolute-date>`, actual)
34+
actual := dateTimeLegacy("short", "invalid")
35+
assert.EqualValues(t, `-`, actual)
3236

33-
actual = DateTime("short", refTimeStr)
34-
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00Z">2018-01-01T00:00:00Z</absolute-date>`, actual)
37+
actual = dateTimeLegacy("short", refTimeStr)
38+
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00Z">2018-01-01</absolute-date>`, actual)
3539

36-
actual = DateTime("short", refTime)
40+
actual = du.AbsoluteShort(refTime)
3741
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00Z">2018-01-01</absolute-date>`, actual)
3842

39-
actual = DateTime("short", refDateStr)
40-
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01">2018-01-01</absolute-date>`, actual)
43+
actual = dateTimeLegacy("short", refDateStr)
44+
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2018-01-01T00:00:00-05:00">2018-01-01</absolute-date>`, actual)
4145

42-
actual = DateTime("short", refTimeStamp)
46+
actual = du.AbsoluteShort(refTimeStamp)
4347
assert.EqualValues(t, `<absolute-date weekday="" year="numeric" month="short" day="numeric" date="2017-12-31T19:00:00-05:00">2017-12-31</absolute-date>`, actual)
4448

45-
actual = DateTime("full", refTimeStamp)
49+
actual = du.FullTime(refTimeStamp)
4650
assert.EqualValues(t, `<relative-time weekday="" year="numeric" format="datetime" month="short" day="numeric" hour="numeric" minute="numeric" second="numeric" data-tooltip-content data-tooltip-interactive="true" datetime="2017-12-31T19:00:00-05:00">2017-12-31 19:00:00 -05:00</relative-time>`, actual)
4751
}

modules/timeutil/datetime.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
)
1313

1414
// DateTime renders an absolute time HTML element by datetime.
15-
func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
16-
// TODO: remove the extraAttrs argument, it's not used in any call to DateTime
17-
15+
func DateTime(format string, datetime any) template.HTML {
1816
if p, ok := datetime.(*time.Time); ok {
1917
datetime = *p
2018
}
@@ -34,9 +32,6 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
3432
switch v := datetime.(type) {
3533
case nil:
3634
return "-"
37-
case string:
38-
datetimeEscaped = html.EscapeString(v)
39-
textEscaped = datetimeEscaped
4035
case time.Time:
4136
if v.IsZero() || v.Unix() == 0 {
4237
return "-"
@@ -51,10 +46,7 @@ func DateTime(format string, datetime any, extraAttrs ...string) template.HTML {
5146
panic(fmt.Sprintf("Unsupported time type %T", datetime))
5247
}
5348

54-
attrs := make([]string, 0, 10+len(extraAttrs))
55-
attrs = append(attrs, extraAttrs...)
56-
attrs = append(attrs, `weekday=""`, `year="numeric"`)
57-
49+
attrs := []string{`weekday=""`, `year="numeric"`}
5850
switch format {
5951
case "short", "long": // date only
6052
attrs = append(attrs, `month="`+format+`"`, `day="numeric"`)

routers/api/v1/repo/repo.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
repo_model "code.gitea.io/gitea/models/repo"
2222
unit_model "code.gitea.io/gitea/models/unit"
2323
user_model "code.gitea.io/gitea/models/user"
24-
"code.gitea.io/gitea/modules/git"
2524
"code.gitea.io/gitea/modules/gitrepo"
2625
"code.gitea.io/gitea/modules/label"
2726
"code.gitea.io/gitea/modules/log"
@@ -739,10 +738,8 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
739738
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
740739
if !repo.IsEmpty {
741740
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
742-
if !git.IsErrUnsupportedVersion(err) {
743-
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
744-
return err
745-
}
741+
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
742+
return err
746743
}
747744
updateRepoLicense = true
748745
}

routers/private/default_branch.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/http"
99

1010
repo_model "code.gitea.io/gitea/models/repo"
11-
"code.gitea.io/gitea/modules/git"
1211
"code.gitea.io/gitea/modules/gitrepo"
1312
"code.gitea.io/gitea/modules/private"
1413
gitea_context "code.gitea.io/gitea/services/context"
@@ -23,12 +22,10 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
2322

2423
ctx.Repo.Repository.DefaultBranch = branch
2524
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
26-
if !git.IsErrUnsupportedVersion(err) {
27-
ctx.JSON(http.StatusInternalServerError, private.Response{
28-
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
29-
})
30-
return
31-
}
25+
ctx.JSON(http.StatusInternalServerError, private.Response{
26+
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
27+
})
28+
return
3229
}
3330

3431
if err := repo_model.UpdateDefaultBranch(ctx, ctx.Repo.Repository); err != nil {

routers/web/repo/activity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func Activity(ctx *context.Context) {
4848
ctx.Data["Period"] = "weekly"
4949
timeFrom = timeUntil.Add(-time.Hour * 168)
5050
}
51-
ctx.Data["DateFrom"] = timeFrom.UTC().Format(time.RFC3339)
52-
ctx.Data["DateUntil"] = timeUntil.UTC().Format(time.RFC3339)
51+
ctx.Data["DateFrom"] = timeFrom
52+
ctx.Data["DateUntil"] = timeUntil
5353
ctx.Data["PeriodText"] = ctx.Tr("repo.activity.period." + ctx.Data["Period"].(string))
5454

5555
var err error

0 commit comments

Comments
 (0)