Skip to content

Commit 65e882a

Browse files
authored
Merge branch 'master' into delete_releases_attachments_if_release_is_deleted
2 parents 7b8c816 + 710245e commit 65e882a

File tree

17 files changed

+117
-96
lines changed

17 files changed

+117
-96
lines changed

cmd/hook.go

+43-34
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,37 @@ func runHookPreReceive(c *cli.Context) error {
8989
newCommitID := string(fields[1])
9090
refFullName := string(fields[2])
9191

92-
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
93-
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
94-
if err != nil {
95-
fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
96-
}
97-
98-
if protectBranch != nil && protectBranch.IsProtected() {
99-
// check and deletion
100-
if newCommitID == git.EmptySHA {
101-
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
92+
// If the ref is a branch, check if it's protected
93+
if strings.HasPrefix(refFullName, git.BranchPrefix) {
94+
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
95+
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
96+
if err != nil {
97+
fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
10298
}
10399

104-
// detect force push
105-
if git.EmptySHA != oldCommitID {
106-
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
107-
if err != nil {
108-
fail("Internal error", "Fail to detect force push: %v", err)
109-
} else if len(output) > 0 {
110-
fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
100+
if protectBranch != nil && protectBranch.IsProtected() {
101+
// check and deletion
102+
if newCommitID == git.EmptySHA {
103+
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
111104
}
112-
}
113105

114-
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
115-
canPush, err := private.CanUserPush(protectBranch.ID, userID)
116-
if err != nil {
117-
fail("Internal error", "Fail to detect user can push: %v", err)
118-
} else if !canPush {
119-
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
106+
// detect force push
107+
if git.EmptySHA != oldCommitID {
108+
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
109+
if err != nil {
110+
fail("Internal error", "Fail to detect force push: %v", err)
111+
} else if len(output) > 0 {
112+
fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
113+
}
114+
}
115+
116+
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
117+
canPush, err := private.CanUserPush(protectBranch.ID, userID)
118+
if err != nil {
119+
fail("Internal error", "Fail to detect user can push: %v", err)
120+
} else if !canPush {
121+
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
122+
}
120123
}
121124
}
122125
}
@@ -169,16 +172,22 @@ func runHookPostReceive(c *cli.Context) error {
169172
newCommitID := string(fields[1])
170173
refFullName := string(fields[2])
171174

172-
if err := private.PushUpdate(models.PushUpdateOptions{
173-
RefFullName: refFullName,
174-
OldCommitID: oldCommitID,
175-
NewCommitID: newCommitID,
176-
PusherID: pusherID,
177-
PusherName: pusherName,
178-
RepoUserName: repoUser,
179-
RepoName: repoName,
180-
}); err != nil {
181-
log.GitLogger.Error("Update: %v", err)
175+
// Only trigger activity updates for changes to branches or
176+
// tags. Updates to other refs (eg, refs/notes, refs/changes,
177+
// or other less-standard refs spaces are ignored since there
178+
// may be a very large number of them).
179+
if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
180+
if err := private.PushUpdate(models.PushUpdateOptions{
181+
RefFullName: refFullName,
182+
OldCommitID: oldCommitID,
183+
NewCommitID: newCommitID,
184+
PusherID: pusherID,
185+
PusherName: pusherName,
186+
RepoUserName: repoUser,
187+
RepoName: repoName,
188+
}); err != nil {
189+
log.GitLogger.Error("Update: %v", err)
190+
}
182191
}
183192

184193
if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {

contrib/pr/checkout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func runPR() {
4343
if err != nil {
4444
log.Fatal(err)
4545
}
46-
setting.SetCustomPathAndConf("", "")
46+
setting.SetCustomPathAndConf("", "", "")
4747
setting.NewContext()
4848

4949
setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")

docs/content/doc/usage/command-line.en-us.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ All global options can be placed at the command level.
2525

2626
- `--help`, `-h`: Show help text and exit. Optional.
2727
- `--version`, `-v`: Show version and exit. Optional. (example: `Gitea version 1.1.0+218-g7b907ed built with: bindata, sqlite`).
28-
- `--custom-path path`, `-C path`: Location of the Gitea custom folder. Optional. (default: $PWD/custom).
29-
- `--config path`, `-c path`: Gitea configuration file path. Optional. (default: custom/conf/app.ini).
28+
- `--custom-path path`, `-C path`: Location of the Gitea custom folder. Optional. (default: `AppWorkPath`/custom or `$GITEA_CUSTOM`).
29+
- `--config path`, `-c path`: Gitea configuration file path. Optional. (default: `custom`/conf/app.ini).
30+
- `--work-path path`, `-w path`: Gitea `AppWorkPath`. Optional. (default: LOCATION_OF_GITEA_BINARY or `$GITEA_WORK_DIR`)
31+
32+
NB: The defaults custom-path, config and work-path can also be
33+
changed at build time (if preferred).
3034

3135
### Commands
3236

integrations/integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func initIntegrationTest() {
118118
setting.CustomConf = giteaConf
119119
}
120120

121-
setting.SetCustomPathAndConf("", "")
121+
setting.SetCustomPathAndConf("", "", "")
122122
setting.NewContext()
123123
setting.CheckLFSVersion()
124124
models.LoadConfigs()

main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
6868
// Now adjust these commands to add our global configuration options
6969

7070
// First calculate the default paths and set the AppHelpTemplates in this context
71-
setting.SetCustomPathAndConf("", "")
71+
setting.SetCustomPathAndConf("", "", "")
7272
setAppHelpTemplates()
7373

7474
// default configuration flags
@@ -84,6 +84,11 @@ arguments - which can alternatively be run by running the subcommand web.`
8484
Usage: "Custom configuration file path",
8585
},
8686
cli.VersionFlag,
87+
cli.StringFlag{
88+
Name: "work-path, w",
89+
Value: setting.AppWorkPath,
90+
Usage: "Set the gitea working path",
91+
},
8792
}
8893

8994
// Set the default to be equivalent to cmdWeb and add the default flags
@@ -114,10 +119,11 @@ func setFlagsAndBeforeOnSubcommands(command *cli.Command, defaultFlags []cli.Fla
114119
func establishCustomPath(ctx *cli.Context) error {
115120
var providedCustom string
116121
var providedConf string
122+
var providedWorkPath string
117123

118124
currentCtx := ctx
119125
for {
120-
if len(providedCustom) != 0 && len(providedConf) != 0 {
126+
if len(providedCustom) != 0 && len(providedConf) != 0 && len(providedWorkPath) != 0 {
121127
break
122128
}
123129
if currentCtx == nil {
@@ -129,10 +135,13 @@ func establishCustomPath(ctx *cli.Context) error {
129135
if currentCtx.IsSet("config") && len(providedConf) == 0 {
130136
providedConf = currentCtx.String("config")
131137
}
138+
if currentCtx.IsSet("work-path") && len(providedWorkPath) == 0 {
139+
providedWorkPath = currentCtx.String("work-path")
140+
}
132141
currentCtx = currentCtx.Parent()
133142

134143
}
135-
setting.SetCustomPathAndConf(providedCustom, providedConf)
144+
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
136145

137146
setAppHelpTemplates()
138147

models/login_source.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"fmt"
1212
"net/smtp"
1313
"net/textproto"
14+
"regexp"
1415
"strings"
1516

1617
"github.com/Unknwon/com"
17-
"github.com/go-macaron/binding"
1818
"github.com/go-xorm/core"
1919
"github.com/go-xorm/xorm"
2020

@@ -384,6 +384,10 @@ func composeFullName(firstname, surname, username string) string {
384384
}
385385
}
386386

387+
var (
388+
alphaDashDotPattern = regexp.MustCompile("[^\\w-\\.]")
389+
)
390+
387391
// LoginViaLDAP queries if login/password is valid against the LDAP directory pool,
388392
// and create a local user if success when enabled.
389393
func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoRegister bool) (*User, error) {
@@ -408,7 +412,7 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoR
408412
sr.Username = login
409413
}
410414
// Validate username make sure it satisfies requirement.
411-
if binding.AlphaDashDotPattern.MatchString(sr.Username) {
415+
if alphaDashDotPattern.MatchString(sr.Username) {
412416
return nil, fmt.Errorf("Invalid pattern for attribute 'username' [%s]: must be valid alpha or numeric or dash(-_) or dot characters", sr.Username)
413417
}
414418

models/mail.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const (
3333

3434
var templates *template.Template
3535

36-
// InitMailRender initializes the macaron mail renderer
36+
// InitMailRender initializes the mail renderer
3737
func InitMailRender(tmpls *template.Template) {
3838
templates = tmpls
3939
}

models/repo.go

+3-44
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"io/ioutil"
1414
"net/url"
1515
"os"
16-
"os/exec"
1716
"path"
1817
"path/filepath"
1918
"regexp"
@@ -32,11 +31,9 @@ import (
3231
"code.gitea.io/gitea/modules/sync"
3332
"code.gitea.io/gitea/modules/util"
3433

35-
"github.com/Unknwon/cae/zip"
3634
"github.com/Unknwon/com"
3735
"github.com/go-xorm/builder"
3836
"github.com/go-xorm/xorm"
39-
version "github.com/mcuadros/go-version"
4037
ini "gopkg.in/ini.v1"
4138
)
4239

@@ -67,8 +64,8 @@ var (
6764
ItemsPerPage = 40
6865
)
6966

70-
// LoadRepoConfig loads the repository config
71-
func LoadRepoConfig() {
67+
// loadRepoConfig loads the repository config
68+
func loadRepoConfig() {
7269
// Load .gitignore and license files and readme templates.
7370
types := []string{"gitignore", "license", "readme", "label"}
7471
typeFiles := make([][]string, 4)
@@ -119,45 +116,7 @@ func LoadRepoConfig() {
119116

120117
// NewRepoContext creates a new repository context
121118
func NewRepoContext() {
122-
zip.Verbose = false
123-
124-
// Check Git installation.
125-
if _, err := exec.LookPath("git"); err != nil {
126-
log.Fatal("Failed to test 'git' command: %v (forgotten install?)", err)
127-
}
128-
129-
// Check Git version.
130-
var err error
131-
setting.Git.Version, err = git.BinVersion()
132-
if err != nil {
133-
log.Fatal("Failed to get Git version: %v", err)
134-
}
135-
136-
log.Info("Git Version: %s", setting.Git.Version)
137-
if version.Compare("1.7.1", setting.Git.Version, ">") {
138-
log.Fatal("Gitea requires Git version greater or equal to 1.7.1")
139-
}
140-
141-
// Git requires setting user.name and user.email in order to commit changes.
142-
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
143-
if stdout, stderr, err := process.GetManager().Exec("NewRepoContext(get setting)", "git", "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
144-
// ExitError indicates this config is not set
145-
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
146-
if _, stderr, gerr := process.GetManager().Exec("NewRepoContext(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
147-
log.Fatal("Failed to set git %s(%s): %s", configKey, gerr, stderr)
148-
}
149-
log.Info("Git config %s set to %s", configKey, defaultValue)
150-
} else {
151-
log.Fatal("Failed to get git %s(%s): %s", configKey, err, stderr)
152-
}
153-
}
154-
}
155-
156-
// Set git some configurations.
157-
if _, stderr, err := process.GetManager().Exec("NewRepoContext(git config --global core.quotepath false)",
158-
"git", "config", "--global", "core.quotepath", "false"); err != nil {
159-
log.Fatal("Failed to execute 'git config --global core.quotepath false': %s", stderr)
160-
}
119+
loadRepoConfig()
161120

162121
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
163122
}

models/ssh_key_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func init() {
17-
setting.SetCustomPathAndConf("", "")
17+
setting.SetCustomPathAndConf("", "", "")
1818
setting.NewContext()
1919
}
2020

modules/git/git.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14+
"code.gitea.io/gitea/modules/process"
15+
1416
"github.com/mcuadros/go-version"
1517
)
1618

@@ -31,6 +33,8 @@ var (
3133
// GitExecutable is the command name of git
3234
// Could be updated to an absolute path while initialization
3335
GitExecutable = "git"
36+
37+
gitVersion string
3438
)
3539

3640
func log(format string, args ...interface{}) {
@@ -46,8 +50,6 @@ func log(format string, args ...interface{}) {
4650
}
4751
}
4852

49-
var gitVersion string
50-
5153
// BinVersion returns current Git version from shell.
5254
func BinVersion() (string, error) {
5355
if len(gitVersion) > 0 {
@@ -89,6 +91,26 @@ func init() {
8991
if version.Compare(gitVersion, GitVersionRequired, "<") {
9092
panic(fmt.Sprintf("Git version not supported. Requires version > %v", GitVersionRequired))
9193
}
94+
95+
// Git requires setting user.name and user.email in order to commit changes.
96+
for configKey, defaultValue := range map[string]string{"user.name": "Gitea", "user.email": "[email protected]"} {
97+
if stdout, stderr, err := process.GetManager().Exec("git.Init(get setting)", GitExecutable, "config", "--get", configKey); err != nil || strings.TrimSpace(stdout) == "" {
98+
// ExitError indicates this config is not set
99+
if _, ok := err.(*exec.ExitError); ok || strings.TrimSpace(stdout) == "" {
100+
if _, stderr, gerr := process.GetManager().Exec("git.Init(set "+configKey+")", "git", "config", "--global", configKey, defaultValue); gerr != nil {
101+
panic(fmt.Sprintf("Failed to set git %s(%s): %s", configKey, gerr, stderr))
102+
}
103+
} else {
104+
panic(fmt.Sprintf("Failed to get git %s(%s): %s", configKey, err, stderr))
105+
}
106+
}
107+
}
108+
109+
// Set git some configurations.
110+
if _, stderr, err := process.GetManager().Exec("git.Init(git config --global core.quotepath false)",
111+
GitExecutable, "config", "--global", "core.quotepath", "false"); err != nil {
112+
panic(fmt.Sprintf("Failed to execute 'git config --global core.quotepath false': %s", stderr))
113+
}
92114
}
93115

94116
// Fsck verifies the connectivity and validity of the objects in the database

modules/setting/git.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
var (
1717
// Git settings
1818
Git = struct {
19-
Version string `ini:"-"`
2019
DisableDiffHighlight bool
2120
MaxGitDiffLines int
2221
MaxGitDiffLineCharacters int
@@ -65,6 +64,8 @@ func newGit() {
6564
log.Fatal("Error retrieving git version: %v", err)
6665
}
6766

67+
log.Info("Git Version: %s", binVersion)
68+
6869
if version.Compare(binVersion, "2.9", ">=") {
6970
// Explicitly disable credential helper, otherwise Git credentials might leak
7071
git.GlobalCommandArgs = append(git.GlobalCommandArgs, "-c", "credential.helper=")

0 commit comments

Comments
 (0)