MCPcopy Create free account
hub / github.com/gogs/gogs / runHookPreReceive

Function runHookPreReceive

internal/cmd/hook.go:61–152  ·  view source on GitHub ↗
(c *cli.Context)

Source from the content-addressed store, hash-verified

59)
60
61func 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

Callers

nothing calls this directly

Calls 10

IsErrBranchNotExistFunction · 0.92
RepoPathFunction · 0.92
IsWindowsRuntimeFunction · 0.92
setupFunction · 0.85
stringFunction · 0.85
WriteMethod · 0.80
BytesMethod · 0.80
failFunction · 0.70

Tested by

no test coverage detected