Skip to content

Commit 4f9d79b

Browse files
author
James Morrison
committed
Initial commit
0 parents  commit 4f9d79b

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/vendor/
3+
.DS_Store
4+
.svn
5+
.cvs
6+
.sass-cache
7+
Thumbs.db

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Display Git Branch
2+
3+
Shows which Git branch you're working on. Highlights configurable restricted branches in red.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "jamesmorrison/display-git-branch",
3+
"description": "Display Git Branch",
4+
"homepage": "https://github.com/jamesmorrison/display-git-branch",
5+
"version": "0.1.0",
6+
"keywords": [
7+
"wordpress"
8+
],
9+
"type": "wordpress-plugin",
10+
"license": "GPL-2.0+",
11+
"authors": [
12+
{
13+
"name": "James Morrison",
14+
"email": "[email protected]",
15+
"homepage": "http://www.jamesmorrison.me"
16+
}
17+
],
18+
"support": {
19+
"source": "https://github.com/jamesmorrison/display-git-branch",
20+
"issues": "https://github.com/jamesmorrison/display-git-branch/issues"
21+
}
22+
}

display-git-branch.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Plugin Name: Display Git Branch
4+
* Plugin URI: https://github.com/jamesmorrison/display-git-branch/
5+
* Description: Shows which Git branch you're working on. Highlights configurable restricted branches in red.
6+
* Version: 0.1.0
7+
* Author: James Morrison
8+
* Author URI: http://www.jamesmorrison.me/
9+
* Text Domain: display-git-branch
10+
**/
11+
12+
// Security check
13+
defined( 'ABSPATH' ) || die();
14+
15+
// Useful global constants
16+
define( 'DISPLAY_GIT_BRANCH_VERSION', '0.1.0' );
17+
define( 'DISPLAY_GIT_BRANCH_URL', plugin_dir_url( __FILE__ ) );
18+
define( 'DISPLAY_GIT_BRANCH_PATH', dirname( __FILE__ ) . '/' );
19+
define( 'DISPLAY_GIT_BRANCH_INC', DISPLAY_GIT_BRANCH_PATH . 'includes/' );
20+
21+
// Include files.
22+
require_once DISPLAY_GIT_BRANCH_INC . '/core.php';
23+
24+
// Bootstrap.
25+
\Display_Git_Branch\Core\setup();

includes/core.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Core plugin functionality.
4+
*
5+
* This file is used to register global commands for the GP Surgery Platform
6+
*
7+
* @package Display_Git_Branch
8+
*/
9+
10+
namespace Display_Git_Branch\Core;
11+
12+
// Security check - stop direct file access
13+
defined( 'DISPLAY_GIT_BRANCH_VERSION' ) || die();
14+
15+
/**
16+
* Default setup routine
17+
*
18+
* @return void
19+
*/
20+
function setup() {
21+
22+
// Useful helper function
23+
$n = function( $function ) {
24+
return __NAMESPACE__ . "\\$function";
25+
};
26+
27+
add_action( 'admin_bar_menu', $n( 'wp_admin_bar' ), 100, 1 );
28+
29+
add_action( 'admin_head', $n( 'style' ), 10, 0 );
30+
add_action( 'wp_head', $n( 'style' ), 10, 0 );
31+
32+
if ( in_array( branch(), restricted_branches(), true ) ) {
33+
add_action( 'admin_head', $n( 'warning_style' ), 10, 0 );
34+
add_action( 'wp_head', $n( 'warning_style' ), 10, 0 );
35+
}
36+
37+
}
38+
39+
/**
40+
* WP Admin Bar Node
41+
*
42+
* Add a node to the WP Admin Bar with the Git Branch
43+
*
44+
* @return void
45+
*/
46+
function wp_admin_bar( $wp_admin_bar ) {
47+
48+
$wp_admin_bar->add_node(
49+
[
50+
'id' => 'show-git-branch',
51+
/* Translators: The branch name */
52+
'title' => sprintf( __( 'Git branch: %s', 'display-git-branch' ), branch() ),
53+
]
54+
);
55+
56+
}
57+
58+
/**
59+
* Find Git branch
60+
*
61+
* @return string $branch - the git branch name, if found. Sane default if not.
62+
*/
63+
function branch() {
64+
65+
// Sane default, assume this isn't using Git
66+
$branch = esc_html( __( 'Not detected', 'display-git-branch' ) );
67+
68+
// Paths to search for a git branch
69+
$paths = apply_filters( 'display_git_branch_paths',
70+
[
71+
trailingslashit( WP_CONTENT_DIR ), // Content directory
72+
trailingslashit( ABSPATH ), // Site root
73+
]
74+
);
75+
76+
// Run through the paths and check each, break on success
77+
// PHPCS suggests wp_remote_get instead of file_get_contents - this does not work as our path is relative
78+
foreach ( $paths as $location ) {
79+
if ( file_exists( $location . '.git/HEAD' ) ) {
80+
$branch = str_replace( "\n", '', implode( '/', array_slice( explode( '/', file_get_contents( $location . '.git/HEAD' ) ), 2 ) ) );
81+
break;
82+
}
83+
}
84+
85+
// This includes a line break... Strip it out and return the branch name.
86+
return esc_html( $branch );
87+
88+
}
89+
90+
/**
91+
* Restricted Branches
92+
*
93+
* If you're on one of these branches, the WP Admin Bar node turns red
94+
*/
95+
function restricted_branches() {
96+
97+
return apply_filters( 'display_git_branch_restricted_branches',
98+
[
99+
'master',
100+
'develop',
101+
]
102+
);
103+
104+
}
105+
106+
/**
107+
* WP Admin Bar CSS
108+
*
109+
* Defines the style for the WP Admin Bar node
110+
*/
111+
function style() {
112+
113+
?>
114+
<style type="text/css">#wp-admin-bar-show-git-branch .ab-item:before { content: "\f237"; top: 2px; }</style>
115+
<?php
116+
117+
}
118+
119+
/**
120+
* WP Admin Bar Warning CSS
121+
*
122+
* Defines the style for the WP Admin Bar node
123+
*/
124+
function warning_style() {
125+
126+
?>
127+
<style type="text/css">#wp-admin-bar-show-git-branch .ab-item { background: #c00; }</style>
128+
<?php
129+
130+
}

index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// silence is golden

0 commit comments

Comments
 (0)