Skip to content

Commit 6b23363

Browse files
author
Gusted
authored
Merge branch 'release/v1.17' into backport-20238
2 parents 88a4a4e + c556a83 commit 6b23363

File tree

27 files changed

+206
-76
lines changed

27 files changed

+206
-76
lines changed

custom/conf/app.example.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,10 @@ ROUTER = console
617617
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618618
;;
619619
;; The path of git executable. If empty, Gitea searches through the PATH environment.
620-
PATH =
620+
;PATH =
621+
;;
622+
;; The HOME directory for Git
623+
;HOME_PATH = %(APP_DATA_PATH)/home
621624
;;
622625
;; Disables highlight of added and removed changes
623626
;DISABLE_DIFF_HIGHLIGHT = false

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,8 @@ Default templates for project boards:
947947
## Git (`git`)
948948

949949
- `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment.
950+
- `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git.
951+
This directory will be used to contain the `.gitconfig` and possible `.gnupg` directories that Gitea's git calls will use. If you can confirm Gitea is the only application running in this environment, you can set it to the normal home directory for Gitea user.
950952
- `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes.
951953
- `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view.
952954
- `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view.

docs/content/doc/advanced/signing.en-us.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ repositories, `SIGNING_KEY=default` could be used to provide different
9797
signing keys on a per-repository basis. However, this is clearly not an
9898
ideal UI and therefore subject to change.
9999

100-
**Since 1.17**, Gitea runs git in its own home directory `[repository].ROOT` and uses its own config `{[repository].ROOT}/.gitconfig`.
100+
**Since 1.17**, Gitea runs git in its own home directory `[git].HOME_PATH` (default to `%(APP_DATA_PATH)/home`)
101+
and uses its own config `{[git].HOME_PATH}/.gitconfig`.
101102
If you have your own customized git config for Gitea, you should set these configs in system git config (aka `/etc/gitconfig`)
102-
or the Gitea internal git config `{[repository].ROOT}/.gitconfig`.
103-
Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[repository].ROOT`.
103+
or the Gitea internal git config `{[git].HOME_PATH}/.gitconfig`.
104+
Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[git].HOME_PATH`.
104105

105106

106107
### `INITIAL_COMMIT`

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ require (
8585
github.com/urfave/cli v1.22.9
8686
github.com/xanzy/go-gitlab v0.64.0
8787
github.com/yohcop/openid-go v1.0.0
88-
github.com/yuin/goldmark v1.4.12
88+
github.com/yuin/goldmark v1.4.13
8989
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
9090
github.com/yuin/goldmark-meta v1.1.0
9191
go.jolheiser.com/hcaptcha v0.0.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,8 +1540,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
15401540
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
15411541
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
15421542
github.com/yuin/goldmark v1.4.5/go.mod h1:rmuwmfZ0+bvzB24eSC//bk1R1Zp3hM0OXYv/G2LIilg=
1543-
github.com/yuin/goldmark v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0=
1544-
github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1543+
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
1544+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
15451545
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594 h1:yHfZyN55+5dp1wG7wDKv8HQ044moxkyGq12KFFMFDxg=
15461546
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594/go.mod h1:U9ihbh+1ZN7fR5Se3daSPoz1CGF9IYtSvWwVQtnzGHU=
15471547
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=

models/perm/access/access.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ func updateUserAccess(accessMap map[int64]*userAccess, user *user_model.User, mo
8686
// FIXME: do cross-comparison so reduce deletions and additions to the minimum?
8787
func refreshAccesses(ctx context.Context, repo *repo_model.Repository, accessMap map[int64]*userAccess) (err error) {
8888
minMode := perm.AccessModeRead
89-
if !repo.IsPrivate {
89+
if err := repo.GetOwner(ctx); err != nil {
90+
return fmt.Errorf("GetOwner: %v", err)
91+
}
92+
93+
// If the repo isn't private and isn't owned by a organization,
94+
// increase the minMode to Write.
95+
if !repo.IsPrivate && !repo.Owner.IsOrganization() {
9096
minMode = perm.AccessModeWrite
9197
}
9298

models/unittest/testdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) {
107107

108108
setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
109109

110+
setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home")
111+
110112
if err = storage.Init(); err != nil {
111113
fatalTestError("storage.Init: %v\n", err)
112114
}

models/user/user.go

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,37 +316,45 @@ func (u *User) GenerateEmailActivateCode(email string) string {
316316
}
317317

318318
// GetUserFollowers returns range of user's followers.
319-
func GetUserFollowers(u *User, listOptions db.ListOptions) ([]*User, error) {
320-
sess := db.GetEngine(db.DefaultContext).
319+
func GetUserFollowers(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
320+
sess := db.GetEngine(ctx).
321+
Select("`user`.*").
322+
Join("LEFT", "follow", "`user`.id=follow.user_id").
321323
Where("follow.follow_id=?", u.ID).
322-
Join("LEFT", "follow", "`user`.id=follow.user_id")
324+
And(isUserVisibleToViewerCond(viewer))
323325

324326
if listOptions.Page != 0 {
325327
sess = db.SetSessionPagination(sess, &listOptions)
326328

327329
users := make([]*User, 0, listOptions.PageSize)
328-
return users, sess.Find(&users)
330+
count, err := sess.FindAndCount(&users)
331+
return users, count, err
329332
}
330333

331334
users := make([]*User, 0, 8)
332-
return users, sess.Find(&users)
335+
count, err := sess.FindAndCount(&users)
336+
return users, count, err
333337
}
334338

335339
// GetUserFollowing returns range of user's following.
336-
func GetUserFollowing(u *User, listOptions db.ListOptions) ([]*User, error) {
340+
func GetUserFollowing(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
337341
sess := db.GetEngine(db.DefaultContext).
342+
Select("`user`.*").
343+
Join("LEFT", "follow", "`user`.id=follow.follow_id").
338344
Where("follow.user_id=?", u.ID).
339-
Join("LEFT", "follow", "`user`.id=follow.follow_id")
345+
And(isUserVisibleToViewerCond(viewer))
340346

341347
if listOptions.Page != 0 {
342348
sess = db.SetSessionPagination(sess, &listOptions)
343349

344350
users := make([]*User, 0, listOptions.PageSize)
345-
return users, sess.Find(&users)
351+
count, err := sess.FindAndCount(&users)
352+
return users, count, err
346353
}
347354

348355
users := make([]*User, 0, 8)
349-
return users, sess.Find(&users)
356+
count, err := sess.FindAndCount(&users)
357+
return users, count, err
350358
}
351359

352360
// NewGitSig generates and returns the signature of given user.
@@ -485,6 +493,9 @@ func (u *User) GitName() string {
485493

486494
// ShortName ellipses username to length
487495
func (u *User) ShortName(length int) string {
496+
if setting.UI.DefaultShowFullName && len(u.FullName) > 0 {
497+
return base.EllipsisString(u.FullName, length)
498+
}
488499
return base.EllipsisString(u.Name, length)
489500
}
490501

@@ -1219,6 +1230,39 @@ func GetAdminUser() (*User, error) {
12191230
return &admin, nil
12201231
}
12211232

1233+
func isUserVisibleToViewerCond(viewer *User) builder.Cond {
1234+
if viewer != nil && viewer.IsAdmin {
1235+
return builder.NewCond()
1236+
}
1237+
1238+
if viewer == nil || viewer.IsRestricted {
1239+
return builder.Eq{
1240+
"`user`.visibility": structs.VisibleTypePublic,
1241+
}
1242+
}
1243+
1244+
return builder.Neq{
1245+
"`user`.visibility": structs.VisibleTypePrivate,
1246+
}.Or(
1247+
builder.In("`user`.id",
1248+
builder.
1249+
Select("`follow`.user_id").
1250+
From("follow").
1251+
Where(builder.Eq{"`follow`.follow_id": viewer.ID})),
1252+
builder.In("`user`.id",
1253+
builder.
1254+
Select("`team_user`.uid").
1255+
From("team_user").
1256+
Join("INNER", "`team_user` AS t2", "`team_user`.id = `t2`.id").
1257+
Where(builder.Eq{"`t2`.uid": viewer.ID})),
1258+
builder.In("`user`.id",
1259+
builder.
1260+
Select("`team_user`.uid").
1261+
From("team_user").
1262+
Join("INNER", "`team_user` AS t2", "`team_user`.org_id = `t2`.org_id").
1263+
Where(builder.Eq{"`t2`.uid": viewer.ID})))
1264+
}
1265+
12221266
// IsUserVisibleToViewer check if viewer is able to see user profile
12231267
func IsUserVisibleToViewer(ctx context.Context, u, viewer *User) bool {
12241268
if viewer != nil && viewer.IsAdmin {

modules/git/command.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,36 @@ type RunOpts struct {
105105
PipelineFunc func(context.Context, context.CancelFunc) error
106106
}
107107

108-
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
109-
func CommonGitCmdEnvs() []string {
108+
func commonBaseEnvs() []string {
110109
// at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it
111-
return []string{
112-
fmt.Sprintf("LC_ALL=%s", DefaultLocale),
113-
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
114-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
110+
envs := []string{
115111
"HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config
112+
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
113+
}
114+
115+
// some environment variables should be passed to git command
116+
passThroughEnvKeys := []string{
117+
"GNUPGHOME", // git may call gnupg to do commit signing
118+
}
119+
for _, key := range passThroughEnvKeys {
120+
if val, ok := os.LookupEnv(key); ok {
121+
envs = append(envs, key+"="+val)
122+
}
116123
}
124+
return envs
125+
}
126+
127+
// CommonGitCmdEnvs returns the common environment variables for a "git" command.
128+
func CommonGitCmdEnvs() []string {
129+
return append(commonBaseEnvs(), []string{
130+
"LC_ALL=" + DefaultLocale,
131+
"GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3
132+
}...)
117133
}
118134

119135
// CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command
120136
func CommonCmdServEnvs() []string {
121-
return []string{
122-
"GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace)
123-
"HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config
124-
}
137+
return commonBaseEnvs()
125138
}
126139

127140
// Run runs the command with the RunOpts

modules/git/git.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"regexp"
1516
"runtime"
1617
"strings"
@@ -19,7 +20,6 @@ import (
1920

2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/setting"
22-
2323
"github.com/hashicorp/go-version"
2424
)
2525

@@ -126,8 +126,8 @@ func VersionInfo() string {
126126
}
127127

128128
func checkInit() error {
129-
if setting.RepoRootPath == "" {
130-
return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
129+
if setting.Git.HomePath == "" {
130+
return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
131131
}
132132
if DefaultContext != nil {
133133
log.Warn("git module has been initialized already, duplicate init should be fixed")
@@ -137,14 +137,14 @@ func checkInit() error {
137137

138138
// HomeDir is the home dir for git to store the global config file used by Gitea internally
139139
func HomeDir() string {
140-
if setting.RepoRootPath == "" {
140+
if setting.Git.HomePath == "" {
141141
// strict check, make sure the git module is initialized correctly.
142142
// attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users.
143143
// for example: if there is gitea git hook code calling git.NewCommand before git.InitXxx, the integration test won't show the real failure reasons.
144-
log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly")
144+
log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules")
145145
return ""
146146
}
147-
return setting.RepoRootPath
147+
return setting.Git.HomePath
148148
}
149149

150150
// InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
@@ -175,11 +175,15 @@ func InitOnceWithSync(ctx context.Context) (err error) {
175175
}
176176

177177
initOnce.Do(func() {
178-
err = InitSimple(ctx)
179-
if err != nil {
178+
if err = InitSimple(ctx); err != nil {
180179
return
181180
}
182181

182+
// when git works with gnupg (commit signing), there should be a stable home for gnupg commands
183+
if _, ok := os.LookupEnv("GNUPGHOME"); !ok {
184+
_ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg"))
185+
}
186+
183187
// Since git wire protocol has been released from git v2.18
184188
if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil {
185189
globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2")
@@ -206,7 +210,7 @@ func InitOnceWithSync(ctx context.Context) (err error) {
206210
// syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem)
207211
func syncGitConfig() (err error) {
208212
if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil {
209-
return fmt.Errorf("unable to create directory %s, err: %w", setting.RepoRootPath, err)
213+
return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err)
210214
}
211215

212216
// Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults"

modules/git/git_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
func testRun(m *testing.M) error {
2222
_ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`)
2323

24-
repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
24+
gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home")
2525
if err != nil {
2626
return fmt.Errorf("unable to create temp dir: %w", err)
2727
}
28-
defer util.RemoveAll(repoRootPath)
29-
setting.RepoRootPath = repoRootPath
28+
defer util.RemoveAll(gitHomePath)
29+
setting.Git.HomePath = gitHomePath
3030

3131
if err = InitOnceWithSync(context.Background()); err != nil {
3232
return fmt.Errorf("failed to call Init: %w", err)

modules/markup/html.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,10 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
841841

842842
// Repos with external issue trackers might still need to reference local PRs
843843
// We need to concern with the first one that shows up in the text, whichever it is
844-
if hasExtTrackFormat && !isNumericStyle {
844+
if hasExtTrackFormat && !isNumericStyle && refNumeric != nil {
845845
// If numeric (PR) was found, and it was BEFORE the non-numeric pattern, use that
846-
if foundNumeric && refNumeric.RefLocation.Start < ref.RefLocation.Start {
846+
// Allow a free-pass when non-numeric pattern wasn't found.
847+
if found && (ref == nil || refNumeric.RefLocation.Start < ref.RefLocation.Start) {
847848
found = foundNumeric
848849
ref = refNumeric
849850
}

modules/setting/git.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package setting
66

77
import (
8+
"path/filepath"
89
"time"
910

1011
"code.gitea.io/gitea/modules/log"
@@ -13,6 +14,7 @@ import (
1314
// Git settings
1415
var Git = struct {
1516
Path string
17+
HomePath string
1618
DisableDiffHighlight bool
1719
MaxGitDiffLines int
1820
MaxGitDiffLineCharacters int
@@ -67,7 +69,16 @@ var Git = struct {
6769
}
6870

6971
func newGit() {
70-
if err := Cfg.Section("git").MapTo(&Git); err != nil {
72+
sec := Cfg.Section("git")
73+
74+
if err := sec.MapTo(&Git); err != nil {
7175
log.Fatal("Failed to map Git settings: %v", err)
7276
}
77+
78+
Git.HomePath = sec.Key("HOME_PATH").MustString("home")
79+
if !filepath.IsAbs(Git.HomePath) {
80+
Git.HomePath = filepath.Join(AppDataPath, Git.HomePath)
81+
} else {
82+
Git.HomePath = filepath.Clean(Git.HomePath)
83+
}
7384
}

modules/setting/setting.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,9 +862,7 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
862862
SSH.AuthorizedPrincipalsAllow, SSH.AuthorizedPrincipalsEnabled = parseAuthorizedPrincipalsAllow(sec.Key("SSH_AUTHORIZED_PRINCIPALS_ALLOW").Strings(","))
863863

864864
if !SSH.Disabled && !SSH.StartBuiltinServer {
865-
if err := os.MkdirAll(SSH.RootPath, 0o700); err != nil {
866-
log.Fatal("Failed to create '%s': %v", SSH.RootPath, err)
867-
} else if err = os.MkdirAll(SSH.KeyTestPath, 0o644); err != nil {
865+
if err = os.MkdirAll(SSH.KeyTestPath, 0o644); err != nil {
868866
log.Fatal("Failed to create '%s': %v", SSH.KeyTestPath, err)
869867
}
870868

0 commit comments

Comments
 (0)