Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 186048b

Browse files
Complete the calculation of LFS disk usage
1 parent b548733 commit 186048b

9 files changed

+244
-37
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -289,46 +289,32 @@ private void ConfigureMergeSettings()
289289

290290
private void CaptureRepoSize()
291291
{
292+
IProcessTask<int> diskUsageTask = null;
293+
var gitLfsDataPath = Environment.RepositoryPath.Combine(".git", "lfs");
294+
if (gitLfsDataPath.Exists())
295+
{
296+
diskUsageTask = Environment.IsWindows
297+
? (IProcessTask<int>)new WindowsDiskUsageTask(gitLfsDataPath, TaskManager.Token)
298+
: new LinuxDiskUsageTask(gitLfsDataPath, TaskManager.Token);
299+
300+
diskUsageTask.Configure(ProcessManager);
301+
}
302+
292303
GitClient.CountObjects()
293-
.Then((success, gitObjects) => {
294-
if (success)
304+
.Finally((b1, gitObjects) => {
305+
if (b1)
295306
{
296307
UsageTracker.UpdateRepoSize(gitObjects.kilobytes);
297308
}
309+
310+
diskUsageTask?.Then((b2, kilobytes) => {
311+
if (b2)
312+
{
313+
UsageTracker.UpdateLfsDiskUsage(kilobytes);
314+
}
315+
}).Start();
298316
})
299317
.Start();
300-
301-
var gitLfsDataPath = Environment.RepositoryPath.Combine(".git", "lfs");
302-
if (gitLfsDataPath.Exists())
303-
{
304-
if (Environment.IsWindows)
305-
{
306-
new WindowsDiskUsageTask(gitLfsDataPath, TaskManager.Token).Configure(ProcessManager).Then(
307-
(b, list) => {
308-
if (b)
309-
{
310-
try
311-
{
312-
var output = list[list.Count - 2];
313-
var proc = new LineParser(output);
314-
proc.SkipWhitespace();
315-
proc.ReadUntilWhitespace();
316-
proc.ReadUntilWhitespace();
317-
proc.SkipWhitespace();
318-
319-
var sizeInBytes = int.Parse(proc.ReadUntilWhitespace().Replace(",", string.Empty));
320-
var sizeInKilobytes = sizeInBytes / 1024;
321-
322-
UsageTracker.UpdateLfsDiskUsage(sizeInKilobytes);
323-
}
324-
catch (Exception e)
325-
{
326-
Logger.Error(e, "Error Calculating LFS Disk Usage");
327-
}
328-
}
329-
}).Start();
330-
}
331-
}
332318
}
333319

334320
public void RestartRepository()

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@
120120
<Compile Include="OutputProcessors\GitAheadBehindStatusOutputProcessor.cs" />
121121
<Compile Include="OutputProcessors\GitCountObjectsProcessor.cs" />
122122
<Compile Include="OutputProcessors\LfsVersionOutputProcessor.cs" />
123+
<Compile Include="OutputProcessors\LinuxDiskUsageOutputProcessor.cs" />
123124
<Compile Include="OutputProcessors\VersionOutputProcessor.cs" />
124125
<Compile Include="Helpers\TaskHelpers.cs" />
126+
<Compile Include="OutputProcessors\WindowsDiskUsageOutputProcessor.cs" />
125127
<Compile Include="Platform\DefaultEnvironment.cs" />
126128
<Compile Include="Extensions\EnvironmentExtensions.cs" />
127129
<Compile Include="Extensions\FileEventExtensions.cs" />
@@ -135,6 +137,7 @@
135137
<Compile Include="Git\IGitObjectFactory.cs" />
136138
<Compile Include="Git\Tasks\GitRevertTask.cs" />
137139
<Compile Include="Application\IApplicationManager.cs" />
140+
<Compile Include="Platform\LinuxDiskUsageTask.cs" />
138141
<Compile Include="Platform\WindowsDiskUsageTask.cs" />
139142
<Compile Include="Primitives\Package.cs" />
140143
<Compile Include="Primitives\TheVersion.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
public class LinuxDiskUsageOutputProcessor : BaseOutputProcessor<int>
6+
{
7+
private string buffer;
8+
9+
public override void LineReceived(string line)
10+
{
11+
if (line == null)
12+
{
13+
if (buffer == null)
14+
{
15+
throw new InvalidOperationException("Not enough input");
16+
}
17+
18+
var proc = new LineParser(buffer);
19+
var kilobytes = int.Parse(proc.ReadUntilWhitespace());
20+
21+
RaiseOnEntry(kilobytes);
22+
}
23+
else
24+
{
25+
buffer = line;
26+
}
27+
}
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
public class WindowsDiskUsageOutputProcessor : BaseOutputProcessor<int>
6+
{
7+
private int index = -1;
8+
private int lineCount = 0;
9+
private string[] buffer = new string[2];
10+
11+
public override void LineReceived(string line)
12+
{
13+
lineCount++;
14+
index = (index + 1) % 2;
15+
16+
if (line == null)
17+
{
18+
if (lineCount <= 2)
19+
{
20+
throw new InvalidOperationException("Not enough input");
21+
}
22+
23+
var output = buffer[index];
24+
25+
Logger.Trace("Processing: {0}", output);
26+
27+
var proc = new LineParser(output);
28+
proc.SkipWhitespace();
29+
proc.ReadUntilWhitespace();
30+
proc.ReadUntilWhitespace();
31+
proc.SkipWhitespace();
32+
33+
var bytes = int.Parse(proc.ReadUntilWhitespace().Replace(",", string.Empty));
34+
var kilobytes = bytes / 1024;
35+
RaiseOnEntry(kilobytes);
36+
}
37+
else
38+
{
39+
buffer[index] = line;
40+
}
41+
}
42+
}
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading;
2+
3+
namespace GitHub.Unity
4+
{
5+
class LinuxDiskUsageTask : ProcessTask<int>
6+
{
7+
private readonly string arguments;
8+
9+
public LinuxDiskUsageTask(NPath directory, CancellationToken token)
10+
: base(token, new LinuxDiskUsageOutputProcessor())
11+
{
12+
Name = "du";
13+
arguments = string.Format("-h \"{0}\"", directory);
14+
}
15+
16+
public override string ProcessName { get { return Name; } }
17+
public override string ProcessArguments { get { return arguments; } }
18+
public override TaskAffinity Affinity { get { return TaskAffinity.Concurrent; } }
19+
public override string Message { get; set; } = "Getting directory size...";
20+
}
21+
}

src/GitHub.Api/Platform/WindowsDiskUsageTask.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
namespace GitHub.Unity
55
{
6-
class WindowsDiskUsageTask : ProcessTask<List<string>>
6+
class WindowsDiskUsageTask : ProcessTask<int>
77
{
88
private readonly string arguments;
99

1010
public WindowsDiskUsageTask(NPath directory, CancellationToken token)
11-
: base(token, new SimpleListOutputProcessor())
11+
: base(token, new WindowsDiskUsageOutputProcessor())
1212
{
1313
Name = "cmd";
14-
arguments = "/c dir /a/s \"" + directory + "\"";
14+
arguments = string.Format("/c dir /a/s \"{0}\"", directory);
1515
}
1616

1717
public override string ProcessName { get { return Name; } }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections.Generic;
2+
using GitHub.Unity;
3+
using NUnit.Framework;
4+
5+
namespace UnitTests
6+
{
7+
[TestFixture]
8+
class LinuxDiskUsageOutputProcessorTests : BaseOutputProcessorTests
9+
{
10+
[Test]
11+
public void ParseOutput()
12+
{
13+
var output = new[]
14+
{
15+
"4 .git/lfs/objects/f7/0a",
16+
"4 .git/lfs/objects/f7",
17+
"44 .git/lfs/objects/f8/0c",
18+
"1 .git/lfs/objects/f8/1e",
19+
"45 .git/lfs/objects/f8",
20+
"176 .git/lfs/objects/fa/7c",
21+
"176 .git/lfs/objects/fa",
22+
"7716 .git/lfs/objects/fb/8d",
23+
"188 .git/lfs/objects/fb/ac",
24+
"7904 .git/lfs/objects/fb",
25+
"168 .git/lfs/objects/fd/39",
26+
"168 .git/lfs/objects/fd",
27+
"284 .git/lfs/objects/fe/50",
28+
"284 .git/lfs/objects/fe",
29+
"2556 .git/lfs/objects/ff/48",
30+
"1 .git/lfs/objects/ff/c6",
31+
"2557 .git/lfs/objects/ff",
32+
"4 .git/lfs/objects/incomplete",
33+
"0 .git/lfs/objects/logs",
34+
"628417 .git/lfs/objects",
35+
"0 .git/lfs/tmp/objects",
36+
"64 .git/lfs/tmp",
37+
"628481 .git/lfs",
38+
null
39+
};
40+
41+
AssertProcessOutput(output, 628481);
42+
}
43+
44+
private void AssertProcessOutput(IEnumerable<string> lines, int expected)
45+
{
46+
int? result = null;
47+
var outputProcessor = new LinuxDiskUsageOutputProcessor();
48+
outputProcessor.OnEntry += status => { result = status; };
49+
50+
foreach (var line in lines)
51+
{
52+
outputProcessor.LineReceived(line);
53+
}
54+
55+
Assert.IsTrue(result.HasValue);
56+
Assert.AreEqual(expected, result.Value);
57+
}
58+
}
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using GitHub.Unity;
3+
using NUnit.Framework;
4+
5+
namespace UnitTests
6+
{
7+
[TestFixture]
8+
class WindowsDiskUsageOutputProcessorTests : BaseOutputProcessorTests
9+
{
10+
[Test]
11+
public void WindowsDiskUsageOutput()
12+
{
13+
var output = new[]
14+
{
15+
"01/10/2018 09:11 AM 0 862394886",
16+
"05 / 10 / 2018 01:17 PM 0 880468387",
17+
"05 / 10 / 2018 02:12 PM 0 882539090",
18+
"05 / 08 / 2018 10:56 AM 0 890139785",
19+
"05 / 10 / 2018 02:12 PM 0 907039131",
20+
"01 / 10 / 2018 09:11 AM 0 909343522",
21+
"01 / 10 / 2018 09:11 AM 0 914279800",
22+
"03 / 08 / 2018 12:50 PM 0 935882723",
23+
"02 / 20 / 2018 10:40 AM 0 953135163",
24+
"01 / 10 / 2018 09:11 AM 0 956375995",
25+
"01 / 10 / 2018 09:11 AM 0 957028503",
26+
"01 / 10 / 2018 09:11 AM 0 957454540",
27+
"05 / 08 / 2018 10:56 AM 0 961987973",
28+
"01 / 10 / 2018 09:11 AM 0 972291688",
29+
"01 / 10 / 2018 09:11 AM 0 986253768",
30+
"03 / 07 / 2018 06:33 PM 0 991012201",
31+
"04 / 02 / 2018 02:39 PM<DIR> objects",
32+
"148 File(s) 0 bytes",
33+
"",
34+
@" Directory of C:\Users\Spade\Projects\GitHub\Unity\.git\lfs\tmp\objects",
35+
"",
36+
"04 / 02 / 2018 02:39 PM<DIR>.",
37+
"04 / 02 / 2018 02:39 PM<DIR>..",
38+
" 0 File(s) 0 bytes",
39+
"",
40+
" Total Files Listed:",
41+
" 409 File(s) 643,058,481 bytes",
42+
" 1325 Dir(s) 151,921,385,472 bytes free",
43+
null
44+
};
45+
46+
AssertProcessOutput(output, 627986);
47+
}
48+
49+
private void AssertProcessOutput(IEnumerable<string> lines, int expected)
50+
{
51+
int? result = null;
52+
var outputProcessor = new WindowsDiskUsageOutputProcessor();
53+
outputProcessor.OnEntry += status => { result = status; };
54+
55+
foreach (var line in lines)
56+
{
57+
outputProcessor.LineReceived(line);
58+
}
59+
60+
Assert.IsTrue(result.HasValue);
61+
Assert.AreEqual(expected, result.Value);
62+
}
63+
}
64+
}

src/tests/UnitTests/UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="IO\BranchListOutputProcessorTests.cs" />
7878
<Compile Include="Extensions\EnvironmentExtensionTests.cs" />
7979
<Compile Include="IO\LfsVersionOutputProcessorTests.cs" />
80+
<Compile Include="IO\LinuxDiskUsageOutputProcessorTests.cs" />
8081
<Compile Include="IO\LockOutputProcessorTests.cs" />
8182
<Compile Include="IO\CountObjectProcessorTests.cs" />
8283
<Compile Include="IO\VersionOutputProcessorTests.cs" />
@@ -92,6 +93,7 @@
9293
<Compile Include="IO\StatusOutputProcessorTests.cs" />
9394
<Compile Include="IO\MacBasedGitEnvironmentTests.cs" />
9495
<Compile Include="IO\LinuxBasedGitEnvironmentTests.cs" />
96+
<Compile Include="IO\WindowsDiskUsageOutputProcessorTests.cs" />
9597
<Compile Include="IO\WindowsGitEnvironmentTests.cs" />
9698
<Compile Include="Primitives\SerializationTests.cs" />
9799
<Compile Include="Primitives\UriStringTests.cs" />

0 commit comments

Comments
 (0)