1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* update phar file timestamp to the last commit in the repository for binary reproduceable build |
4
|
|
|
* of phar-files. |
5
|
|
|
* |
6
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use Seld\PharUtils\Timestamps; |
10
|
|
|
|
11
|
|
|
echo "reset phar file timestamps to latest commit timestamp (reproducible builds)\n"; |
12
|
|
|
|
13
|
|
|
# seld/phar-utils via build requirements |
14
|
|
|
require __DIR__ . '/../../vendor/seld/phar-utils/src/Timestamps.php'; |
15
|
|
|
|
16
|
|
|
$projectDir = __DIR__ . '/../..'; |
17
|
|
|
|
18
|
|
|
$build = new SimpleXMLElement($projectDir . '/build.xml', 0, true); |
19
|
|
|
|
20
|
|
|
$file = $projectDir . '/' . $build['name'] . '.phar'; |
21
|
|
|
|
22
|
|
|
list($signature) = $build->xpath('//patched-pharpackage/@signature') + array(null); |
23
|
|
|
|
24
|
|
|
echo "Signature: ", $signature, " (from build.xml) \n"; |
25
|
|
|
$const = "Phar::" . strtoupper($signature); |
26
|
|
|
|
27
|
|
|
$sig = constant($const); |
28
|
|
|
echo "Phar Algo: ", var_export($sig, true), " ($const)\n"; |
29
|
|
|
|
30
|
|
|
if (!is_file($file) || !is_readable($file)) { |
31
|
|
|
throw new RuntimeException(sprintf('Is not a file or not readable: %s', var_export($file, true))); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$commitHash = `git log --format=format:%H HEAD -1`; |
35
|
|
|
$timestamp = (int) `git log --format=format:%ct HEAD -1`; |
36
|
|
|
printf("Commit...: %s\n", $commitHash); |
37
|
|
|
printf("Timestamp: %d (%s, date of commit)\n", $timestamp, date(DATE_RFC3339, $timestamp)); |
38
|
|
|
$threshold = 1343826993; # 2012-08-01T15:14:33Z |
|
|
|
|
39
|
|
|
if ($timestamp < $threshold) { |
40
|
|
|
throw new RuntimeException( |
41
|
|
|
sprintf('Timestamp older than %d (%s).', $threshold, date(DATE_RFC3339, $threshold)) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$tmp = $file . '.tmp'; |
46
|
|
|
|
47
|
|
|
if ($tmp !== $file && file_exists($tmp)) { |
48
|
|
|
unlink($tmp); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!rename($file, $tmp)) { |
52
|
|
|
throw new RuntimeException( |
53
|
|
|
sprintf('Failed to move file %s to %s', var_export($file, true), var_export($tmp, true)) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!is_file($tmp)) { |
58
|
|
|
throw new RuntimeException('No tempfile %s for reading', var_export($tmp, true)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$timestamps = new Timestamps($tmp); |
62
|
|
|
$timestamps->updateTimestamps($timestamp); |
63
|
|
|
$timestamps->save($file, $sig); |
64
|
|
|
|
65
|
|
|
echo "SHA1.....: ", sha1_file($file), "\nMD5......: ", md5_file($file), "\n"; |
66
|
|
|
|
67
|
|
|
if (!unlink($tmp)) { |
68
|
|
|
throw new RuntimeException('Error deleting tempfile %s', var_export($tmp, true)); |
69
|
|
|
} |
70
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.