-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
executable file
·102 lines (77 loc) · 3.23 KB
/
Copy pathexport.php
File metadata and controls
executable file
·102 lines (77 loc) · 3.23 KB
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
#!/usr/bin/env php
<?php
// Manual Git export maintenance: initialization, recovery and verification.
//
// php export.php enable [repository]
// php export.php initialize [repository]
// php export.php recover [repository]
// php export.php verify [repository]
if(php_sapi_name() != 'cli') die("This program is CLI-only.\n");
$_ENV = getenv("ENV") ?: 'prod';
require_once __DIR__ . "/core.php";
function say($message) { fwrite(STDOUT, "$message\n"); }
function bail($message) { fwrite(STDERR, "$message\n"); exit(1); }
$command = @$argv[1];
$repository = @$argv[2] ?? \export\repository();
if(!is_str($repository) && $command !== null)
bail("No repository given; pass a path or configure developer.git-repository.");
try {
switch($command) {
// The full enable lifecycle, mirroring the Settings action: create the
// repository if needed, commit the baseline, store the config rows and
// commit the enabled state.
case 'enable':
\export\create_repository($repository);
$repository = realpath($repository);
\export\validate($repository);
\export\lock($repository);
if(\export\enabled()) {
$current = \export\repository();
if(!is_str($current) || realpath($current) != $repository)
bail("Git export is already enabled for '$current'; disable it first.");
}
if(\export\initialized($repository)) \export\recover($repository);
else \export\initialize_baseline($repository);
\store\update_config('developer.git-repository', $repository);
\store\update_config('developer.git-enabled', 'true');
\store\put_audit_log('config', 'developer', "Enabled Git export.", 'system');
\export\commit_state($repository, "Enable Git export");
say("Git export enabled, committing into $repository.");
break;
case 'initialize':
\export\create_repository($repository);
\export\validate($repository);
\export\lock($repository);
if(\export\initialized($repository)) bail("The repository is already initialized.");
\export\initialize_baseline($repository);
say("Baseline committed, export initialized.");
break;
case 'recover':
\export\validate($repository);
\export\lock($repository);
$pending = \export\read_pending($repository);
if(!$pending) {
if(!\export\worktree_clean($repository))
bail("The worktree is dirty but no export is pending, refusing to guess.");
say("Nothing to recover.");
break;
}
$committed = \export\commit_state($repository, "{$pending['method']} {$pending['path']}");
\export\clear_pending($repository);
say($committed ? "Recovered, pending changes are committed." : "Recovered, no changes were left to commit.");
break;
case 'verify':
\export\validate($repository);
\export\lock($repository);
$differences = \export\verify($repository);
if(!$differences) { say("The export repository matches the database."); break; }
foreach($differences as $difference) say($difference);
exit(1);
default:
say("Usage: php export.php <enable|initialize|recover|verify> [repository]");
exit(64);
}
}
catch(Throwable $error) {
bail($error->getMessage());
}