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

Commit

Permalink
Merge pull request #778 from konsorten/fix-windows-symlink3
Browse files Browse the repository at this point in the history
Worktree.Checkout: handling of symlink on Windows
  • Loading branch information
mcuadros authored Mar 12, 2018
2 parents 1d28459 + 4915f58 commit 71e3741
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,22 @@ 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
}

0 comments on commit 71e3741

Please sign in to comment.