forked from sdboyer/drupalorg-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-cleanup.php
100 lines (83 loc) · 3.21 KB
/
import-cleanup.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
#!/usr/bin/php
<?php
/**
* @file
* Strip translations and keywords from a project.
*/
$options = getopt('ktd:');
// Make sure we get a git directory to cleanup.
if (empty($options['d'])) {
echo "Cleanup script not passed a directory\n";
exit(1);
}
putenv('[email protected]');
putenv('GIT_AUTHOR_NAME=The Great Git Migration');
// Load shared functions.
require_once dirname(__FILE__) . '/shared.php';
$destination_dir = realpath($options['d']);
$project = basename($destination_dir);
// Core is different. We can't strip translations from it.
if ($project == 'drupal.git') {
unset($options['t']);
}
// Create a temporary directory, and register a clean up.
$cmd = 'mktemp -dt cvs2git-import-' . escapeshellarg($project) . '.XXXXXXXXXX';
$temp_dir = realpath(trim(`$cmd`));
register_shutdown_function('_clean_up_import', $temp_dir);
git_invoke("git clone $destination_dir $temp_dir");
try {
$all_branches = git_invoke("ls " . escapeshellarg("$destination_dir/refs/heads/"));
$all_branches = array_filter(explode("\n", $all_branches)); // array-ify & remove empties
}
catch (Exception $e) {
git_log("Branch list retrieval failed with error '$e'.", 'WARN', $project);
}
foreach($all_branches as $name) {
if ($name != 'master') {
git_invoke("git checkout -t origin/$name", FALSE, "$temp_dir/.git", $temp_dir);
}
else {
git_invoke("git checkout $name", FALSE, "$temp_dir/.git", $temp_dir);
}
// If needed, cleanup keywords.
if (isset($options['k'])) {
try {
strip_cvs_keywords($project, $temp_dir);
}
catch (exception $e) {
git_log("CVS tag removal for branch $name failed with error '$e'", 'WARN', $project);
}
}
// If needed, cleanup translations.
if (isset($options['t'])) {
try {
kill_translations($project, $temp_dir);
}
catch (exception $e) {
git_log("Translation removal for branch $name failed with error '$e'", 'WARN', $project);
}
}
}
git_invoke('git push', FALSE, "$temp_dir/.git");
// ------- Utility functions -----------------------------------------------
function strip_cvs_keywords($project, $directory) {
passthru('./strip-cvs-keywords.py ' . escapeshellarg($directory));
$commit_message = escapeshellarg("Stripping CVS keywords from $project");
if (git_invoke('git status --untracked-files=no -sz --', TRUE, "$directory/.git", $directory)) {
git_invoke("git commit -a -m $commit_message", FALSE, "$directory/.git", $directory);
}
}
function kill_translations($project, $directory) {
$directories = git_invoke('find ' . escapeshellarg($directory) . ' -name translations -type d');
$translations = array_filter(explode("\n", $directories)); // array-ify & remove empties
$directories = git_invoke('find ' . escapeshellarg($directory) . ' -name po -type d');
$po = array_filter(explode("\n", $directories)); // array-ify & remove empties
$directories = array_merge($translations, $po);
if (!empty($directories)) {
$commit_message = escapeshellarg("Removing translation directories from $project");
foreach ($directories as $dir) {
git_invoke("git rm -r $dir", FALSE, "$directory/.git", $directory);
}
git_invoke("git commit -a -m $commit_message", FALSE, "$directory/.git", $directory);
}
}