11using System . Globalization ;
2+ using System . Linq ;
23using LibGit2Sharp . Core ;
34using System . Collections . Generic ;
5+ using LibGit2Sharp . Handlers ;
46
57namespace LibGit2Sharp
68{
@@ -9,6 +11,54 @@ namespace LibGit2Sharp
911 /// </summary>
1012 public static class NetworkExtensions
1113 {
14+ /// <summary>
15+ /// Push the specified branch to its tracked branch on the remote.
16+ /// </summary>
17+ /// <param name="network">The <see cref="Network"/> being worked with.</param>
18+ /// <param name="branch">The branch to push.</param>
19+ /// <param name="onPushStatusError">Handler for reporting failed push updates.</param>
20+ /// <param name="credentials">Credentials to use for user/pass authentication.</param>
21+ /// <exception cref="LibGit2SharpException">Throws if either the Remote or the UpstreamBranchCanonicalName is not set.</exception>
22+ public static void Push (
23+ this Network network ,
24+ Branch branch ,
25+ PushStatusErrorHandler onPushStatusError = null ,
26+ Credentials credentials = null )
27+ {
28+ network . Push ( new [ ] { branch } , onPushStatusError , credentials ) ;
29+ }
30+
31+ /// <summary>
32+ /// Push the specified branches to their tracked branches on the remote.
33+ /// </summary>
34+ /// <param name="network">The <see cref="Network"/> being worked with.</param>
35+ /// <param name="branches">The branches to push.</param>
36+ /// <param name="onPushStatusError">Handler for reporting failed push updates.</param>
37+ /// <param name="credentials">Credentials to use for user/pass authentication.</param>
38+ /// <exception cref="LibGit2SharpException">Throws if either the Remote or the UpstreamBranchCanonicalName is not set.</exception>
39+ public static void Push (
40+ this Network network ,
41+ IEnumerable < Branch > branches ,
42+ PushStatusErrorHandler onPushStatusError = null ,
43+ Credentials credentials = null )
44+ {
45+ var enumeratedBranches = branches as IList < Branch > ?? branches . ToList ( ) ;
46+
47+ foreach ( var branch in enumeratedBranches )
48+ {
49+ if ( string . IsNullOrEmpty ( branch . UpstreamBranchCanonicalName ) )
50+ {
51+ throw new LibGit2SharpException ( string . Format ( "The branch '{0}' (\" {1}\" ) that you are trying to push does not track an upstream branch." ,
52+ branch . Name , branch . CanonicalName ) ) ;
53+ }
54+ }
55+
56+ foreach ( var branch in enumeratedBranches )
57+ {
58+ network . Push ( branch . Remote , string . Format ( "{0}:{1}" , branch . CanonicalName , branch . UpstreamBranchCanonicalName ) , onPushStatusError ) ;
59+ }
60+ }
61+
1262 /// <summary>
1363 /// Push the objectish to the destination reference on the <see cref = "Remote" />.
1464 /// </summary>
@@ -46,7 +96,7 @@ public static PushResult Push(this Network network, Remote remote, string pushRe
4696 Ensure . ArgumentNotNull ( remote , "remote" ) ;
4797 Ensure . ArgumentNotNullOrEmptyString ( pushRefSpec , "pushRefSpec" ) ;
4898
49- return network . Push ( remote , new string [ ] { pushRefSpec } , credentials ) ;
99+ return network . Push ( remote , new [ ] { pushRefSpec } , credentials ) ;
50100 }
51101
52102 /// <summary>
@@ -62,7 +112,7 @@ public static PushResult Push(this Network network, Remote remote, IEnumerable<s
62112 Ensure . ArgumentNotNull ( remote , "remote" ) ;
63113 Ensure . ArgumentNotNull ( pushRefSpecs , "pushRefSpecs" ) ;
64114
65- List < PushStatusError > failedRemoteUpdates = new List < PushStatusError > ( ) ;
115+ var failedRemoteUpdates = new List < PushStatusError > ( ) ;
66116
67117 network . Push (
68118 remote ,
0 commit comments