1+ using GitSquash . VisualStudio . Properties ;
2+
13namespace GitSquash . VisualStudio
24{
35 using System ;
@@ -29,10 +31,6 @@ public class GitSquashWrapper : IGitSquashWrapper
2931
3032 private readonly string repoDirectory ;
3133
32- private readonly Repository repository ;
33-
34- private bool isDisposed ;
35-
3634 /// <summary>
3735 /// Initializes a new instance of the <see cref="GitSquashWrapper" /> class.
3836 /// </summary>
@@ -42,15 +40,6 @@ public GitSquashWrapper(string repoDirectory, IGitSquashOutputLogger logger)
4240 {
4341 this . repoDirectory = repoDirectory ;
4442 outputLogger = logger ;
45-
46- this . repository = new Repository ( repoDirectory ) ;
47- }
48-
49- /// <inheritdoc />
50- public void Dispose ( )
51- {
52- this . Dispose ( true ) ;
53- GC . SuppressFinalize ( this ) ;
5443 }
5544
5645 /// <inheritdoc />
@@ -62,10 +51,12 @@ public string GetCommitMessages(GitCommit startCommit)
6251 }
6352
6453 var sb = new StringBuilder ( ) ;
65-
66- foreach ( Commit commit in this . repository . Head . Commits . TakeWhile ( x => x . Sha != startCommit . Sha ) )
54+ using ( var repository = GetRepository ( ) )
6755 {
68- sb . AppendLine ( commit . Message ) ;
56+ foreach ( Commit commit in repository . Head . Commits . TakeWhile ( x => x . Sha != startCommit . Sha ) )
57+ {
58+ sb . AppendLine ( commit . Message ) ;
59+ }
6960 }
7061
7162 return sb . ToString ( ) ;
@@ -74,31 +65,40 @@ public string GetCommitMessages(GitCommit startCommit)
7465 /// <inheritdoc />
7566 public GitBranch GetCurrentBranch ( )
7667 {
77- return new GitBranch ( this . repository . Head . FriendlyName ) ;
68+ using ( var repository = GetRepository ( ) )
69+ {
70+ return new GitBranch ( repository . Head . FriendlyName ) ;
71+ }
7872 }
7973
8074 /// <inheritdoc />
8175 public async Task < GitCommandResponse > PushForce ( CancellationToken token )
8276 {
83- if ( this . repository . Head . IsTracking == false )
77+ using ( var repository = GetRepository ( ) )
8478 {
85- return new GitCommandResponse ( false , "The branch has not been pushed before." ) ;
86- }
79+ if ( repository . Head . IsTracking == false )
80+ {
81+ return new GitCommandResponse ( false , "The branch has not been pushed before." ) ;
82+ }
8783
88- return await this . RunGit ( "push -f" , token ) ;
84+ return await this . RunGit ( "push -f" , token ) ;
85+ }
8986 }
9087
9188 /// <inheritdoc />
9289 public IEnumerable < GitCommit > GetCommitsForBranch ( GitBranch branch , int number = 25 )
9390 {
94- Branch internalBranch = this . repository . Branches . FirstOrDefault ( x => x . FriendlyName == branch . FriendlyName ) ;
95-
96- if ( internalBranch == null )
91+ using ( var repository = GetRepository ( ) )
9792 {
98- return Enumerable . Empty < GitCommit > ( ) ;
99- }
93+ Branch internalBranch = repository . Branches . FirstOrDefault ( x => x . FriendlyName == branch . FriendlyName ) ;
94+
95+ if ( internalBranch == null )
96+ {
97+ return Enumerable . Empty < GitCommit > ( ) ;
98+ }
10099
101- return internalBranch . Commits . Take ( number ) . Select ( x => new GitCommit ( x . Sha , x . MessageShort ) ) ;
100+ return internalBranch . Commits . Take ( number ) . Select ( x => new GitCommit ( x . Sha , x . MessageShort ) ) ;
101+ }
102102 }
103103
104104 /// <inheritdoc />
@@ -112,21 +112,30 @@ public bool IsRebaseHappening()
112112 /// <inheritdoc />
113113 public bool IsWorkingDirectoryDirty ( )
114114 {
115- return this . repository . RetrieveStatus ( ) . IsDirty ;
115+ using ( var repository = GetRepository ( ) )
116+ {
117+ return repository . RetrieveStatus ( ) . IsDirty ;
118+ }
116119 }
117120
118121 /// <inheritdoc />
119122 public bool HasConflicts ( )
120123 {
121- return this . repository . Index . IsFullyMerged == false ;
124+ using ( var repository = GetRepository ( ) )
125+ {
126+ return repository . Index . IsFullyMerged == false ;
127+ }
122128 }
123129
124130 /// <inheritdoc />
125131 public async Task < GitCommandResponse > Squash ( CancellationToken token , string newCommitMessage , GitCommit startCommit )
126132 {
127- if ( this . repository . RetrieveStatus ( ) . IsDirty )
133+ using ( var repository = GetRepository ( ) )
128134 {
129- return new GitCommandResponse ( false , "Cannot rebase: You have unstaged changes." ) ;
135+ if ( repository . RetrieveStatus ( ) . IsDirty )
136+ {
137+ return new GitCommandResponse ( false , "Cannot rebase: You have unstaged changes." ) ;
138+ }
130139 }
131140
132141 string rewriterName ;
@@ -155,9 +164,12 @@ public Task<GitCommandResponse> FetchOrigin(CancellationToken token)
155164 /// <inheritdoc />
156165 public async Task < GitCommandResponse > Rebase ( CancellationToken token , GitBranch parentBranch )
157166 {
158- if ( this . repository . RetrieveStatus ( ) . IsDirty )
167+ using ( var repository = GetRepository ( ) )
159168 {
160- return new GitCommandResponse ( false , "Cannot rebase: You have unstaged changes." ) ;
169+ if ( repository . RetrieveStatus ( ) . IsDirty )
170+ {
171+ return new GitCommandResponse ( false , "Cannot rebase: You have unstaged changes." ) ;
172+ }
161173 }
162174
163175 GitCommandResponse response = await this . FetchOrigin ( token ) ;
@@ -192,26 +204,10 @@ public async Task<GitCommandResponse> Continue(CancellationToken token)
192204 /// <inheritdoc />
193205 public IList < GitBranch > GetBranches ( )
194206 {
195- return this . repository . Branches . OrderBy ( x => x . FriendlyName ) . Select ( x => new GitBranch ( x . FriendlyName ) ) . ToList ( ) ;
196- }
197-
198- /// <summary>
199- /// Disposes of any objects contained in the class.
200- /// </summary>
201- /// <param name="isDisposing">If the ,method is being called by the dispose method.</param>
202- protected virtual void Dispose ( bool isDisposing )
203- {
204- if ( this . isDisposed )
207+ using ( var repository = GetRepository ( ) )
205208 {
206- return ;
209+ return repository . Branches . OrderBy ( x => x . FriendlyName ) . Select ( x => new GitBranch ( x . FriendlyName ) ) . ToList ( ) ;
207210 }
208-
209- if ( isDisposing )
210- {
211- this . repository . Dispose ( ) ;
212- }
213-
214- this . isDisposed = true ;
215211 }
216212
217213 private static Process CreateGitProcess ( string arguments , string repoDirectory )
@@ -228,7 +224,7 @@ private static Task<int> RunProcessAsync(Process process, CancellationToken toke
228224 token . Register ( ( ) =>
229225 {
230226 process . Kill ( ) ;
231- outputLogger . WriteLine ( "Killing GIT process." ) ;
227+ outputLogger . WriteLine ( Resources . KillingProcess ) ;
232228 } ) ;
233229
234230 process . Exited += ( s , ea ) => tcs . SetResult ( process . ExitCode ) ;
@@ -247,6 +243,11 @@ private static Task<int> RunProcessAsync(Process process, CancellationToken toke
247243 return tcs . Task ;
248244 }
249245
246+ private Repository GetRepository ( )
247+ {
248+ return new Repository ( repoDirectory ) ;
249+ }
250+
250251 private bool GetWritersName ( out string rebaseWriter , out string commentWriter )
251252 {
252253 rebaseWriter = null ;
@@ -339,7 +340,7 @@ private void OnErrorReceived(object sender, DataReceivedEventArgs dataReceivedEv
339340
340341 error = new StringBuilder ( ) ;
341342 error . Append ( dataReceivedEventArgs . Data ) ;
342- outputLogger . WriteLine ( $ "Error: { dataReceivedEventArgs . Data } " ) ;
343+ outputLogger . WriteLine ( string . Format ( Resources . ErrorGit , dataReceivedEventArgs . Data ) ) ;
343344 }
344345 }
345346}
0 commit comments