forked from poseidix/TRAC-SVN-to-GIT-migration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertTracTickets.php
121 lines (96 loc) · 3.18 KB
/
convertTracTickets.php
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
<?php
/**
* This script converts the commit references from SVN IDs to GIT IDs, i.e. changing in all tickets
* [1234] to [a42v2e3] or whatever the corresponding GIT hash is
*
* It needs a SVN ID -> GIT ID lookup table file called lookupTable.txt to match IDs.
*
* Execute it with php.exe convertTracTickets.php
*
* Needs the sqlite3 extension enabled to access the TRAC database.
**/
error_reporting(E_ALL);
/* CONFIGURATION */
// Path to trac DB
$pathDB = "/var/www/trac-git/tracenv/db/trac.db";
// Path to lookup table (SVN revision number to GIT revion hash)
$pathLookupTable = "lookupTable.txt";
// Number of characters for the changeset hash. This has to be 4 <= nr <= 40
$nrHashCharacters = 40;
/* END CONFIGURATION */
/**
* Converts a text with references to an SVN revision [1234] into the corresponding GIT revision
*
* @param text Text to convert
* @param lookupTable Conversion table from SVN ID to Git ID
* @returns True if conversions have been made
*/
function convertSVNIDToGitID(&$text, $lookupTable, $nrHashCharacters)
{
// Extract references to SVN revisions [####]
$pattern = '/\[([0-9]+)\]/';
if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER) > 0)
{
foreach($matches as $match)
{
$svnID = $match[1];
if (!isSet($lookupTable[$svnID]))
{
echo "Warning: unknown GIT hash for SVN revision $svnID\n";
continue;
}
$gitID = substr($lookupTable[$svnID], 0, $nrHashCharacters);
$text = str_replace('[' . $svnID . ']', '[' . $gitID . '] (SVN r' . $svnID . ')', $text);
}
return true;
}
return false;
}
echo "Creating SVN -> GIT conversion table table...\n";
// Create the lookup table
$lines = file($pathLookupTable);
foreach ($lines as $line)
{
if (empty($line)) continue;
list ($svnID, $gitID) = explode("\t", trim($line));
$lookupTable[$svnID] = $gitID;
}
// Connect to the TRAC database
$db = new SQLite3($pathDB);
echo "Converting table 'ticket_change'...\n";
// Convert table 'ticket_change'
$result = $db->query('SELECT * FROM ticket_change');
$i = 1;
while ($row = $result->fetchArray())
{
$i++;
$oldValue = $db->escapeString($row['oldvalue']);
$newValue = $db->escapeString($row['newvalue']);
// Only update when there is something to be changed, since SQLite isn't the fastest beast around
if (convertSVNIDToGitID($oldValue, $lookupTable, $nrHashCharacters) || convertSVNIDToGitID($newValue, $lookupTable, $nrHashCharacters))
{
$query = "UPDATE ticket_change SET oldvalue='$oldValue', newvalue='$newValue' WHERE ticket = '${row['ticket']}' AND time = '${row['time']}' AND author='${row['author']}' AND field='${row['field']}'";
if (!$db->exec($query))
{
echo "Query failed: " . $query . "\n";
}
echo "Updated ticket_change $i\n";
}
}
echo "Converting table 'ticket'...\n";
// Convert table 'ticket'
$i = 1;
$result = $db->query('SELECT * FROM ticket');
while ($row = $result->fetchArray())
{
$description = $db->escapeString($row['description']);
if (convertSVNIDToGitID($description, $lookupTable, $nrHashCharacters))
{
$query = "UPDATE ticket SET description='$description' WHERE id = " . $row['id'];
$db->exec($query);
echo "Updated ticket $i\n";
}
}
// Done :)
echo "Done!\n";
?>