@@ -23,8 +23,9 @@ public void CanCheckoutAnExistingBranch(string branchName)
23
23
Branch master = repo . Branches [ "master" ] ;
24
24
Assert . True ( master . IsCurrentRepositoryHead ) ;
25
25
26
- // Hard reset to ensure that working directory, index, and HEAD match
27
- repo . Reset ( ResetOptions . Hard ) ;
26
+ // Set the working directory to the current head
27
+ ResetAndCleanWorkingDirectory ( repo ) ;
28
+
28
29
Assert . False ( repo . Index . RetrieveStatus ( ) . IsDirty ) ;
29
30
30
31
Branch branch = repo . Branches [ branchName ] ;
@@ -55,8 +56,9 @@ public void CanCheckoutAnExistingBranchByName(string branchName)
55
56
Branch master = repo . Branches [ "master" ] ;
56
57
Assert . True ( master . IsCurrentRepositoryHead ) ;
57
58
58
- // Hard reset to ensure that working directory, index, and HEAD match
59
- repo . Reset ( ResetOptions . Hard ) ;
59
+ // Set the working directory to the current head
60
+ ResetAndCleanWorkingDirectory ( repo ) ;
61
+
60
62
Assert . False ( repo . Index . RetrieveStatus ( ) . IsDirty ) ;
61
63
62
64
Branch test = repo . Checkout ( branchName ) ;
@@ -84,8 +86,9 @@ public void CanCheckoutAnArbitraryCommit(string commitPointer)
84
86
Branch master = repo . Branches [ "master" ] ;
85
87
Assert . True ( master . IsCurrentRepositoryHead ) ;
86
88
87
- // Hard reset to ensure that working directory, index, and HEAD match
88
- repo . Reset ( ResetOptions . Hard ) ;
89
+ // Set the working directory to the current head
90
+ ResetAndCleanWorkingDirectory ( repo ) ;
91
+
89
92
Assert . False ( repo . Index . RetrieveStatus ( ) . IsDirty ) ;
90
93
91
94
Branch detachedHead = repo . Checkout ( commitPointer ) ;
@@ -197,8 +200,9 @@ public void CanForcefullyCheckoutWithStagedChanges()
197
200
Branch master = repo . Branches [ "master" ] ;
198
201
Assert . True ( master . IsCurrentRepositoryHead ) ;
199
202
200
- // Hard reset to ensure that working directory, index, and HEAD match
201
- repo . Reset ( ResetOptions . Hard ) ;
203
+ // Set the working directory to the current head
204
+ ResetAndCleanWorkingDirectory ( repo ) ;
205
+
202
206
Assert . False ( repo . Index . RetrieveStatus ( ) . IsDirty ) ;
203
207
204
208
// Add local change
@@ -335,5 +339,19 @@ private void PopulateBasicRepository(Repository repo)
335
339
336
340
repo . CreateBranch ( otherBranchName ) ;
337
341
}
342
+
343
+ /// <summary>
344
+ /// Reset and clean current working directory. This will ensure that the current
345
+ /// working directory matches the current Head commit.
346
+ /// </summary>
347
+ /// <param name="repo">Repository whose current working directory should be operated on.</param>
348
+ private void ResetAndCleanWorkingDirectory ( Repository repo )
349
+ {
350
+ // Reset the index and the working tree.
351
+ repo . Reset ( ResetOptions . Hard ) ;
352
+
353
+ // Remove untracked files.
354
+ repo . Index . CleanWorkingDirectory ( ) ;
355
+ }
338
356
}
339
357
}
0 commit comments