Skip to content

Commit cfdc62e

Browse files
authored
Comment force push detect to fix bug #1073 (#1077)
* umcomment force push detect to fix bug #1073 * fix #1086 * handle global config set and fix #1086
1 parent 9cb08a3 commit cfdc62e

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

cmd/hook.go

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,22 @@ func runHookPreReceive(c *cli.Context) error {
6969
return nil
7070
}
7171

72+
if c.IsSet("config") {
73+
setting.CustomConf = c.String("config")
74+
} else if c.GlobalIsSet("config") {
75+
setting.CustomConf = c.GlobalString("config")
76+
}
77+
7278
if err := setup("hooks/pre-receive.log"); err != nil {
7379
fail("Hook pre-receive init failed", fmt.Sprintf("setup: %v", err))
7480
}
7581

7682
// the environment setted on serv command
7783
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
7884
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
79-
username := os.Getenv(models.EnvRepoUsername)
80-
reponame := os.Getenv(models.EnvRepoName)
81-
repoPath := models.RepoPath(username, reponame)
85+
//username := os.Getenv(models.EnvRepoUsername)
86+
//reponame := os.Getenv(models.EnvRepoName)
87+
//repoPath := models.RepoPath(username, reponame)
8288

8389
buf := bytes.NewBuffer(nil)
8490
scanner := bufio.NewScanner(os.Stdin)
@@ -96,31 +102,36 @@ func runHookPreReceive(c *cli.Context) error {
96102
continue
97103
}
98104

99-
oldCommitID := string(fields[0])
105+
//oldCommitID := string(fields[0])
100106
newCommitID := string(fields[1])
101107
refFullName := string(fields[2])
102108

109+
// FIXME: when we add feature to protected branch to deny force push, then uncomment below
110+
/*var isForce bool
111+
// detect force push
112+
if git.EmptySHA != oldCommitID {
113+
output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
114+
if err != nil {
115+
fail("Internal error", "Fail to detect force push: %v", err)
116+
} else if len(output) > 0 {
117+
isForce = true
118+
}
119+
}*/
120+
103121
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
104122
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
105123
if err != nil {
106124
log.GitLogger.Fatal(2, "retrieve protected branches information failed")
107125
}
108126

109127
if protectBranch != nil {
110-
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
111-
}
112-
113-
// check and deletion
114-
if newCommitID == git.EmptySHA {
115-
fail(fmt.Sprintf("Branch '%s' is protected from deletion", branchName), "")
116-
}
117-
118-
// Check force push
119-
output, err := git.NewCommand("rev-list", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
120-
if err != nil {
121-
fail("Internal error", "Fail to detect force push: %v", err)
122-
} else if len(output) > 0 {
123-
fail(fmt.Sprintf("Branch '%s' is protected from force push", branchName), "")
128+
// check and deletion
129+
if newCommitID == git.EmptySHA {
130+
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
131+
} else {
132+
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
133+
//fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
134+
}
124135
}
125136
}
126137

@@ -132,6 +143,12 @@ func runHookUpdate(c *cli.Context) error {
132143
return nil
133144
}
134145

146+
if c.IsSet("config") {
147+
setting.CustomConf = c.String("config")
148+
} else if c.GlobalIsSet("config") {
149+
setting.CustomConf = c.GlobalString("config")
150+
}
151+
135152
if err := setup("hooks/update.log"); err != nil {
136153
fail("Hook update init failed", fmt.Sprintf("setup: %v", err))
137154
}
@@ -144,6 +161,12 @@ func runHookPostReceive(c *cli.Context) error {
144161
return nil
145162
}
146163

164+
if c.IsSet("config") {
165+
setting.CustomConf = c.String("config")
166+
} else if c.GlobalIsSet("config") {
167+
setting.CustomConf = c.GlobalString("config")
168+
}
169+
147170
if err := setup("hooks/post-receive.log"); err != nil {
148171
fail("Hook post-receive init failed", fmt.Sprintf("setup: %v", err))
149172
}

0 commit comments

Comments
 (0)