Skip to content

Commit 671b457

Browse files
authored
Merge branch 'main' into main
2 parents 585dfe5 + 4bbbf35 commit 671b457

38 files changed

+286
-34
lines changed

cmd/admin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ func runChangePassword(c *cli.Context) error {
379379
if err := initDB(ctx); err != nil {
380380
return err
381381
}
382+
if len(c.String("password")) < setting.MinPasswordLength {
383+
return fmt.Errorf("Password is not long enough. Needs to be at least %d", setting.MinPasswordLength)
384+
}
385+
382386
if !pwd.IsComplexEnough(c.String("password")) {
383387
return errors.New("Password does not meet complexity requirements")
384388
}

cmd/dump.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (o outputType) String() string {
8686
}
8787

8888
var outputTypeEnum = &outputType{
89-
Enum: []string{"zip", "tar", "tar.gz", "tar.xz", "tar.bz2"},
89+
Enum: []string{"zip", "rar", "tar", "sz", "tar.gz", "tar.xz", "tar.bz2", "tar.br", "tar.lz4"},
9090
Default: "zip",
9191
}
9292

@@ -152,12 +152,16 @@ func fatal(format string, args ...interface{}) {
152152
func runDump(ctx *cli.Context) error {
153153
var file *os.File
154154
fileName := ctx.String("file")
155+
outType := ctx.String("type")
155156
if fileName == "-" {
156157
file = os.Stdout
157158
err := log.DelLogger("console")
158159
if err != nil {
159160
fatal("Deleting default logger failed. Can not write to stdout: %v", err)
160161
}
162+
} else {
163+
fileName = strings.TrimSuffix(fileName, path.Ext(fileName))
164+
fileName += "." + outType
161165
}
162166
setting.LoadFromExisting()
163167

@@ -200,7 +204,6 @@ func runDump(ctx *cli.Context) error {
200204
}
201205

202206
verbose := ctx.Bool("verbose")
203-
outType := ctx.String("type")
204207
var iface interface{}
205208
if fileName == "-" {
206209
iface, err = archiver.ByExtension(fmt.Sprintf(".%s", outType))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3a810dbf6b96afaa8c5f69a8b6ec1dabfca7368b
1+
59e2c41e8f5140bb0182acebec17c8ad9831cc62

integrations/integration_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
255255
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
256256

257257
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
258+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
259+
if err != nil {
260+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
261+
}
262+
for _, ownerDir := range ownerDirs {
263+
if !ownerDir.Type().IsDir() {
264+
continue
265+
}
266+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
267+
if err != nil {
268+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
269+
}
270+
for _, repoDir := range repoDirs {
271+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
272+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
273+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
274+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
275+
}
276+
}
258277

259278
return deferFn
260279
}
@@ -532,4 +551,23 @@ func resetFixtures(t *testing.T) {
532551
assert.NoError(t, unittest.LoadFixtures())
533552
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
534553
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
554+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
555+
if err != nil {
556+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
557+
}
558+
for _, ownerDir := range ownerDirs {
559+
if !ownerDir.Type().IsDir() {
560+
continue
561+
}
562+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
563+
if err != nil {
564+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
565+
}
566+
for _, repoDir := range repoDirs {
567+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
568+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
569+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
570+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
571+
}
572+
}
535573
}

integrations/migration-test/migration_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ func initMigrationTest(t *testing.T) func() {
6161
assert.True(t, len(setting.RepoRootPath) != 0)
6262
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
6363
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
64+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
65+
if err != nil {
66+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
67+
}
68+
for _, ownerDir := range ownerDirs {
69+
if !ownerDir.Type().IsDir() {
70+
continue
71+
}
72+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
73+
if err != nil {
74+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
75+
}
76+
for _, repoDir := range repoDirs {
77+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
78+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
79+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
80+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
81+
}
82+
}
6483

6584
git.CheckLFSVersion()
6685
setting.InitDBConfig()

integrations/nonascii_branches_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integrations
66

77
import (
88
"net/http"
9+
"net/url"
910
"path"
1011
"testing"
1112

@@ -159,6 +160,41 @@ func TestNonasciiBranches(t *testing.T) {
159160
to: "tag/%D0%81/%E4%BA%BA",
160161
status: http.StatusOK,
161162
},
163+
{
164+
from: "Plus+Is+Not+Space/%25%252525mightnotplaywell",
165+
to: "branch/Plus+Is+Not+Space/%25%252525mightnotplaywell",
166+
status: http.StatusOK,
167+
},
168+
{
169+
from: "Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
170+
to: "branch/Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
171+
status: http.StatusOK,
172+
},
173+
{
174+
from: "Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
175+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
176+
status: http.StatusOK,
177+
},
178+
{
179+
from: "Plus+Is+Not+Space/10%25.md",
180+
to: "branch/Plus+Is+Not+Space/10%25.md",
181+
status: http.StatusOK,
182+
},
183+
{
184+
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
185+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
186+
status: http.StatusOK,
187+
},
188+
{
189+
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
190+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
191+
status: http.StatusOK,
192+
},
193+
{
194+
from: "Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
195+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
196+
status: http.StatusOK,
197+
},
162198
}
163199

164200
defer prepareTestEnv(t)()

models/action.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/log"
2424
"code.gitea.io/gitea/modules/setting"
2525
"code.gitea.io/gitea/modules/timeutil"
26+
"code.gitea.io/gitea/modules/util"
2627

2728
"xorm.io/builder"
2829
)
@@ -252,6 +253,21 @@ func (a *Action) GetBranch() string {
252253
return strings.TrimPrefix(a.RefName, git.BranchPrefix)
253254
}
254255

256+
// GetRefLink returns the action's ref link.
257+
func (a *Action) GetRefLink() string {
258+
switch {
259+
case strings.HasPrefix(a.RefName, git.BranchPrefix):
260+
return a.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.BranchPrefix))
261+
case strings.HasPrefix(a.RefName, git.TagPrefix):
262+
return a.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.TagPrefix))
263+
case len(a.RefName) == 40 && git.SHAPattern.MatchString(a.RefName):
264+
return a.GetRepoLink() + "/src/commit/" + a.RefName
265+
default:
266+
// FIXME: we will just assume it's a branch - this was the old way - at some point we may want to enforce that there is always a ref here.
267+
return a.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.BranchPrefix))
268+
}
269+
}
270+
255271
// GetTag returns the action's repository tag.
256272
func (a *Action) GetTag() string {
257273
return strings.TrimPrefix(a.RefName, git.TagPrefix)

models/migrations/migrations_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,25 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
207207

208208
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
209209
setting.RepoRootPath))
210+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
211+
if err != nil {
212+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
213+
}
214+
for _, ownerDir := range ownerDirs {
215+
if !ownerDir.Type().IsDir() {
216+
continue
217+
}
218+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
219+
if err != nil {
220+
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
221+
}
222+
for _, repoDir := range repoDirs {
223+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
224+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
225+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
226+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
227+
}
228+
}
210229

211230
if err := deleteDB(); err != nil {
212231
t.Errorf("unable to reset database: %v", err)

models/org_team.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
114114

115115
// ColorFormat provides a basic color format for a Team
116116
func (t *Team) ColorFormat(s fmt.State) {
117+
if t == nil {
118+
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
119+
log.NewColoredIDValue(0),
120+
"<nil>",
121+
log.NewColoredIDValue(0),
122+
0)
123+
return
124+
}
117125
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
118126
log.NewColoredIDValue(t.ID),
119127
t.Name,

models/repo/repo.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ func (repo *Repository) SanitizedOriginalURL() string {
173173

174174
// ColorFormat returns a colored string to represent this repo
175175
func (repo *Repository) ColorFormat(s fmt.State) {
176+
if repo == nil {
177+
log.ColorFprintf(s, "%d:%s/%s",
178+
log.NewColoredIDValue(0),
179+
"<nil>",
180+
"<nil>")
181+
return
182+
}
176183
log.ColorFprintf(s, "%d:%s/%s",
177184
log.NewColoredIDValue(repo.ID),
178185
repo.OwnerName,

models/unittest/testdb.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ func MainTest(m *testing.M, pathToGiteaRoot string, fixtureFiles ...string) {
104104
fatalTestError("util.CopyDir: %v\n", err)
105105
}
106106

107+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
108+
if err != nil {
109+
fatalTestError("unable to read the new repo root: %v\n", err)
110+
}
111+
for _, ownerDir := range ownerDirs {
112+
if !ownerDir.Type().IsDir() {
113+
continue
114+
}
115+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
116+
if err != nil {
117+
fatalTestError("unable to read the new repo root: %v\n", err)
118+
}
119+
for _, repoDir := range repoDirs {
120+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
121+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
122+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
123+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
124+
}
125+
}
126+
107127
exitStatus := m.Run()
108128
if err = util.RemoveAll(repoRootPath); err != nil {
109129
fatalTestError("util.RemoveAll: %v\n", err)
@@ -152,5 +172,22 @@ func PrepareTestEnv(t testing.TB) {
152172
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
153173
metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
154174
assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath))
175+
176+
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
177+
assert.NoError(t, err)
178+
for _, ownerDir := range ownerDirs {
179+
if !ownerDir.Type().IsDir() {
180+
continue
181+
}
182+
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
183+
assert.NoError(t, err)
184+
for _, repoDir := range repoDirs {
185+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0755)
186+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0755)
187+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0755)
188+
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0755)
189+
}
190+
}
191+
155192
base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set
156193
}

models/user/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ type SearchOrganizationsOptions struct {
160160

161161
// ColorFormat writes a colored string to identify this struct
162162
func (u *User) ColorFormat(s fmt.State) {
163+
if u == nil {
164+
log.ColorFprintf(s, "%d:%s",
165+
log.NewColoredIDValue(0),
166+
log.NewColoredValue("<nil>"))
167+
return
168+
}
163169
log.ColorFprintf(s, "%d:%s",
164170
log.NewColoredIDValue(u.ID),
165171
log.NewColoredValue(u.Name))

modules/context/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ func Contexter() func(next http.Handler) http.Handler {
609609
var locale = middleware.Locale(resp, req)
610610
var startTime = time.Now()
611611
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
612+
613+
chiCtx := chi.RouteContext(req.Context())
614+
chiCtx.RoutePath = req.URL.EscapedPath()
615+
612616
var ctx = Context{
613617
Resp: NewResponse(resp),
614618
Cache: mc.GetCache(),

modules/git/batch_reader.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ type WriteCloserError interface {
2727
CloseWithError(err error) error
2828
}
2929

30+
// EnsureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository.
31+
// Run before opening git cat-file.
32+
// This is needed otherwise the git cat-file will hang for invalid repositories.
33+
func EnsureValidGitRepository(ctx context.Context, repoPath string) error {
34+
stderr := strings.Builder{}
35+
err := NewCommandContext(ctx, "rev-parse").
36+
SetDescription(fmt.Sprintf("%s rev-parse [repo_path: %s]", GitExecutable, repoPath)).
37+
RunInDirFullPipeline(repoPath, nil, &stderr, nil)
38+
if err != nil {
39+
return ConcatenateError(err, (&stderr).String())
40+
}
41+
return nil
42+
}
43+
3044
// CatFileBatchCheck opens git cat-file --batch-check in the provided repo and returns a stdin pipe, a stdout reader and cancel function
3145
func CatFileBatchCheck(ctx context.Context, repoPath string) (WriteCloserError, *bufio.Reader, func()) {
3246
batchStdinReader, batchStdinWriter := io.Pipe()

modules/git/repo_base_nogogit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func OpenRepositoryCtx(ctx context.Context, repoPath string) (*Repository, error
5050
return nil, errors.New("no such file or directory")
5151
}
5252

53+
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
54+
if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
55+
return nil, err
56+
}
57+
5358
repo := &Repository{
5459
Path: repoPath,
5560
tagCache: newObjectCache(),

modules/git/repo_commit_nogogit.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
3737
func (repo *Repository) GetRefCommitID(name string) (string, error) {
3838
wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
3939
defer cancel()
40-
_, _ = wr.Write([]byte(name + "\n"))
40+
_, err := wr.Write([]byte(name + "\n"))
41+
if err != nil {
42+
return "", err
43+
}
4144
shaBs, _, _, err := ReadBatchLine(rd)
4245
if IsErrNotExist(err) {
4346
return "", ErrNotExist{name, ""}

modules/indexer/code/bleve.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ func (b *BleveIndexer) Index(repo *repo_model.Repository, sha string, changes *r
275275
batch := gitea_bleve.NewFlushingBatch(b.indexer, maxBatchSize)
276276
if len(changes.Updates) > 0 {
277277

278+
// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
279+
if err := git.EnsureValidGitRepository(git.DefaultContext, repo.RepoPath()); err != nil {
280+
log.Error("Unable to open git repo: %s for %-v: %v", repo.RepoPath(), repo, err)
281+
return err
282+
}
283+
278284
batchWriter, batchReader, cancel := git.CatFileBatch(git.DefaultContext, repo.RepoPath())
279285
defer cancel()
280286

0 commit comments

Comments
 (0)