]> source.dussan.org Git - gitea.git/commitdiff
fix detect force push failure on deletion of protected branches (#5522) (#5531)
authorLunny Xiao <xiaolunwen@gmail.com>
Wed, 12 Dec 2018 14:49:47 +0000 (22:49 +0800)
committertechknowlogick <hello@techknowlogick.com>
Wed, 12 Dec 2018 14:49:47 +0000 (09:49 -0500)
cmd/hook.go

index 02eb30a13b61aa17d1c30fbb99974b8f5f3b4317..981325285d90e925f34aff14d7efc57b0efea180 100644 (file)
@@ -112,10 +112,15 @@ func runHookPreReceive(c *cli.Context) error {
                branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
                protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
                if err != nil {
-                       log.GitLogger.Fatal(2, "retrieve protected branches information failed")
+                       fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
                }
 
                if protectBranch != nil && protectBranch.IsProtected() {
+                       // check and deletion
+                       if newCommitID == git.EmptySHA {
+                               fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
+                       }
+
                        // detect force push
                        if git.EmptySHA != oldCommitID {
                                output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
@@ -126,17 +131,12 @@ func runHookPreReceive(c *cli.Context) error {
                                }
                        }
 
-                       // check and deletion
-                       if newCommitID == git.EmptySHA {
-                               fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
-                       } else {
-                               userID, _ := strconv.ParseInt(userIDStr, 10, 64)
-                               canPush, err := private.CanUserPush(protectBranch.ID, userID)
-                               if err != nil {
-                                       fail("Internal error", "Fail to detect user can push: %v", err)
-                               } else if !canPush {
-                                       fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
-                               }
+                       userID, _ := strconv.ParseInt(userIDStr, 10, 64)
+                       canPush, err := private.CanUserPush(protectBranch.ID, userID)
+                       if err != nil {
+                               fail("Internal error", "Fail to detect user can push: %v", err)
+                       } else if !canPush {
+                               fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
                        }
                }
        }