change pre-receive hook from flag to sub-command

---
type: change
description: change pre-receive hook from flag to sub-command
fingerprint: AN/k4s8msJaAVVOrm8dQPLpb75lPHy/AFEuyicQfbn1W
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl6l8CgACgkQlcRvpqQRSKxA2A//Q8oJFH10/HYp38bG6OUem7nIg7+J0uC6jCpsP3P66jHVQ2+U0tQ1zdrlZLrufVPPTrYuFnY3m2qezl47IOQnMw5qLN0/+8/tv2rWvG+1Ffz/dGhn215r7D59bgihPiyBSEZW6l2uty/LBWHyHHOn4TgGsi49BH+E/CdTS1Wk+6eWcJHBMOjrzdzTq3Im75HU7GRkvVlyuX69dW1tNlItmcEJ0moRCoc5WHUwn64RRjlxQMkMMMNtWL2MdW9/fbVDNQHtS8L91bsUcyp2uAQW0xqVzezen3+LNxMuxpIjD/FRB3NEnkEHspeorIYCTYWxWZaQnSYB26vri/LUSpKDPH6kQ4yBexZDfwU1xI6qJARNPpczUdn0XycddXAHhtlig9Vbx0x8Y7CiuKejhMtkcNK2Zvtt+AYVoF2gCeRNBXbRXRTt3WBNyvju3NeiZD17cIi6XIOIFkpm4hFGrdMEjfeZFsUln3Jt445t1Whg3DIl6rgcS/uVpz01c6OZvH05o0JncuvlMU5akd0i+0j1L7+zH38vj3NC8rLpy4ECnnH72yAnvR2TSB7KVtjVZB3o8VAiQPFAaDn0A8vkrf63/jcyiAFhL/Ee8u38richLfzghjCJJ6JVg61OevlQ6tH+zuzH2+5TleCpjESNZZRu9OclfEFeB/AO5qYY4I38RM4=
  account: mediocregopher
This commit is contained in:
mediocregopher 2020-04-26 14:33:54 -06:00
parent b01fe1524a
commit 422c444a50
2 changed files with 37 additions and 34 deletions

View File

@ -16,48 +16,51 @@ import (
func cmdHook(ctx context.Context, cmd *dcmd.Cmd) { func cmdHook(ctx context.Context, cmd *dcmd.Cmd) {
flag := cmd.FlagSet() flag := cmd.FlagSet()
preRcv := flag.Bool("pre-receive", false, "Use dehub as a server-side pre-receive hook")
var proj proj var proj proj
proj.initFlags(flag) proj.initFlags(flag)
cmd.Run(func() (context.Context, error) { body := func() (context.Context, error) {
if !*preRcv {
return nil, errors.New("must set the hook type")
}
if err := proj.openProj(); err != nil { if err := proj.openProj(); err != nil {
return nil, err return nil, err
} }
return ctx, nil
}
br := bufio.NewReader(os.Stdin) cmd.SubCmd("pre-receive", "Use dehub as a server-side pre-receive hook",
for { func(ctx context.Context, cmd *dcmd.Cmd) {
line, err := br.ReadString('\n') cmd.Run(func() (context.Context, error) {
if errors.Is(err, io.EOF) { br := bufio.NewReader(os.Stdin)
break for {
} else if err != nil { line, err := br.ReadString('\n')
return nil, fmt.Errorf("error reading next line from stdin: %w", err) if errors.Is(err, io.EOF) {
} break
fmt.Printf("Processing line %q\n", strings.TrimSpace(line)) } else if err != nil {
return nil, fmt.Errorf("error reading next line from stdin: %w", err)
}
fmt.Printf("Processing line %q\n", strings.TrimSpace(line))
lineParts := strings.Fields(line) lineParts := strings.Fields(line)
if len(lineParts) < 3 { if len(lineParts) < 3 {
return nil, fmt.Errorf("malformed pre-receive hook stdin line %q", line) return nil, fmt.Errorf("malformed pre-receive hook stdin line %q", line)
} }
endHash := plumbing.NewHash(lineParts[1]) endHash := plumbing.NewHash(lineParts[1])
branchName := plumbing.ReferenceName(lineParts[2]) branchName := plumbing.ReferenceName(lineParts[2])
if !branchName.IsBranch() { if !branchName.IsBranch() {
return nil, fmt.Errorf("reference %q is not a branch, can't push to it", branchName) return nil, fmt.Errorf("reference %q is not a branch, can't push to it", branchName)
} else if endHash == plumbing.ZeroHash { } else if endHash == plumbing.ZeroHash {
return nil, errors.New("deleting remote branches is not currently supported") return nil, errors.New("deleting remote branches is not currently supported")
} }
return nil, proj.VerifyCanSetBranchHEADTo(branchName, endHash) return nil, proj.VerifyCanSetBranchHEADTo(branchName, endHash)
} }
fmt.Println("All pushed commits have been verified, well done.") fmt.Println("All pushed commits have been verified, well done.")
return nil, nil return nil, nil
}) })
},
)
cmd.Run(body)
} }

View File

@ -194,9 +194,9 @@ func (proj *Project) initRemotePreReceive(bare bool) error {
var preRcvBody string var preRcvBody string
if bare { if bare {
preRcvBody = "#!/bin/sh\nexec dehub hook -bare -pre-receive\n" preRcvBody = "#!/bin/sh\nexec dehub hook -bare pre-receive\n"
} else { } else {
preRcvBody = "#!/bin/sh\nexec dehub hook -pre-receive\n" preRcvBody = "#!/bin/sh\nexec dehub hook pre-receive\n"
} }
if _, err := io.Copy(preRcv, bytes.NewBufferString(preRcvBody)); err != nil { if _, err := io.Copy(preRcv, bytes.NewBufferString(preRcvBody)); err != nil {