-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitDiffToGeojson.cs
144 lines (117 loc) · 4.87 KB
/
GitDiffToGeojson.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System.Diagnostics;
using System.Text;
namespace prepareBikeParking
{
public static class GitDiffToGeojson
{
public static (List<string>, List<string>) LatestVsPrevious()
{
var diffOutput = RunGitDiffCommand("0");
var (addedlines, removedObjects) = ExtractDiffedLines(diffOutput);
return (addedlines, removedObjects);
}
public static string GetLastCommittedVersion()
{
string command = "show HEAD:../../../bikeshare.geojson";
string arguments = "";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = $"{command} {arguments}",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8, // Ensure UTF-8 encoding
StandardErrorEncoding = Encoding.UTF8 // Ensure UTF-8 encoding for error output
};
StringBuilder output = new StringBuilder();
StringBuilder errorOutput = new StringBuilder();
using (Process process = Process.Start(startInfo))
{
if (process != null)
{
while (!process.StandardOutput.EndOfStream)
{
output.AppendLine(process.StandardOutput.ReadLine());
}
while (!process.StandardError.EndOfStream)
{
errorOutput.AppendLine(process.StandardError.ReadLine());
}
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Git command failed: {errorOutput}");
}
}
}
return output.ToString();
}
public static (List<string>, List<string>) Compare(string @new = "HEAD", string old = "")
{
var diffOutput = RunGitDiffCommand(@new, old);
var (addedlines, removedObjects) = ExtractDiffedLines(diffOutput);
return (addedlines, removedObjects);
}
private static string RunGitDiffCommand(string @new = "HEAD", string old = "")
{
string command = $"--no-pager diff --unified=0 {@new} {old} \"../../../bikeshare.geojson\"";
string arguments = "";
Console.WriteLine($"Running git {command} {arguments}");
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = $"{command} {arguments}",
RedirectStandardOutput = true,
RedirectStandardError = true, // Add this line to redirect the error stream
UseShellExecute = false,
CreateNoWindow = true
};
StringBuilder output = new StringBuilder();
StringBuilder errorOutput = new StringBuilder(); // Create a StringBuilder to store the error output
// Start the process
using Process process = Process.Start(startInfo);
if (process != null)
{
// Read the output
while (!process.StandardOutput.EndOfStream)
{
output.AppendLine(process.StandardOutput.ReadLine());
}
// Read the error output
while (!process.StandardError.EndOfStream)
{
errorOutput.AppendLine(process.StandardError.ReadLine());
}
// Wait for the process to finish
process.WaitForExit();
// Output the result
Console.WriteLine(output.ToString());
// Output the error result
Console.WriteLine(errorOutput.ToString());
}
var result = output.ToString();
return result;
}
private static (List<string>, List<string>) ExtractDiffedLines(string gitDiffInput)
{
var lines = gitDiffInput.Split("\n");
var addedObjects =
lines
.Where(x =>
x.StartsWith("+\u001e{"))
.Select(x =>
x.Replace("+\u001e{", "{"))
.ToList();
var removedObjects =
lines
.Where(x =>
x.StartsWith("-\u001e{"))
.Select(x =>
x.Replace("-\u001e{", "{"))
.ToList();
return (addedObjects, removedObjects);
}
}
}