11package exec
22
33import (
4+ "errors"
5+ "fmt"
46 "github.com/captainhook-go/captainhook/configuration"
57 "github.com/captainhook-go/captainhook/git"
68 "github.com/captainhook-go/captainhook/hooks/util"
79 "github.com/captainhook-go/captainhook/info"
810 "github.com/captainhook-go/captainhook/io"
911 "os"
1012 "os/exec"
13+ "path/filepath"
1114 "sort"
1215 "strings"
1316 "text/template"
@@ -59,6 +62,7 @@ func (i *Installer) Run() error {
5962 for _ , hook := range keys {
6063 err := i .installHook (hook , hooks [hook ] && ! i .force )
6164 if err != nil {
65+ i .appIO .Write ("<warning>" + err .Error ()+ "</warning>" , true , io .NORMAL )
6266 return err
6367 }
6468 }
@@ -100,6 +104,11 @@ func (i *Installer) writeHookFile(hook string) error {
100104 doIt = io .AnswerToBool (answer )
101105 }
102106
107+ err := i .checkForBrokenSymlink (hook )
108+ if err != nil {
109+ return err
110+ }
111+
103112 if doIt {
104113 vars := make (map [string ]interface {})
105114 vars ["HOOK_NAME" ] = hook
@@ -110,7 +119,10 @@ func (i *Installer) writeHookFile(hook string) error {
110119
111120 tpl , _ := template .New ("hook" ).Parse (i .HookTemplate ())
112121
113- file , _ := os .Create (i .repo .HooksDir () + "/" + hook )
122+ file , fileErr := os .Create (i .repo .HooksDir () + "/" + hook )
123+ if fileErr != nil {
124+ return fileErr
125+ }
114126 defer file .Close ()
115127
116128 tplErr := tpl .Execute (file , vars )
@@ -236,6 +248,31 @@ func (i *Installer) HookTemplate() string {
236248 "hook {{ .HOOK_NAME }} \" $@\" <&0\n \n "
237249}
238250
251+ func (i * Installer ) checkForBrokenSymlink (hook string ) error {
252+ filePath := i .repo .HooksDir () + "/" + hook
253+ isSymlink , _ := isSymlink (filePath )
254+ if ! isSymlink {
255+ return nil
256+ }
257+ target , err := os .Readlink (filePath )
258+ if err != nil {
259+ return err
260+ }
261+ if ! filepath .IsAbs (target ) {
262+ target = filepath .Join (filepath .Dir (filePath ), target )
263+ }
264+
265+ _ , err = os .Stat (target )
266+ if err != nil {
267+ if os .IsNotExist (err ) {
268+ return errors .New (hook + " is a broken symlink please delete or fix it and try again" )
269+ }
270+ fmt .Println ("File fucked: " , err .Error ())
271+ return err
272+ }
273+ return nil
274+ }
275+
239276func NewInstaller (appIO io.IO , config * configuration.Configuration , repo git.Repo ) * Installer {
240277 return & Installer {
241278 appIO : appIO ,
0 commit comments