You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
11 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "bufio"
  7. "bytes"
  8. "fmt"
  9. "io"
  10. "net/http"
  11. "os"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "code.gitea.io/gitea/models"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/private"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/util"
  20. "github.com/urfave/cli"
  21. )
  22. const (
  23. hookBatchSize = 30
  24. )
  25. var (
  26. // CmdHook represents the available hooks sub-command.
  27. CmdHook = cli.Command{
  28. Name: "hook",
  29. Usage: "Delegate commands to corresponding Git hooks",
  30. Description: "This should only be called by Git",
  31. Subcommands: []cli.Command{
  32. subcmdHookPreReceive,
  33. subcmdHookUpdate,
  34. subcmdHookPostReceive,
  35. },
  36. }
  37. subcmdHookPreReceive = cli.Command{
  38. Name: "pre-receive",
  39. Usage: "Delegate pre-receive Git hook",
  40. Description: "This command should only be called by Git",
  41. Action: runHookPreReceive,
  42. Flags: []cli.Flag{
  43. cli.BoolFlag{
  44. Name: "debug",
  45. },
  46. },
  47. }
  48. subcmdHookUpdate = cli.Command{
  49. Name: "update",
  50. Usage: "Delegate update Git hook",
  51. Description: "This command should only be called by Git",
  52. Action: runHookUpdate,
  53. Flags: []cli.Flag{
  54. cli.BoolFlag{
  55. Name: "debug",
  56. },
  57. },
  58. }
  59. subcmdHookPostReceive = cli.Command{
  60. Name: "post-receive",
  61. Usage: "Delegate post-receive Git hook",
  62. Description: "This command should only be called by Git",
  63. Action: runHookPostReceive,
  64. Flags: []cli.Flag{
  65. cli.BoolFlag{
  66. Name: "debug",
  67. },
  68. },
  69. }
  70. )
  71. type delayWriter struct {
  72. internal io.Writer
  73. buf *bytes.Buffer
  74. timer *time.Timer
  75. }
  76. func newDelayWriter(internal io.Writer, delay time.Duration) *delayWriter {
  77. timer := time.NewTimer(delay)
  78. return &delayWriter{
  79. internal: internal,
  80. buf: &bytes.Buffer{},
  81. timer: timer,
  82. }
  83. }
  84. func (d *delayWriter) Write(p []byte) (n int, err error) {
  85. if d.buf != nil {
  86. select {
  87. case <-d.timer.C:
  88. _, err := d.internal.Write(d.buf.Bytes())
  89. if err != nil {
  90. return 0, err
  91. }
  92. d.buf = nil
  93. return d.internal.Write(p)
  94. default:
  95. return d.buf.Write(p)
  96. }
  97. }
  98. return d.internal.Write(p)
  99. }
  100. func (d *delayWriter) WriteString(s string) (n int, err error) {
  101. if d.buf != nil {
  102. select {
  103. case <-d.timer.C:
  104. _, err := d.internal.Write(d.buf.Bytes())
  105. if err != nil {
  106. return 0, err
  107. }
  108. d.buf = nil
  109. return d.internal.Write([]byte(s))
  110. default:
  111. return d.buf.WriteString(s)
  112. }
  113. }
  114. return d.internal.Write([]byte(s))
  115. }
  116. func (d *delayWriter) Close() error {
  117. if d == nil {
  118. return nil
  119. }
  120. stopped := util.StopTimer(d.timer)
  121. if stopped || d.buf == nil {
  122. return nil
  123. }
  124. _, err := d.internal.Write(d.buf.Bytes())
  125. d.buf = nil
  126. return err
  127. }
  128. type nilWriter struct{}
  129. func (n *nilWriter) Write(p []byte) (int, error) {
  130. return len(p), nil
  131. }
  132. func (n *nilWriter) WriteString(s string) (int, error) {
  133. return len(s), nil
  134. }
  135. func runHookPreReceive(c *cli.Context) error {
  136. if os.Getenv(models.EnvIsInternal) == "true" {
  137. return nil
  138. }
  139. setup("hooks/pre-receive.log", c.Bool("debug"))
  140. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  141. if setting.OnlyAllowPushIfGiteaEnvironmentSet {
  142. fail(`Rejecting changes as Gitea environment not set.
  143. If you are pushing over SSH you must push with a key managed by
  144. Gitea or set your environment appropriately.`, "")
  145. } else {
  146. return nil
  147. }
  148. }
  149. // the environment setted on serv command
  150. isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
  151. username := os.Getenv(models.EnvRepoUsername)
  152. reponame := os.Getenv(models.EnvRepoName)
  153. userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
  154. prID, _ := strconv.ParseInt(os.Getenv(models.EnvPRID), 10, 64)
  155. isDeployKey, _ := strconv.ParseBool(os.Getenv(models.EnvIsDeployKey))
  156. hookOptions := private.HookOptions{
  157. UserID: userID,
  158. GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
  159. GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
  160. GitQuarantinePath: os.Getenv(private.GitQuarantinePath),
  161. GitPushOptions: pushOptions(),
  162. ProtectedBranchID: prID,
  163. IsDeployKey: isDeployKey,
  164. }
  165. scanner := bufio.NewScanner(os.Stdin)
  166. oldCommitIDs := make([]string, hookBatchSize)
  167. newCommitIDs := make([]string, hookBatchSize)
  168. refFullNames := make([]string, hookBatchSize)
  169. count := 0
  170. total := 0
  171. lastline := 0
  172. var out io.Writer
  173. out = &nilWriter{}
  174. if setting.Git.VerbosePush {
  175. if setting.Git.VerbosePushDelay > 0 {
  176. dWriter := newDelayWriter(os.Stdout, setting.Git.VerbosePushDelay)
  177. defer dWriter.Close()
  178. out = dWriter
  179. } else {
  180. out = os.Stdout
  181. }
  182. }
  183. for scanner.Scan() {
  184. // TODO: support news feeds for wiki
  185. if isWiki {
  186. continue
  187. }
  188. fields := bytes.Fields(scanner.Bytes())
  189. if len(fields) != 3 {
  190. continue
  191. }
  192. oldCommitID := string(fields[0])
  193. newCommitID := string(fields[1])
  194. refFullName := string(fields[2])
  195. total++
  196. lastline++
  197. // If the ref is a branch, check if it's protected
  198. if strings.HasPrefix(refFullName, git.BranchPrefix) {
  199. oldCommitIDs[count] = oldCommitID
  200. newCommitIDs[count] = newCommitID
  201. refFullNames[count] = refFullName
  202. count++
  203. fmt.Fprintf(out, "*")
  204. if count >= hookBatchSize {
  205. fmt.Fprintf(out, " Checking %d branches\n", count)
  206. hookOptions.OldCommitIDs = oldCommitIDs
  207. hookOptions.NewCommitIDs = newCommitIDs
  208. hookOptions.RefFullNames = refFullNames
  209. statusCode, msg := private.HookPreReceive(username, reponame, hookOptions)
  210. switch statusCode {
  211. case http.StatusOK:
  212. // no-op
  213. case http.StatusInternalServerError:
  214. fail("Internal Server Error", msg)
  215. default:
  216. fail(msg, "")
  217. }
  218. count = 0
  219. lastline = 0
  220. }
  221. } else {
  222. fmt.Fprintf(out, ".")
  223. }
  224. if lastline >= hookBatchSize {
  225. fmt.Fprintf(out, "\n")
  226. lastline = 0
  227. }
  228. }
  229. if count > 0 {
  230. hookOptions.OldCommitIDs = oldCommitIDs[:count]
  231. hookOptions.NewCommitIDs = newCommitIDs[:count]
  232. hookOptions.RefFullNames = refFullNames[:count]
  233. fmt.Fprintf(out, " Checking %d branches\n", count)
  234. statusCode, msg := private.HookPreReceive(username, reponame, hookOptions)
  235. switch statusCode {
  236. case http.StatusInternalServerError:
  237. fail("Internal Server Error", msg)
  238. case http.StatusForbidden:
  239. fail(msg, "")
  240. }
  241. } else if lastline > 0 {
  242. fmt.Fprintf(out, "\n")
  243. lastline = 0
  244. }
  245. fmt.Fprintf(out, "Checked %d references in total\n", total)
  246. return nil
  247. }
  248. func runHookUpdate(c *cli.Context) error {
  249. // Update is empty and is kept only for backwards compatibility
  250. return nil
  251. }
  252. func runHookPostReceive(c *cli.Context) error {
  253. if os.Getenv(models.EnvIsInternal) == "true" {
  254. return nil
  255. }
  256. setup("hooks/post-receive.log", c.Bool("debug"))
  257. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  258. if setting.OnlyAllowPushIfGiteaEnvironmentSet {
  259. fail(`Rejecting changes as Gitea environment not set.
  260. If you are pushing over SSH you must push with a key managed by
  261. Gitea or set your environment appropriately.`, "")
  262. } else {
  263. return nil
  264. }
  265. }
  266. var out io.Writer
  267. var dWriter *delayWriter
  268. out = &nilWriter{}
  269. if setting.Git.VerbosePush {
  270. if setting.Git.VerbosePushDelay > 0 {
  271. dWriter = newDelayWriter(os.Stdout, setting.Git.VerbosePushDelay)
  272. defer dWriter.Close()
  273. out = dWriter
  274. } else {
  275. out = os.Stdout
  276. }
  277. }
  278. // the environment setted on serv command
  279. repoUser := os.Getenv(models.EnvRepoUsername)
  280. isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
  281. repoName := os.Getenv(models.EnvRepoName)
  282. pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
  283. pusherName := os.Getenv(models.EnvPusherName)
  284. hookOptions := private.HookOptions{
  285. UserName: pusherName,
  286. UserID: pusherID,
  287. GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
  288. GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
  289. GitQuarantinePath: os.Getenv(private.GitQuarantinePath),
  290. GitPushOptions: pushOptions(),
  291. }
  292. oldCommitIDs := make([]string, hookBatchSize)
  293. newCommitIDs := make([]string, hookBatchSize)
  294. refFullNames := make([]string, hookBatchSize)
  295. count := 0
  296. total := 0
  297. wasEmpty := false
  298. masterPushed := false
  299. results := make([]private.HookPostReceiveBranchResult, 0)
  300. scanner := bufio.NewScanner(os.Stdin)
  301. for scanner.Scan() {
  302. // TODO: support news feeds for wiki
  303. if isWiki {
  304. continue
  305. }
  306. fields := bytes.Fields(scanner.Bytes())
  307. if len(fields) != 3 {
  308. continue
  309. }
  310. fmt.Fprintf(out, ".")
  311. oldCommitIDs[count] = string(fields[0])
  312. newCommitIDs[count] = string(fields[1])
  313. refFullNames[count] = string(fields[2])
  314. if refFullNames[count] == git.BranchPrefix+"master" && newCommitIDs[count] != git.EmptySHA && count == total {
  315. masterPushed = true
  316. }
  317. count++
  318. total++
  319. if count >= hookBatchSize {
  320. fmt.Fprintf(out, " Processing %d references\n", count)
  321. hookOptions.OldCommitIDs = oldCommitIDs
  322. hookOptions.NewCommitIDs = newCommitIDs
  323. hookOptions.RefFullNames = refFullNames
  324. resp, err := private.HookPostReceive(repoUser, repoName, hookOptions)
  325. if resp == nil {
  326. _ = dWriter.Close()
  327. hookPrintResults(results)
  328. fail("Internal Server Error", err)
  329. }
  330. wasEmpty = wasEmpty || resp.RepoWasEmpty
  331. results = append(results, resp.Results...)
  332. count = 0
  333. }
  334. }
  335. if count == 0 {
  336. if wasEmpty && masterPushed {
  337. // We need to tell the repo to reset the default branch to master
  338. err := private.SetDefaultBranch(repoUser, repoName, "master")
  339. if err != nil {
  340. fail("Internal Server Error", "SetDefaultBranch failed with Error: %v", err)
  341. }
  342. }
  343. fmt.Fprintf(out, "Processed %d references in total\n", total)
  344. _ = dWriter.Close()
  345. hookPrintResults(results)
  346. return nil
  347. }
  348. hookOptions.OldCommitIDs = oldCommitIDs[:count]
  349. hookOptions.NewCommitIDs = newCommitIDs[:count]
  350. hookOptions.RefFullNames = refFullNames[:count]
  351. fmt.Fprintf(out, " Processing %d references\n", count)
  352. resp, err := private.HookPostReceive(repoUser, repoName, hookOptions)
  353. if resp == nil {
  354. _ = dWriter.Close()
  355. hookPrintResults(results)
  356. fail("Internal Server Error", err)
  357. }
  358. wasEmpty = wasEmpty || resp.RepoWasEmpty
  359. results = append(results, resp.Results...)
  360. fmt.Fprintf(out, "Processed %d references in total\n", total)
  361. if wasEmpty && masterPushed {
  362. // We need to tell the repo to reset the default branch to master
  363. err := private.SetDefaultBranch(repoUser, repoName, "master")
  364. if err != nil {
  365. fail("Internal Server Error", "SetDefaultBranch failed with Error: %v", err)
  366. }
  367. }
  368. _ = dWriter.Close()
  369. hookPrintResults(results)
  370. return nil
  371. }
  372. func hookPrintResults(results []private.HookPostReceiveBranchResult) {
  373. for _, res := range results {
  374. if !res.Message {
  375. continue
  376. }
  377. fmt.Fprintln(os.Stderr, "")
  378. if res.Create {
  379. fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res.Branch)
  380. fmt.Fprintf(os.Stderr, " %s\n", res.URL)
  381. } else {
  382. fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
  383. fmt.Fprintf(os.Stderr, " %s\n", res.URL)
  384. }
  385. fmt.Fprintln(os.Stderr, "")
  386. os.Stderr.Sync()
  387. }
  388. }
  389. func pushOptions() map[string]string {
  390. opts := make(map[string]string)
  391. if pushCount, err := strconv.Atoi(os.Getenv(private.GitPushOptionCount)); err == nil {
  392. for idx := 0; idx < pushCount; idx++ {
  393. opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx))
  394. kv := strings.SplitN(opt, "=", 2)
  395. if len(kv) == 2 {
  396. opts[kv[0]] = kv[1]
  397. }
  398. }
  399. }
  400. return opts
  401. }