Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Added support for symlink checkouts on Windows #776

Closed
wants to merge 9 commits into from
17 changes: 17 additions & 0 deletions worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,23 @@ func (w *Worktree) checkoutFileSymlink(f *object.File) (err error) {
}

err = w.Filesystem.Symlink(string(bytes), f.Name)

// On windows, this might fail.
// Follow Git on Windows behavior by writing the link as it is.
if err != nil && isSymlinkWindowsNonAdmin(err) {
mode, _ := f.Mode.ToOSFileMode()

to, err := w.Filesystem.OpenFile(f.Name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm())
if err != nil {
return err
}

defer ioutil.CheckClose(to, &err)

_, err = to.Write(bytes)
return err
}

return
}

Expand Down
4 changes: 4 additions & 0 deletions worktree_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ func init() {
}
}
}

func isSymlinkWindowsNonAdmin(err error) bool {
return false
}
4 changes: 4 additions & 0 deletions worktree_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ func init() {
}
}
}

func isSymlinkWindowsNonAdmin(err error) bool {
return false
}
15 changes: 15 additions & 0 deletions worktree_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package git

import (
"os"
"syscall"
"time"

Expand All @@ -18,3 +19,17 @@ func init() {
}
}
}

func isSymlinkWindowsNonAdmin(err error) bool {
const ERROR_PRIVILEGE_NOT_HELD syscall.Errno = 1314

if err != nil {
if errLink, ok := err.(*os.LinkError); ok {
if errNo, ok := errLink.Err.(syscall.Errno); ok {
return errNo == ERROR_PRIVILEGE_NOT_HELD
}
}
}

return false
}