(c *cli.Context)
| 59 | ) |
| 60 | |
| 61 | func runHookPreReceive(c *cli.Context) error { |
| 62 | if os.Getenv("SSH_ORIGINAL_COMMAND") == "" { |
| 63 | return nil |
| 64 | } |
| 65 | setup(c, "pre-receive.log", true) |
| 66 | |
| 67 | isWiki := strings.Contains(os.Getenv(database.EnvRepoCustomHooksPath), ".wiki.git/") |
| 68 | |
| 69 | buf := bytes.NewBuffer(nil) |
| 70 | scanner := bufio.NewScanner(os.Stdin) |
| 71 | for scanner.Scan() { |
| 72 | buf.Write(scanner.Bytes()) |
| 73 | buf.WriteByte('\n') |
| 74 | |
| 75 | if isWiki { |
| 76 | continue |
| 77 | } |
| 78 | |
| 79 | fields := bytes.Fields(scanner.Bytes()) |
| 80 | if len(fields) != 3 { |
| 81 | continue |
| 82 | } |
| 83 | oldCommitID := string(fields[0]) |
| 84 | newCommitID := string(fields[1]) |
| 85 | branchName := git.RefShortName(string(fields[2])) |
| 86 | |
| 87 | // Branch protection |
| 88 | repoID := com.StrTo(os.Getenv(database.EnvRepoID)).MustInt64() |
| 89 | protectBranch, err := database.GetProtectBranchOfRepoByName(repoID, branchName) |
| 90 | if err != nil { |
| 91 | if database.IsErrBranchNotExist(err) { |
| 92 | continue |
| 93 | } |
| 94 | fail("Internal error", "GetProtectBranchOfRepoByName [repo_id: %d, branch: %s]: %v", repoID, branchName, err) |
| 95 | } |
| 96 | if !protectBranch.Protected { |
| 97 | continue |
| 98 | } |
| 99 | |
| 100 | // Whitelist users can bypass require pull request check |
| 101 | bypassRequirePullRequest := false |
| 102 | |
| 103 | // Check if user is in whitelist when enabled |
| 104 | userID := com.StrTo(os.Getenv(database.EnvAuthUserID)).MustInt64() |
| 105 | if protectBranch.EnableWhitelist { |
| 106 | if !database.IsUserInProtectBranchWhitelist(repoID, userID, branchName) { |
| 107 | fail(fmt.Sprintf("Branch '%s' is protected and you are not in the push whitelist", branchName), "") |
| 108 | } |
| 109 | |
| 110 | bypassRequirePullRequest = true |
| 111 | } |
| 112 | |
| 113 | // Check if branch allows direct push |
| 114 | if !bypassRequirePullRequest && protectBranch.RequirePullRequest { |
| 115 | fail(fmt.Sprintf("Branch '%s' is protected and commits must be merged through pull request", branchName), "") |
| 116 | } |
| 117 | |
| 118 | // check and deletion |
nothing calls this directly
no test coverage detected