Skip to content

Commit 8448dcc

Browse files
author
Edward Thomson
committed
Minor refactor for cleanliness
1 parent c41bf72 commit 8448dcc

File tree

3 files changed

+43
-44
lines changed

3 files changed

+43
-44
lines changed

LibGit2Sharp/Index.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,6 @@ public virtual FileStatus RetrieveStatus(string filePath)
502502
/// <returns>A <see cref="RepositoryStatus"/> holding the state of all the files.</returns>
503503
public virtual RepositoryStatus RetrieveStatus(StatusOptions options = null)
504504
{
505-
if (options == null)
506-
{
507-
options = new StatusOptions();
508-
}
509-
510505
ReloadFromDisk();
511506

512507
return new RepositoryStatus(repo, options);

LibGit2Sharp/RepositoryStatus.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,9 @@ internal RepositoryStatus(Repository repo, StatusOptions options)
5858
{
5959
statusEntries = new List<StatusEntry>();
6060

61-
var gitOptions = new GitStatusOptions
61+
using (GitStatusOptions coreOptions = CreateStatusOptions(options ?? new StatusOptions()))
6262
{
63-
Version = 1,
64-
Show = (GitStatusShow)options.Show,
65-
Flags =
66-
GitStatusOptionFlags.IncludeIgnored |
67-
GitStatusOptionFlags.IncludeUntracked |
68-
GitStatusOptionFlags.RecurseUntrackedDirs,
69-
};
70-
71-
if (options != null && options.DetectRenamesInIndex)
72-
{
73-
gitOptions.Flags |=
74-
GitStatusOptionFlags.RenamesHeadToIndex |
75-
GitStatusOptionFlags.RenamesFromRewrites;
76-
}
77-
78-
if (options != null && options.DetectRenamesInWorkDir)
79-
{
80-
gitOptions.Flags |=
81-
GitStatusOptionFlags.RenamesIndexToWorkDir |
82-
GitStatusOptionFlags.RenamesFromRewrites;
83-
}
84-
85-
if (options != null && options.ExcludeSubmodules)
86-
{
87-
gitOptions.Flags |=
88-
GitStatusOptionFlags.ExcludeSubmodules;
89-
}
90-
91-
try
92-
{
93-
StatusListSafeHandle list = Proxy.git_status_list_new(repo.Handle, gitOptions);
63+
StatusListSafeHandle list = Proxy.git_status_list_new(repo.Handle, coreOptions);
9464
int count = Proxy.git_status_list_entrycount(list);
9565

9666
for (int i = 0; i < count; i++)
@@ -115,10 +85,41 @@ internal RepositoryStatus(Repository repo, StatusOptions options)
11585

11686
isDirty = statusEntries.Any(entry => entry.State != FileStatus.Ignored);
11787
}
118-
finally
88+
}
89+
90+
private static GitStatusOptions CreateStatusOptions(StatusOptions options)
91+
{
92+
var coreOptions = new GitStatusOptions
93+
{
94+
Version = 1,
95+
Show = (GitStatusShow)options.Show,
96+
Flags =
97+
GitStatusOptionFlags.IncludeIgnored |
98+
GitStatusOptionFlags.IncludeUntracked |
99+
GitStatusOptionFlags.RecurseUntrackedDirs,
100+
};
101+
102+
if (options.DetectRenamesInIndex)
119103
{
120-
gitOptions.Dispose();
104+
coreOptions.Flags |=
105+
GitStatusOptionFlags.RenamesHeadToIndex |
106+
GitStatusOptionFlags.RenamesFromRewrites;
107+
}
108+
109+
if (options.DetectRenamesInWorkDir)
110+
{
111+
coreOptions.Flags |=
112+
GitStatusOptionFlags.RenamesIndexToWorkDir |
113+
GitStatusOptionFlags.RenamesFromRewrites;
121114
}
115+
116+
if (options.ExcludeSubmodules)
117+
{
118+
coreOptions.Flags |=
119+
GitStatusOptionFlags.ExcludeSubmodules;
120+
}
121+
122+
return coreOptions;
122123
}
123124

124125
private void AddStatusEntryForDelta(FileStatus gitStatus, GitDiffDelta deltaHeadToIndex, GitDiffDelta deltaIndexToWorkDir)

LibGit2Sharp/StatusOptions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ public enum StatusShowOption
2929
/// <summary>
3030
/// Options controlling the status behavior.
3131
/// </summary>
32-
public class StatusOptions
32+
public sealed class StatusOptions
3333
{
3434
/// <summary>
3535
/// Initializes a new instance of the <see cref="StatusOptions"/> class.
36+
/// By default, both the index and the working directory will be scanned
37+
/// for status, and renames will be detected from changes staged in the
38+
/// index only.
3639
/// </summary>
3740
public StatusOptions()
3841
{
@@ -42,21 +45,21 @@ public StatusOptions()
4245
/// <summary>
4346
/// Which files should be scanned and returned
4447
/// </summary>
45-
public virtual StatusShowOption Show { get; set; }
48+
public StatusShowOption Show { get; set; }
4649

4750
/// <summary>
4851
/// Examine the staged changes for renames.
4952
/// </summary>
50-
public virtual bool DetectRenamesInIndex { get; set; }
53+
public bool DetectRenamesInIndex { get; set; }
5154

5255
/// <summary>
5356
/// Examine unstaged changes in the working directory for renames.
5457
/// </summary>
55-
public virtual bool DetectRenamesInWorkDir { get; set; }
58+
public bool DetectRenamesInWorkDir { get; set; }
5659

5760
/// <summary>
5861
/// Exclude submodules from being scanned for status
5962
/// </summary>
60-
public virtual bool ExcludeSubmodules { get; set; }
63+
public bool ExcludeSubmodules { get; set; }
6164
}
6265
}

0 commit comments

Comments
 (0)