forked from danielbachhuber/git-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-switch.php
More file actions
418 lines (344 loc) · 10.2 KB
/
git-switch.php
File metadata and controls
418 lines (344 loc) · 10.2 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/*
* Plugin Name: Git Switch
* Plugin URI: http://danielbachhuber.com
* Description: Switch your theme between Git branches.
* Author: Daniel Bachhuber
* Version: 1.1.0
* Author URI: http://danielbachhuber.com
*/
class Git_Switch {
private static $instance;
private $capability = 'switch_themes';
const CACHE_KEY = 'git-switch-status';
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Git_Switch;
self::$instance->load();
}
return self::$instance;
}
/**
* Load the plugin
*/
private function load() {
if ( ! function_exists( 'exec' ) ) {
return false;
}
add_action( 'wp_ajax_git-switch-branch', array( $this, 'handle_switch_branch_action' ) );
add_action( 'admin_bar_menu', array( $this, 'action_admin_bar_menu' ), 999 );
$this->handle_deploy();
add_action( 'init', function() {
$maybe_purge_cache = get_transient( 'force_purge_cache' );
if ( ! $maybe_purge_cache ) {
return;
}
delete_transient( 'force_purge_cache' );
$this->purge_cache();
});
$clean_link = function() {
?>
<style>
#wp-admin-bar-git-switch-details > a {
height: auto !important;
}
#wp-admin-bar-git-switch-details > a:hover {
color: #eee !important;
}
#wp-admin-bar-git-switch-branches-default {
max-height: 80vh;
overflow: scroll;
}
</style>
<?php
};
add_action( 'admin_head', $clean_link );
add_action( 'wp_head', $clean_link );
}
/**
* Purge cache for current site or entire multisite network.
*/
protected function purge_cache() {
if ( is_multisite() ) {
$sites = get_sites();
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$this->handle_cache_purging();
restore_current_blog();
}
} else {
$this->handle_cache_purging();
}
}
/**
* Actually purge some cache.
*/
protected function handle_cache_purging() {
// Purge The7 dynamic css.
if ( function_exists( 'presscore_refresh_dynamic_css' ) ) {
presscore_refresh_dynamic_css();
}
// Purge Elementor cache.
if ( class_exists( 'Elementor\Plugin' ) ) {
\Elementor\Plugin::$instance->files_manager->clear_cache();
}
}
/**
* Handle the action to switch a branch
*/
public function handle_switch_branch_action() {
$nonce = filter_input( INPUT_GET, 'nonce' );
$branch = filter_input( INPUT_GET, 'branch' );
$repo = filter_input( INPUT_GET, 'repo' );
if ( ! current_user_can( $this->capability ) || ! wp_verify_nonce( $nonce, "git-switch-branch-{$repo}-{$branch}" ) ) {
wp_die( "You can't do this." );
}
if ( ! $status = $this->get_repo_data( $repo ) ) {
wp_die( "Can't interact with Git." );
}
$this->execute_git_command( $repo, sprintf( 'git checkout -f %s; git submodule update --init', escapeshellarg( $branch ) ) );
$this->purge_wp_cache();
$this->purge_git_branch_cache( $repo );
$this->schedule_third_party_cache_purging();
do_action( 'git_switch_branch', $branch, $repo );
wp_safe_redirect( wp_get_referer() );
exit;
}
/**
* Display helpful details in the admin bar
*/
public function action_admin_bar_menu( $wp_admin_bar ) {
if ( ! current_user_can( $this->capability ) ) {
return;
}
foreach( $this->get_repos() as $repo ) {
$status = $this->get_repo_data( $repo );
if ( ! $status ) {
continue;
}
$sanitize_repo = sanitize_key( $repo );
$top_menu_id = 'git-switch-' . $sanitize_repo;
$pull_menu_id = 'git-pull-' . $sanitize_repo;
$switch_branch_menu_id = 'git-switch-branches-' . $sanitize_repo;
$wp_admin_bar->add_menu( array(
'id' => $top_menu_id,
'title' => sprintf( '%s(%s)%s', basename( $repo ), $status['branch'], $status['dirty'] ),
'href' => '#'
) );
if ( ! empty( $status['remote'] ) ) {
$wp_admin_bar->add_menu( array(
'parent' => $top_menu_id,
'id' => $pull_menu_id,
'title' => 'Pull changes',
'href' => add_query_arg( [ 'repo' => $repo, 'git-pull' => GIT_SWITCH_DEPLOY_SECRET ] ),
) );
$wp_admin_bar->add_menu( array(
'parent' => $top_menu_id,
'id' => $switch_branch_menu_id,
'title' => 'Switch branch:',
'href' => '#'
) );
foreach( $status['remote'] as $remote_branch ) {
// Do not display HEAD branch.
if ( strpos( $remote_branch, 'HEAD ' ) !== false ) {
continue;
}
$query_args = array(
'action' => 'git-switch-branch',
'repo' => $repo,
'branch' => $remote_branch,
'nonce' => wp_create_nonce( "git-switch-branch-{$repo}-{$remote_branch}" ),
);
$branch_switch_url = add_query_arg( $query_args, admin_url( 'admin-ajax.php' ) );
$title = esc_html( $remote_branch );
if ( $remote_branch === $status['branch'] ) {
$title = '* ' . $title;
}
$wp_admin_bar->add_menu( array(
'parent' => $switch_branch_menu_id,
'id' => $switch_branch_menu_id . '-' . sanitize_key( $remote_branch ),
'title' => $title,
'href' => esc_url( $branch_switch_url ),
) );
}
}
}
}
/**
* Get the current Git status
*/
public function get_repo_data( $repo ) {
if ( ! in_array( $repo, $this->get_repos() ) ) {
return false;
}
$cache_status = array_filter( (array) get_transient( self::CACHE_KEY ) );
if ( isset( $cache_status[ $repo ] ) ) {
return $cache_status[ $repo ];
}
$status = $this->execute_git_command( $repo, 'git status' );
if ( empty( $status ) or ( false !== strpos( $status[0], 'fatal:' ) ) ) {
return false;
}
$end = end( $status );
$return = array(
'dirty' => '*',
'branch' => 'detached',
'status' => $status,
'remote' => array(),
);
if ( preg_match( '/On branch (.+)$/', $status[0], $matches ) ) {
$return['branch'] = trim( $matches[1] );
}
if ( empty( $end ) or ( false !== strpos( $end, 'nothing to commit' ) ) ) {
$return['dirty'] = '';
}
$branches = $this->execute_git_command( $repo, 'git branch -r --sort=-committerdate' );
if ( ! empty( $branches ) ) {
$branches = array_map( function( $branch ) {
return trim( str_replace( 'origin/', '', $branch ) );
}, $branches );
$return['remote'] = $branches;
}
$cache_status[ $repo ] = $return;
set_transient( self::CACHE_KEY, $cache_status, MINUTE_IN_SECONDS * 3 );
return $return;
}
/**
* Refresh Git
*/
public function refresh( $repo = null ) {
if ( $repo === null ) {
foreach( $this->get_repos() as $repo ) {
$this->refresh( $repo );
}
return;
}
$this->execute_git_command( $repo, 'git remote update; git fetch origin; git remote prune origin' );
$status = $this->get_repo_data( $repo );
if ( isset( $status['branch'] ) && 'detached' !== $status['branch'] ) {
$this->execute_git_command( $repo, sprintf( 'git clean -fd; git reset --hard; git pull -f origin %s; git submodule update --init --recursive', escapeshellarg( $status['branch'] ) ) );
}
$this->purge_wp_cache();
$this->purge_git_branch_cache( $repo );
$this->schedule_third_party_cache_purging();
}
/**
* Handle deploy.
*/
protected function handle_deploy() {
if ( ! defined( 'GIT_SWITCH_DEPLOY_SECRET' ) ) {
return;
}
$repo = filter_input( INPUT_GET, 'repo' );
if ( ! $repo ) {
return;
}
$autodeploy_key = filter_input( INPUT_GET, 'git-switch-auto-deploy' );
if ( $autodeploy_key === GIT_SWITCH_DEPLOY_SECRET ) {
$this->refresh( $repo );
echo "Refreshed.";
exit;
}
$git_pull_key = filter_input( INPUT_GET, 'git-pull' );
if ( $git_pull_key === GIT_SWITCH_DEPLOY_SECRET ) {
$this->refresh( $repo );
wp_safe_redirect( remove_query_arg( [ 'git-pull', 'repo' ] ) );
exit;
}
}
/**
* Force cache purging on next page load.
*/
protected function schedule_third_party_cache_purging() {
set_transient( 'force_purge_cache', true, 15 * MINUTE_IN_SECONDS );
}
protected function purge_wp_cache() {
if ( function_exists( 'opcache_reset' ) ) {
opcache_reset();
}
if ( function_exists( 'wp_cache_flush' ) ) {
wp_cache_flush();
}
if ( class_exists( 'Memcached' ) ) {
// OC4EVERYONE plugin
if ( defined( 'OC4EVERYONE_PREDEFINED_SERVER' ) ) {
$memcached = new Memcached();
list($node, $port) = explode(':', OC4EVERYONE_PREDEFINED_SERVER);
$memcached->addServer($node, $port, PHP_INT_MAX);
$memcached->flush(0);
}
}
}
/**
* Get the absolute path to the repo
*
* @param string $repo The repo
*
* @return string The absolute path
*/
private function get_repo_abspath( $repo ) {
return trailingslashit( WP_CONTENT_DIR ) . $repo;
}
/**
* Execute a Git command
*
* @param string $repo The repo
* @param string $command The command
*
* @return array The results of the command
*/
private function execute_git_command( $repo, $command ) {
$this->setup_git_ssh_command( $repo );
$path = $this->get_repo_abspath( $repo );
exec( sprintf( 'cd %s; %s', escapeshellarg( $path ), $command ), $results );
return $results;
}
/**
* Setup the GIT_SSH_COMMAND environment variable
*/
private function setup_git_ssh_command( $repo ) {
if ( ! defined( 'GIT_SWITCH_REPOS' ) ) {
return;
}
$repos = GIT_SWITCH_REPOS;
if ( ! isset( $repos[ $repo ]['ssh_key_path'] ) ) {
return;
}
$ssh_key_path = realpath( ABSPATH . $repos[ $repo ]['ssh_key_path'] );
// Set the GIT_SSH_COMMAND environment variable
putenv("GIT_SSH_COMMAND=ssh -i $ssh_key_path");
}
/**
* Clear the repo cache. If no repo is specified, clear the entire cache.
*
* @param string|null $repo The repo to clear
*/
private function purge_git_branch_cache( $repo = null ) {
if ( $repo ) {
$git_cache = get_transient( self::CACHE_KEY );
unset( $git_cache[ $repo ] );
set_transient( self::CACHE_KEY, $git_cache, MINUTE_IN_SECONDS * 3 );
} else {
delete_transient( self::CACHE_KEY );
}
}
/**
* Get the list of repos.
*
* @return array The list of repos.
*/
private function get_repos() {
if ( defined( 'GIT_SWITCH_REPOS' ) ) {
return array_keys( GIT_SWITCH_REPOS );
}
$active_theme_dir = basename(get_template());
return [ 'themes/' . $active_theme_dir ];
}
}
/**
* Release the kraken!
*/
function Git_Switch() {
return Git_Switch::get_instance();
}
add_action( 'plugins_loaded', 'Git_Switch' );