@@ -495,11 +495,9 @@ public static string Init(string path, bool isBare)
495
495
{
496
496
Ensure . ArgumentNotNullOrEmptyString ( path , "path" ) ;
497
497
498
- using ( RepositoryHandle repo = Proxy . git_repository_init_ext ( null , path , isBare ) )
499
- {
500
- FilePath repoPath = Proxy . git_repository_path ( repo ) ;
501
- return repoPath . Native ;
502
- }
498
+ var initOptions = new InitOptions { IsBare = isBare } ;
499
+
500
+ return Init ( path , initOptions ) ;
503
501
}
504
502
505
503
/// <summary>
@@ -518,9 +516,28 @@ public static string Init(string workingDirectoryPath, string gitDirectoryPath)
518
516
// to pass a path relatively to his current directory.
519
517
string wd = Path . GetFullPath ( workingDirectoryPath ) ;
520
518
519
+ var initOptions = new InitOptions { WorkdirPath = wd } ;
520
+
521
521
// TODO: Shouldn't we ensure that the working folder isn't under the gitDir?
522
522
523
- using ( RepositoryHandle repo = Proxy . git_repository_init_ext ( wd , gitDirectoryPath , false ) )
523
+ return Init ( gitDirectoryPath , initOptions ) ;
524
+ }
525
+
526
+ /// <summary>
527
+ /// Initialize a repository at the specified <paramref name="path"/>,
528
+ /// providing optional behavioral overrides through the <paramref name="options"/> parameter.
529
+ /// </summary>
530
+ /// <param name="path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param>
531
+ /// <param name="options">Options controlling init behavior.</param>
532
+ /// <returns>The path to the created repository.</returns>
533
+ public static string Init ( string path , InitOptions options )
534
+ {
535
+ Ensure . ArgumentNotNullOrEmptyString ( path , "path" ) ;
536
+
537
+ options = options ?? new InitOptions ( ) ;
538
+
539
+ using ( var opts = GitRepositoryInitOptions . BuildFrom ( options ) )
540
+ using ( RepositoryHandle repo = Proxy . git_repository_init_ext ( path , opts ) )
524
541
{
525
542
FilePath repoPath = Proxy . git_repository_path ( repo ) ;
526
543
return repoPath . Native ;
@@ -675,22 +692,47 @@ public static IEnumerable<Reference> ListRemoteReferences(string url, Credential
675
692
Ensure . ArgumentNotNull ( url , "url" ) ;
676
693
677
694
using ( RepositoryHandle repositoryHandle = Proxy . git_repository_new ( ) )
678
- using ( RemoteHandle remoteHandle = Proxy . git_remote_create_anonymous ( repositoryHandle , url ) )
695
+ using ( RemoteHandle remoteHandle = ConnectToAnonymousRemote ( repositoryHandle , url , credentialsProvider ) )
679
696
{
680
- var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
681
- var proxyOptions = new GitProxyOptions { Version = 1 } ;
697
+ return Proxy . git_remote_ls ( null , remoteHandle ) ;
698
+ }
699
+ }
682
700
683
- if ( credentialsProvider != null )
684
- {
685
- var callbacks = new RemoteCallbacks ( credentialsProvider ) ;
686
- gitCallbacks = callbacks . GenerateCallbacks ( ) ;
687
- }
701
+ /// <summary>
702
+ /// Retrieves the name of the Remote Repository's default branch.
703
+ /// </summary>
704
+ /// <param name="url">The url to retrieve from.</param>
705
+ /// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
706
+ /// <returns>The reference name.</returns>
707
+ public static string GetRemoteDefaultBranch ( string url , CredentialsHandler credentialsProvider )
708
+ {
709
+ Ensure . ArgumentNotNull ( url , "url" ) ;
688
710
689
- Proxy . git_remote_connect ( remoteHandle , GitDirection . Fetch , ref gitCallbacks , ref proxyOptions ) ;
690
- return Proxy . git_remote_ls ( null , remoteHandle ) ;
711
+ using ( RepositoryHandle repositoryHandle = Proxy . git_repository_new ( ) )
712
+ using ( RemoteHandle remoteHandle = ConnectToAnonymousRemote ( repositoryHandle , url , credentialsProvider ) )
713
+ {
714
+ return Proxy . git_remote_default_branch ( remoteHandle ) ;
691
715
}
692
716
}
693
717
718
+ private static RemoteHandle ConnectToAnonymousRemote ( RepositoryHandle repositoryHandle , string url , CredentialsHandler credentialsProvider )
719
+ {
720
+ RemoteHandle remoteHandle = Proxy . git_remote_create_anonymous ( repositoryHandle , url ) ;
721
+
722
+ var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
723
+ var proxyOptions = new GitProxyOptions { Version = 1 } ;
724
+
725
+ if ( credentialsProvider != null )
726
+ {
727
+ var callbacks = new RemoteCallbacks ( credentialsProvider ) ;
728
+ gitCallbacks = callbacks . GenerateCallbacks ( ) ;
729
+ }
730
+
731
+ Proxy . git_remote_connect ( remoteHandle , GitDirection . Fetch , ref gitCallbacks , ref proxyOptions ) ;
732
+
733
+ return remoteHandle ;
734
+ }
735
+
694
736
/// <summary>
695
737
/// Probe for a git repository.
696
738
/// <para>The lookup start from <paramref name="startingPath"/> and walk upward parent directories if nothing has been found.</para>
0 commit comments