forked from markrcote/bugzilla-git-migration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastImportRewriter.cs
364 lines (311 loc) · 12.8 KB
/
FastImportRewriter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Fast-Import / Fast-Export Rewriter to migrate bazaar bugtracking metdata
// properties to git.
// More info:
// http://www.fusonic.net/en/blog/2013/03/26/migrating-from-bazaar-to-git/
//
// Licence: MIT X11 / BSD
//
// This version is customized for the Bugzilla project.
//
// Pipe the output from `bzr fast-export --no-plain --git-branch=<branch>`
// into this script, and the output of this script into `git fast-import`.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace migrate
{
class FastImportRewriter
{
class Rename
{
public string OrigName { get; set; }
public string NewName { get; set; }
}
static readonly Regex dataLengthRegex = new Regex("data ([0-9]+)?", RegexOptions.Compiled);
static readonly Regex bugLengthRegex = new Regex("property bugs ([0-9]+)", RegexOptions.Compiled);
static int blockSize = 1024;
static CommitPart commitPart;
static Stream outputStream;
public static void Main(string[] args)
{
using (outputStream = Console.OpenStandardOutput())
{
using (LineReader inputStream = new LineReader(Console.OpenStandardInput()))
{
string s = null;
while ((s = inputStream.ReadLine()) != null)
{
if (ShouldSkipLine(s))
continue;
else if (s.StartsWith("reset "))
{
if (commitPart != null)
commitPart.Flush();
commitPart = new CommitPart();
commitPart.Reset = s;
}
else if (s.StartsWith("commit "))
{
if (commitPart != null)
commitPart.Flush();
commitPart = new CommitPart();
commitPart.Commit = s;
}
else if (s.StartsWith("mark "))
commitPart.Mark = s;
else if (s.StartsWith("committer "))
commitPart.Committer = s;
else if (s.StartsWith("author "))
commitPart.Author = s;
else if (s.StartsWith("data ") && (commitPart != null && commitPart.MessageOrig == null))
{
int dataLength = ParseDataLength(s);
commitPart.MessageOrig = ReadDataBlockAsString(inputStream, dataLength);
}
else if (s.StartsWith("from "))
commitPart.From = s;
else if (s.StartsWith("merge "))
commitPart.Merge = s;
else if (s.StartsWith("property bugs "))
{
int bugLength = ParseBugLength(s);
int bugHeadLength = ("property bugs " + bugLength.ToString() + " ").Length;
commitPart.Bug = s.Substring(bugHeadLength);
if (s.Length - bugHeadLength < bugLength)
commitPart.Bug += "\n" + ReadDataBlockAsString(inputStream, bugLength - (s.Length - bugHeadLength));
}
else if (s.StartsWith("R "))
{
string origName = s.Substring(2, s.IndexOf(" ", 2) - 2);
string newName = s.Substring(s.IndexOf(" ", 2) + 1);
// If directory, track the new name.
if (IsDirectory(origName))
dirs.Add(newName);
else
{
Rename rename = new Rename();
rename.OrigName = origName;
rename.NewName = newName;
commitPart.Renames.Add(rename);
}
}
else if (s.StartsWith("D "))
{
if (!IsDirectory(s.Substring(2)))
commitPart.Deletes.Add(s);
}
else if (s.StartsWith("M "))
{
if (s.StartsWith("M 040000"))
{
// track directories
dirs.Add(s.Substring(11));
// Skip Directory
continue;
}
//Reached end of commit block, now rewrite the commit and output the data
if (commitPart != null)
{
commitPart.Flush();
commitPart = null;
}
WriteLine(s);
}
else if (s.StartsWith("data ") && commitPart == null)
{
int dataLength = ParseDataLength(s);
WriteLine(s);
int current = dataLength;
while (current - blockSize > 0)
{
var data = ReadDataBlock(inputStream, 1024);
Write(data);
current = current - blockSize;
}
if (current > 0)
{
var data = ReadDataBlock(inputStream, current);
Write(data);
}
WriteLine(string.Empty);
}
else
Console.Error.WriteLine("Skipping: " + s);
}
}
if (commitPart != null)
commitPart.Flush();
}
}
private static bool IsDirectory(string s)
{
bool isDir = dirs.Contains(s);
return isDir;
}
private static readonly HashSet<string> dirs = new HashSet<string>();
static void Write(byte[] buffer)
{
outputStream.Write(buffer, 0, buffer.Length);
}
static readonly byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
static void WriteLine(string s)
{
byte[] bytes = System.Text.UTF8Encoding.Default.GetBytes(s);
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Write(newline, 0, newline.Length);
}
static string ReadDataBlockAsString(LineReader inputStream, int dataLength)
{
return System.Text.UTF8Encoding.Default.GetString(inputStream.ReadBytes(dataLength));
}
static byte[] ReadDataBlock(LineReader inputStream, int dataLength)
{
byte[] buffer = inputStream.ReadBytes(dataLength);
return buffer;
}
static int ParseDataLength(string s)
{
var match = dataLengthRegex.Match(s);
if (match.Success)
return int.Parse(match.Groups[1].Value);
return 0;
}
static int ParseBugLength(string s)
{
var match = bugLengthRegex.Match(s);
if (match.Success)
return int.Parse(match.Groups[1].Value);
return 0;
}
static bool ShouldSkipLine(string s)
{
bool skip = s == string.Empty || s.StartsWith("feature") || s.StartsWith("property branch-nick");
return skip;
}
class CommitPart
{
static readonly Regex origBugIdRegex = new Regex(@"[bB]ug\s+([0-9]+)", RegexOptions.Compiled);
static readonly Regex bugsRegex = new Regex("https://bugzilla.mozilla.org/show_bug.cgi\\?id=([0-9]+)", RegexOptions.Compiled);
public CommitPart()
{
Deletes = new List<string>();
Renames = new List<Rename>();
}
public string Reset { get; set; }
public string Commit { get; set; }
public string Mark { get; set; }
public string Committer { get; set; }
public string Author { get; set; }
public string MessageOrig { get; set; }
public string MessageNew { get; private set; }
public string From { get; set; }
public string Merge { get; set; }
public string Bug { get; set; }
public List<string> Deletes
{
get;
private set;
}
public List<Rename> Renames
{
get;
private set;
}
public void RewriteMessageWithBug()
{
if (MessageOrig == null)
return;
if (Bug == null)
{
MessageNew = "data " + System.Text.UTF8Encoding.Default.GetByteCount(MessageOrig).ToString() + "\n" + MessageOrig;
return;
}
string messageAddendum = "";
HashSet<string> bugsInMessage = new HashSet<string>();
foreach (Match match in origBugIdRegex.Matches(MessageOrig))
bugsInMessage.Add(match.Groups[1].Value);
foreach (Match match in bugsRegex.Matches(Bug))
{
if (!bugsInMessage.Contains(match.Groups[1].Value))
{
if (messageAddendum == "" && MessageOrig[MessageOrig.Length-1] != '\n')
messageAddendum = "\n";
messageAddendum += "\n" + match.Value;
}
}
string newMessageString = MessageOrig + messageAddendum;
int byteLength = System.Text.UTF8Encoding.Default.GetByteCount(newMessageString);
MessageNew = "data " + byteLength.ToString() + "\n" + newMessageString;
}
public void Flush()
{
commitPart.RewriteMessageWithBug();
if (Reset != null)
{
var l = "reset refs/tags/".Length;
string reset = Reset.Substring(0, l) + Reset.Substring(l, Reset.Length - l).Replace(' ', '_');
WriteLine(reset);
WriteLine(From);
WriteLine(string.Empty);
return;
}
WriteLine(Commit);
WriteLine(Mark);
if (Author != null)
WriteLine(Author);
WriteLine(Committer);
WriteLine(MessageNew);
if (From != null)
WriteLine(From);
if (Merge != null)
WriteLine(Merge);
foreach (Rename renamed in Renames)
{
bool origDeleted = false;
foreach (string deleted in Deletes)
{
if (deleted.Substring(2) == renamed.OrigName)
{
origDeleted = true;
break;
}
}
if (!origDeleted)
WriteLine("R " + renamed.OrigName + " " + renamed.NewName);
}
foreach (string deleted in Deletes)
WriteLine(deleted);
}
}
}
public class LineReader : BinaryReader
{
public LineReader(Stream stream)
: base(stream, Encoding.UTF8)
{
}
static readonly byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
public string ReadLine()
{
List<byte> buffer = new List<byte>(64);
try
{
while (true)
{
byte lastByte = base.ReadByte();
if (lastByte == newline[0])
return System.Text.UTF8Encoding.Default.GetString(buffer.ToArray());
buffer.Add(lastByte);
}
}
catch (EndOfStreamException)
{
return null;
}
}
}
}