1
+ using GitSquash . VisualStudio . Properties ;
2
+
1
3
namespace GitSquash . VisualStudio
2
4
{
3
5
using System ;
@@ -29,10 +31,6 @@ public class GitSquashWrapper : IGitSquashWrapper
29
31
30
32
private readonly string repoDirectory ;
31
33
32
- private readonly Repository repository ;
33
-
34
- private bool isDisposed ;
35
-
36
34
/// <summary>
37
35
/// Initializes a new instance of the <see cref="GitSquashWrapper" /> class.
38
36
/// </summary>
@@ -42,15 +40,6 @@ public GitSquashWrapper(string repoDirectory, IGitSquashOutputLogger logger)
42
40
{
43
41
this . repoDirectory = repoDirectory ;
44
42
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 ) ;
54
43
}
55
44
56
45
/// <inheritdoc />
@@ -62,10 +51,12 @@ public string GetCommitMessages(GitCommit startCommit)
62
51
}
63
52
64
53
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 ( ) )
67
55
{
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
+ }
69
60
}
70
61
71
62
return sb . ToString ( ) ;
@@ -74,31 +65,40 @@ public string GetCommitMessages(GitCommit startCommit)
74
65
/// <inheritdoc />
75
66
public GitBranch GetCurrentBranch ( )
76
67
{
77
- return new GitBranch ( this . repository . Head . FriendlyName ) ;
68
+ using ( var repository = GetRepository ( ) )
69
+ {
70
+ return new GitBranch ( repository . Head . FriendlyName ) ;
71
+ }
78
72
}
79
73
80
74
/// <inheritdoc />
81
75
public async Task < GitCommandResponse > PushForce ( CancellationToken token )
82
76
{
83
- if ( this . repository . Head . IsTracking == false )
77
+ using ( var repository = GetRepository ( ) )
84
78
{
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
+ }
87
83
88
- return await this . RunGit ( "push -f" , token ) ;
84
+ return await this . RunGit ( "push -f" , token ) ;
85
+ }
89
86
}
90
87
91
88
/// <inheritdoc />
92
89
public IEnumerable < GitCommit > GetCommitsForBranch ( GitBranch branch , int number = 25 )
93
90
{
94
- Branch internalBranch = this . repository . Branches . FirstOrDefault ( x => x . FriendlyName == branch . FriendlyName ) ;
95
-
96
- if ( internalBranch == null )
91
+ using ( var repository = GetRepository ( ) )
97
92
{
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
+ }
100
99
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
+ }
102
102
}
103
103
104
104
/// <inheritdoc />
@@ -112,21 +112,30 @@ public bool IsRebaseHappening()
112
112
/// <inheritdoc />
113
113
public bool IsWorkingDirectoryDirty ( )
114
114
{
115
- return this . repository . RetrieveStatus ( ) . IsDirty ;
115
+ using ( var repository = GetRepository ( ) )
116
+ {
117
+ return repository . RetrieveStatus ( ) . IsDirty ;
118
+ }
116
119
}
117
120
118
121
/// <inheritdoc />
119
122
public bool HasConflicts ( )
120
123
{
121
- return this . repository . Index . IsFullyMerged == false ;
124
+ using ( var repository = GetRepository ( ) )
125
+ {
126
+ return repository . Index . IsFullyMerged == false ;
127
+ }
122
128
}
123
129
124
130
/// <inheritdoc />
125
131
public async Task < GitCommandResponse > Squash ( CancellationToken token , string newCommitMessage , GitCommit startCommit )
126
132
{
127
- if ( this . repository . RetrieveStatus ( ) . IsDirty )
133
+ using ( var repository = GetRepository ( ) )
128
134
{
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
+ }
130
139
}
131
140
132
141
string rewriterName ;
@@ -155,9 +164,12 @@ public Task<GitCommandResponse> FetchOrigin(CancellationToken token)
155
164
/// <inheritdoc />
156
165
public async Task < GitCommandResponse > Rebase ( CancellationToken token , GitBranch parentBranch )
157
166
{
158
- if ( this . repository . RetrieveStatus ( ) . IsDirty )
167
+ using ( var repository = GetRepository ( ) )
159
168
{
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
+ }
161
173
}
162
174
163
175
GitCommandResponse response = await this . FetchOrigin ( token ) ;
@@ -192,26 +204,10 @@ public async Task<GitCommandResponse> Continue(CancellationToken token)
192
204
/// <inheritdoc />
193
205
public IList < GitBranch > GetBranches ( )
194
206
{
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 ( ) )
205
208
{
206
- return ;
209
+ return repository . Branches . OrderBy ( x => x . FriendlyName ) . Select ( x => new GitBranch ( x . FriendlyName ) ) . ToList ( ) ;
207
210
}
208
-
209
- if ( isDisposing )
210
- {
211
- this . repository . Dispose ( ) ;
212
- }
213
-
214
- this . isDisposed = true ;
215
211
}
216
212
217
213
private static Process CreateGitProcess ( string arguments , string repoDirectory )
@@ -228,7 +224,7 @@ private static Task<int> RunProcessAsync(Process process, CancellationToken toke
228
224
token . Register ( ( ) =>
229
225
{
230
226
process . Kill ( ) ;
231
- outputLogger . WriteLine ( "Killing GIT process." ) ;
227
+ outputLogger . WriteLine ( Resources . KillingProcess ) ;
232
228
} ) ;
233
229
234
230
process . Exited += ( s , ea ) => tcs . SetResult ( process . ExitCode ) ;
@@ -247,6 +243,11 @@ private static Task<int> RunProcessAsync(Process process, CancellationToken toke
247
243
return tcs . Task ;
248
244
}
249
245
246
+ private Repository GetRepository ( )
247
+ {
248
+ return new Repository ( repoDirectory ) ;
249
+ }
250
+
250
251
private bool GetWritersName ( out string rebaseWriter , out string commentWriter )
251
252
{
252
253
rebaseWriter = null ;
@@ -339,7 +340,7 @@ private void OnErrorReceived(object sender, DataReceivedEventArgs dataReceivedEv
339
340
340
341
error = new StringBuilder ( ) ;
341
342
error . Append ( dataReceivedEventArgs . Data ) ;
342
- outputLogger . WriteLine ( $ "Error: { dataReceivedEventArgs . Data } " ) ;
343
+ outputLogger . WriteLine ( string . Format ( Resources . ErrorGit , dataReceivedEventArgs . Data ) ) ;
343
344
}
344
345
}
345
346
}
0 commit comments