|
|
@ -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) |
|
|
|
lineParts := strings.Fields(line) |
|
|
|
} |
|
|
|
if len(lineParts) < 3 { |
|
|
|
fmt.Printf("Processing line %q\n", strings.TrimSpace(line)) |
|
|
|
return nil, fmt.Errorf("malformed pre-receive hook stdin line %q", line) |
|
|
|
|
|
|
|
} |
|
|
|
lineParts := strings.Fields(line) |
|
|
|
|
|
|
|
if len(lineParts) < 3 { |
|
|
|
endHash := plumbing.NewHash(lineParts[1]) |
|
|
|
return nil, fmt.Errorf("malformed pre-receive hook stdin line %q", line) |
|
|
|
branchName := plumbing.ReferenceName(lineParts[2]) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !branchName.IsBranch() { |
|
|
|
endHash := plumbing.NewHash(lineParts[1]) |
|
|
|
return nil, fmt.Errorf("reference %q is not a branch, can't push to it", branchName) |
|
|
|
branchName := plumbing.ReferenceName(lineParts[2]) |
|
|
|
} else if endHash == plumbing.ZeroHash { |
|
|
|
|
|
|
|
return nil, errors.New("deleting remote branches is not currently supported") |
|
|
|
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, proj.VerifyCanSetBranchHEADTo(branchName, endHash) |
|
|
|
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 |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("All pushed commits have been verified, well done.") |
|
|
|
cmd.Run(body) |
|
|
|
return nil, nil |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|