This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
piston-export
executable file
·134 lines (103 loc) · 3.87 KB
/
piston-export
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
#!/usr/bin/env php
<?php
if(!isset($_SERVER['argv'][2])) {
echo "Usage: {$_SERVER['argv'][0]} (piston dir) (dest repository) (dest branch)
For example:
cd ~/Sites/yourproject
piston-export sapphire ~/Sites/gitdev/sapphire newchanges
If you have checked an external into your git project using piston, and then made changes to it, you
need a way of getting those changes back.
This script will export all of those changes to a new git branch of a working copy or if the option
`--patches-only` is passed, then a list of patch files will be produced. Where possible, it leaves
the hard work to git.\n";
exit(1);
}
$pistonDir = $_SERVER['argv'][1];
$pistonDirFull = realpath($pistonDir);
$destRepos = realpath($_SERVER['argv'][2]);
$destBranch = isset($_SERVER['argv'][3]) ? $_SERVER['argv'][3] : 'master';
$args = array(
'patches-only' => false
);
// check flags
foreach($_SERVER['argv'] as $arg) {
if(substr($arg, 0, 2) == "--") {
$key = substr($arg, 2);
$value = (strpos($arg, "=") !== false) ? substr($arg, strpos($arg, "=")) : true;
if(isset($args[$key])) $args[$key] = $value;
else {
echo "unknow flag $arg";
exit(1);
}
}
}
if(!file_exists("$pistonDir/.piston.yml")) {
echo "$pistonDir not a piston external ($pistonDir/.piston.yml not found)\n";
exit(2);
}
if(!file_exists("$destRepos/.git") && !$args['patches-only']) {
echo "$destRepos not a piston external ($destRepos/.git not found)\n";
exit(3);
}
if(file_exists("$destRepos/.git/rebase-apply")) {
echo "You should call 'git --git-dir=$destRepos/.git am --abort' on your dest repository first\n";
exit(4);
}
require_once('Spyc.php');
$parser = new Spyc;
$config = $parser->load("$pistonDirFull/.piston.yml");
if(isset($config['exported_to'])) {
$logFilter = $config['exported_to'] . "..HEAD";
} else {
$logFilter = "";
}
$allHashes = explode("\n", trim(`git log --pretty="%H" $logFilter $pistonDir`));
$hashesToIgnore = explode("\n", trim(`git log --pretty="%H" $logFilter $pistonDir/.piston.yml`));
if($allHashes == array("")) $allHashes = array();
if($hashesToIgnore == array("")) $hashesToIgnore = array();
if(!$allHashes) {
if(isset($config['exported_to'])) {
echo "There weren't any more patches to apply since {$config['exported_to']}\n";
} else {
echo "There weren't any patches to apply\n";
}
exit(0);
}
$patchDirBase = "$destRepos/piston-export-" . date('Ymd');
$patchDir = $patchDirBase;
$suffix = 1;
while(file_exists($patchDir)) {
$suffix++;
$patchDir = "$patchDirBase-$suffix";
}
mkdir($patchDir);
$hashesToInclude = array_diff($allHashes, $hashesToIgnore);
$hashesToInclude = array_reverse($hashesToInclude);
$relativePistonDir = trim(`cd $pistonDir && git rev-parse --show-prefix`);
foreach($hashesToInclude as $i => $hash) {
$patchFile = str_pad($i, 5, 0, STR_PAD_LEFT) . '.patch';
echo "Creating patch for $hash -> $patchDir/$patchFile\n";
`git format-patch --relative=$relativePistonDir --stdout $hash~..$hash > $patchDir/$patchFile`;
}
if(!$args['patches-only']) {
// Update config without the full destructive powers of Spyc
$yaml = file_get_contents("$pistonDirFull/.piston.yml");
$newExportedTo = trim(`git rev-parse HEAD`);
$replacedSuccessfully = false;
$yaml = preg_replace('/exported_to: ([a-zA-Z0-9]+)/', "exported_to: $newExportedTo", $yaml, -1, $replacedSuccessfully);
if(!$replacedSuccessfully) {
$yaml .= "\nexported_to: $newExportedTo";
}
file_put_contents("$pistonDirFull/.piston.yml", $yaml);
// Apply the patches
chdir($destRepos);
if(!trim(`git branch | grep $destBranch`)) {
echo "Creating dest branch '$destBranch'...\n";
`git checkout -b $destBranch`;
} else {
echo "Checking out dest branch '$destBranch'...\n";
`git checkout $destBranch`;
}
echo "Applying patches (git am --reject --ignore-whitespace --whitespace=nowarn -p 1 $patchDir/*)...\n";
passthru("git am --reject --ignore-whitespace --whitespace=nowarn -p 1 $patchDir/*");
}