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.
63 lines
1.5 KiB
63 lines
1.5 KiB
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"dehub.dev/src/dehub.git/cmd/dehub/dcmd"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
)
|
|
|
|
func cmdHook(ctx context.Context, cmd *dcmd.Cmd) {
|
|
flag := cmd.FlagSet()
|
|
preRcv := flag.Bool("pre-receive", false, "Use dehub as a server-side pre-receive hook")
|
|
|
|
var proj proj
|
|
proj.initFlags(flag)
|
|
|
|
cmd.Run(func() (context.Context, error) {
|
|
if !*preRcv {
|
|
return nil, errors.New("must set the hook type")
|
|
}
|
|
|
|
if err := proj.openProj(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
br := bufio.NewReader(os.Stdin)
|
|
for {
|
|
line, err := br.ReadString('\n')
|
|
if errors.Is(err, io.EOF) {
|
|
break
|
|
} 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)
|
|
if len(lineParts) < 3 {
|
|
return nil, fmt.Errorf("malformed pre-receive hook stdin line %q", line)
|
|
}
|
|
|
|
endHash := plumbing.NewHash(lineParts[1])
|
|
branchName := plumbing.ReferenceName(lineParts[2])
|
|
|
|
if !branchName.IsBranch() {
|
|
return nil, fmt.Errorf("reference %q is not a branch, can't push to it", branchName)
|
|
} else if endHash == plumbing.ZeroHash {
|
|
return nil, errors.New("deleting remote branches is not currently supported")
|
|
}
|
|
|
|
return nil, proj.VerifyCanSetBranchHEADTo(branchName, endHash)
|
|
}
|
|
|
|
fmt.Println("All pushed commits have been verified, well done.")
|
|
return nil, nil
|
|
})
|
|
}
|
|
|