-
Notifications
You must be signed in to change notification settings - Fork 12
/
dco.go
55 lines (48 loc) · 1.5 KB
/
dco.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package git
import (
"bufio"
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/carolynvs/magex/xplat"
)
//go:embed dco/prepare-commit-msg
var prepareCommitMsg string
// SetupDCO configures your git repository to automatically sign your commits
// to comply with our DCO
func SetupDCO() error {
gotShell := xplat.DetectShell()
if gotShell == "powershell" || gotShell == "cmd" {
return fmt.Errorf("setupDCO must be run from a shell that supports bash but %s was detected", gotShell)
}
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to determine the current working directory: %w", err)
}
repoRoot, err := FindRepositoryRoot(pwd)
if err != nil {
return err
}
hooksDir := filepath.Join(repoRoot, ".git/hooks/")
if err := os.MkdirAll(hooksDir, 0755); err != nil {
return fmt.Errorf("error ensuring that %s exists: %w", hooksDir, err)
}
hookPath := filepath.Join(hooksDir, "prepare-commit-msg")
// Ask first before overwriting an existing hook
_, err = os.Stat(hookPath)
if err == nil {
fmt.Printf("The DCO hook is already installed. Overwrite? [yN]: ")
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
if !strings.HasPrefix(strings.ToLower(response), "y") {
fmt.Println("The DCO hook was not installed")
return nil
}
} else if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("unable to read %s: %w", hookPath, err)
}
fmt.Println("Installing DCO git commit hook")
return os.WriteFile(hookPath, []byte(prepareCommitMsg), 0755)
}