Skip to content

Commit

Permalink
Added support for non-symlink checkouts on Windows when elevated righ…
Browse files Browse the repository at this point in the history
…ts are missing

This implementation mimicks the behavior of Git on Windows
Signed-off-by: Felix Kollmann <[email protected]>
  • Loading branch information
fkollmann committed Mar 12, 2018
1 parent 1d28459 commit 4915f58
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 4915f58

Please sign in to comment.