forked from juj/GitSourceMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
162 lines (142 loc) · 7.65 KB
/
Program.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Web;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
namespace GitSourceMonitor
{
class Program
{
// Given time since Unix epoch time, returns a corresponding DateTime.
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
static void Main(string[] args)
{
List<string> commits = new List<string>();
if (args.Length == 0)
{
Console.WriteLine("Usage: GitSourceMonitor <branchname> [outputfilename]");
Console.WriteLine(" <branchname>: The name of the git branch to add to a SourceMonitor project.");
Console.WriteLine(" [outputfilename]: The name of the output SourceMonitor .smproj file to use. "
+ "If not specified, the file \"GitSourceMonitorProject.smproj\" will be used.");
return;
}
// Run 'git log' to get all the commits in the repository.
// Assuming the tool is run with cwd inside the git repository.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "git";
// Use a special '|||||' as a delimiter so that we can split the results easily below.
startInfo.Arguments = "log " + args[0] + " --format=\"%H|||||%s|||||%at\" --no-color --no-merges";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true; // Bug: For some reason, when invoking git, it creates a window even if this is true.
Process proc = Process.Start(startInfo);
try
{
proc.Start();
while( !proc.StandardOutput.EndOfStream )
{
string line = proc.StandardOutput.ReadLine();
commits.AddRange(line.Split('\n').ToList());
}
proc.WaitForExit();
}
catch(Exception e)
{
throw;
}
string outputProjectFile = args.Length >= 2 ? args[1] : "GitSourceMonitorProject";
outputProjectFile += "_" + args[0] + ".smproj";
// Add checkpoints from oldest to newest. The log file had from newest to oldest.
commits.Reverse();
// Check if we have an old GitSourceMonitor-generated project in the folder.
DateTime projectLastModifiedDate = new DateTime(1970,1,1,0,0,0,0);
if (File.Exists(outputProjectFile))
projectLastModifiedDate = new FileInfo(outputProjectFile).CreationTime;
// Parse all commit lines to count exactly how many commits we have to process.
List<string> parsedCommits = new List<string>();
foreach (string s in commits)
{
List<string> items = Regex.Split(s, "\\|\\|\\|\\|\\|").ToList();
if (items.Count != 3)
continue;
parsedCommits.Add(s);
}
commits = parsedCommits;
int n = 1;
foreach (string s in commits)
{
List<string> items = Regex.Split(s, "\\|\\|\\|\\|\\|").ToList();
if (items.Count != 3)
continue;
string commitHash = items[0];
string message = items[1].Replace("<", "<").Replace(">", ">");
DateTime commitTime = UnixTimeStampToDateTime(double.Parse(items[2]));
string commitTimeString = commitTime.ToString("yyyy-MM-ddTHH:mm:ss");
if (commitTime < projectLastModifiedDate)
{
Console.WriteLine("Skipping already added commit (" + (n++) + "/" + commits.Count + "): " + commitTimeString + ": " + message);
continue;
}
// Check out the commit so that SourceMonitor can process it.
Console.WriteLine("Processing commit (" + (n++) + "/" + commits.Count + "): " + commitTimeString + ": " + message);
startInfo = new ProcessStartInfo();
startInfo.FileName = "git";
startInfo.Arguments = "checkout " + commitHash;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true; // Bug: For some reason, when invoking git, it creates a window even if this is true.
proc = Process.Start(startInfo);
proc.WaitForExit();
// Create the command file for SourceMonitor to process. The new file will instruct SourceMonitor
// to create a new checkpoint to the project.
using( TextWriter writer = File.CreateText("gitsm_temp_checkpoint.xml") )
{
writer.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n" +
"<sourcemonitor_commands>\n" +
"\t<write_log>true</write_log>\n" +
"\t<command>\n" +
"\t\t<project_file>" + outputProjectFile + "</project_file>\n" +
"\t\t<checkpoint_name>" + message + "</checkpoint_name>\n" +
"\t\t<checkpoint_date>" + commitTimeString + "</checkpoint_date>\n" +
"\t\t<project_language>C++</project_language>\n" +
"\t\t<modified_complexity>true</modified_complexity>\n" +
"\t\t<source_directory>.</source_directory>\n" +
"\t\t<parse_utf8_files>True</parse_utf8_files>\n" +
"\t\t<show_measured_max_block_depth>True</show_measured_max_block_depth>\n" +
"\t\t<file_extensions>*.h,*.cpp,*.inl</file_extensions>\n" +
"\t\t<include_subdirectories>true</include_subdirectories>\n" +
"\t\t<ignore_headers_footers>2 DOC only</ignore_headers_footers>\n" +
"\t\t<ignore_headers_footers>false</ignore_headers_footers>\n" +
"\t</command>\n" +
"</sourcemonitor_commands>\n");
writer.Flush();
writer.Close();
}
// Run SourceMonitor.
startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Program Files (x86)\SourceMonitor\SourceMonitor.exe";
startInfo.Arguments = "/C gitsm_temp_checkpoint.xml";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true; // Bug: For some reason, when invoking git, it creates a window even if this is true.
proc = Process.Start(startInfo);
proc.WaitForExit();
File.SetCreationTime(outputProjectFile, commitTime);
}
// Don't leave temp files lying around.
File.Delete("gitsm_temp_checkpoint.xml");
}
}
}