-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProgram.cs
159 lines (146 loc) · 8.7 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using ProtoBuf;
namespace GitImporter
{
public enum TraceId
{
ReadExport = 1,
ReadCleartool,
CreateChangeSet,
ApplyChangeSet,
Cleartool
}
class Program
{
public static TraceSource Logger = new TraceSource("GitImporter", SourceLevels.All);
static void Main(string[] args)
{
Console.Error.WriteLine("GitImporter called with {0} arguments :", args.Length);
foreach (string arg in args)
Console.Error.WriteLine(" " + arg);
Logger.TraceData(TraceEventType.Information, 0, string.Format("GitImporter called with {0} arguments : {1}", args.Length, string.Join(" ", args)));
var importerArguments = new ImporterArguments();
if (!CommandLine.Parser.ParseArgumentsWithUsage(args, importerArguments))
return;
if (!importerArguments.CheckArguments())
{
Console.Error.WriteLine(CommandLine.Parser.ArgumentsUsage(typeof(ImporterArguments)));
return;
}
try
{
Logger.TraceData(TraceEventType.Start | TraceEventType.Information, 0, "Start program");
VobDB vobDB = null;
if (!string.IsNullOrEmpty(importerArguments.FetchFileContent))
{
using (var gitWriter = new GitWriter(importerArguments.ClearcaseRoot, importerArguments.NoFileContent, importerArguments.Labels))
{
if (File.Exists(importerArguments.ThirdpartyConfig))
{
var thirdPartyConfig = ThirdPartyConfig.ReadFromFile(importerArguments.ThirdpartyConfig);
var hook = new ThirdPartyHook(thirdPartyConfig);
gitWriter.PreWritingHooks.AddRange(hook.PreWritingHooks);
gitWriter.PostWritingHooks.AddRange(hook.PostWritingHooks);
}
gitWriter.WriteFile(importerArguments.FetchFileContent);
}
Logger.TraceData(TraceEventType.Stop | TraceEventType.Information, 0, "Stop program");
return;
}
if (importerArguments.LoadVobDB != null && importerArguments.LoadVobDB.Length > 0)
{
foreach (string vobDBFile in importerArguments.LoadVobDB)
{
using (var stream = new FileStream(vobDBFile, FileMode.Open))
if (vobDB == null)
vobDB = Serializer.Deserialize<VobDB>(stream);
else
vobDB.Add(Serializer.Deserialize<VobDB>(stream));
Logger.TraceData(TraceEventType.Information, 0, "Clearcase data successfully loaded from " + vobDBFile);
}
}
var exportReader = new ExportReader(importerArguments.OriginDate, importerArguments.Labels);
foreach (var file in importerArguments.ExportFiles)
exportReader.ReadFile(file);
List<ElementVersion> newVersions = null;
if (!string.IsNullOrWhiteSpace(importerArguments.DirectoriesFile) ||
!string.IsNullOrWhiteSpace(importerArguments.ElementsFile) ||
!string.IsNullOrWhiteSpace(importerArguments.VersionsFile))
using (var cleartoolReader = new CleartoolReader(importerArguments.ClearcaseRoot, importerArguments.OriginDate, importerArguments.Labels))
{
cleartoolReader.Init(vobDB, exportReader.Elements);
// first save of exportReader with oid (if something was actually read)
vobDB = cleartoolReader.VobDB;
if (importerArguments.ExportFiles.Length > 0 && !string.IsNullOrWhiteSpace(importerArguments.SaveVobDB))
{
using (var stream = new FileStream(importerArguments.SaveVobDB + ".export_oid", FileMode.Create))
Serializer.Serialize(stream, vobDB);
Logger.TraceData(TraceEventType.Information, 0, "Clearcase export with oid successfully saved in " + importerArguments.SaveVobDB + ".export_oid");
}
newVersions = cleartoolReader.Read(importerArguments.DirectoriesFile, importerArguments.ElementsFile, importerArguments.VersionsFile);
vobDB = cleartoolReader.VobDB;
if (!string.IsNullOrWhiteSpace(importerArguments.SaveVobDB))
{
using (var stream = new FileStream(importerArguments.SaveVobDB, FileMode.Create))
Serializer.Serialize(stream, vobDB);
Logger.TraceData(TraceEventType.Information, 0, "Clearcase data successfully saved in " + importerArguments.SaveVobDB);
}
}
if (!importerArguments.GenerateVobDBOnly)
{
HistoryBuilder historyBuilder = null;
// we only use an existing HistoryBuilder for incremental import, ie when newVersions != null
if (newVersions != null && !string.IsNullOrWhiteSpace(importerArguments.History) && File.Exists(importerArguments.History))
{
using (var stream = new FileStream(importerArguments.History, FileMode.Open))
historyBuilder = Serializer.Deserialize<HistoryBuilder>(stream);
Logger.TraceData(TraceEventType.Information, 0, "History data successfully loaded from " + importerArguments.History);
historyBuilder.Fixup(vobDB);
}
if (historyBuilder == null)
historyBuilder = new HistoryBuilder(vobDB);
// command-line arguments take precedence
historyBuilder.SetRoots(importerArguments.Roots);
historyBuilder.SetBranchFilters(importerArguments.Branches);
var changeSets = historyBuilder.Build(newVersions);
var branchRename = historyBuilder.GetBranchRename();
using (var gitWriter = new GitWriter(importerArguments.ClearcaseRoot, importerArguments.NoFileContent, importerArguments.Labels, branchRename))
{
if (File.Exists(importerArguments.IgnoreFile))
gitWriter.InitialFiles.Add(new Tuple<string, string>(".gitignore", importerArguments.IgnoreFile));
if (File.Exists(importerArguments.ThirdpartyConfig))
{
var thirdPartyConfig = ThirdPartyConfig.ReadFromFile(importerArguments.ThirdpartyConfig);
var hook = new ThirdPartyHook(thirdPartyConfig);
gitWriter.PreWritingHooks.AddRange(hook.PreWritingHooks);
gitWriter.PostWritingHooks.AddRange(hook.PostWritingHooks);
gitWriter.InitialFiles.Add(new Tuple<string, string>(".gitmodules", hook.ModulesFile));
}
gitWriter.WriteChangeSets(changeSets);
}
if (!string.IsNullOrWhiteSpace(importerArguments.History))
{
if (File.Exists(importerArguments.History))
File.Move(importerArguments.History, importerArguments.History + ".bak");
using (var stream = new FileStream(importerArguments.History, FileMode.Create))
Serializer.Serialize(stream, historyBuilder);
Logger.TraceData(TraceEventType.Information, 0, "History data successfully saved in " + importerArguments.History);
}
}
}
catch (Exception ex)
{
Logger.TraceData(TraceEventType.Critical, 0, "Exception during import : " + ex);
Console.Error.WriteLine("Exception during import : " + ex);
}
finally
{
Logger.TraceData(TraceEventType.Stop | TraceEventType.Information, 0, "Stop program");
Logger.Flush();
}
}
}
}