@@ -272,6 +272,34 @@ public bool IsFileSystemSupported(string path, out string error)
272272 return true ;
273273 }
274274
275+ /// <summary>
276+ /// On Windows, if the current user is elevated, the owner of the directory will be the Administrators group by default.
277+ /// This runs afoul of the git "dubious ownership" check, which can fail if either the .git directory or the working directory
278+ /// are not owned by the current user.
279+ ///
280+ /// At the moment git for windows does not consider a non-elevated admin to be the owner of a directory owned by the Administrators group,
281+ /// though a fix is in progress in the microsoft fork of git. Libgit2(sharp) also does not have this fix.
282+ ///
283+ /// Also, even if the fix were in place, automount would still fail because it runs under SYSTEM account.
284+ ///
285+ /// This method ensures that the directory is owned by the current user (which is verified to work for SYSTEM account for automount).
286+ /// </summary>
287+ public void EnsureDirectoryIsOwnedByCurrentUser ( string directoryPath )
288+ {
289+ // Ensure directory exists, inheriting all other ACLS
290+ Directory . CreateDirectory ( directoryPath ) ;
291+ // If the user is currently elevated, the owner of the directory will be the Administrators group.
292+ DirectorySecurity directorySecurity = Directory . GetAccessControl ( directoryPath ) ;
293+ IdentityReference directoryOwner = directorySecurity . GetOwner ( typeof ( SecurityIdentifier ) ) ;
294+ SecurityIdentifier administratorsSid = new SecurityIdentifier ( WellKnownSidType . BuiltinAdministratorsSid , null ) ;
295+ if ( directoryOwner == administratorsSid )
296+ {
297+ WindowsIdentity currentUser = WindowsIdentity . GetCurrent ( ) ;
298+ directorySecurity . SetOwner ( currentUser . User ) ;
299+ Directory . SetAccessControl ( directoryPath , directorySecurity ) ;
300+ }
301+ }
302+
275303 private class NativeFileReader
276304 {
277305 private const uint GenericRead = 0x80000000 ;
0 commit comments