Display your current Git branch in the admin bar

It’s a common mistake to make…

You’re working on some code and after making changes realise you’re working on the wrong Git branch…

More often than not, it’s a quick change to the right branch, otherwise you might have to unpick changes before you can do anything else which can be a time consuming and difficult exercise.

I’ve put together a simple plugin, also available on Github Gist, that displays the current branch you’re working on in the admin bar; if you’re working on restricted branch (you can change these in the restricted_branches() function), this is highlighted in red.

<?php
/**
* Plugin Name: Display Git Branch
* Version: 1.0.0
* Description: Shows which Git branch you're working on. Highlights restricted branches in red.
* Author: James Morrison
* Author URI: https://www.jamesmorrison.me/
**/

// Namespace

namespace Display_Git_Branch;

// Security Check

defined( 'ABSPATH' ) || die( 'Direct file access is forbidden.' );

// Define restricted branches

function restricted_branches() {

	return [
		'master',
		'develop',
	];

}

// Get git branch

function branch() {

	// Sane default, assume this isn't using Git
	$_branch = 'Not detected';

	// Look in theme, then site root
	$paths = [
		get_stylesheet_directory(), // Theme
		ABSPATH, // Site
	];

	// Run through the paths and check each, break on success
	// PHPCS suggests wp_remote_get instead of file_get_contents - this does not work as our path is relative
	foreach ( $paths as $location ) {
		if ( file_exists( $location . '.git/HEAD' ) ) {
			$_branch = implode( '/', array_slice( explode( '/', file_get_contents( $location . '.git/HEAD' ) ), 2 ) );
			break;
		}
	}

	// This includes a line break... Strip it out and return the branch name.
	return str_replace( "\n", '', $_branch );

}

// Display Git Branch in Admin Bar

add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {

	$_branch = branch();

	$wp_admin_bar->add_node( [
		'id' => 'show-git-branch',
		'title' => "GIT Branch: $_branch",
	] );

}, 99999 );

// Echo CSS inc. warning highlight
add_action( 'admin_head', __NAMESPACE__ . '\\display', 100, 0 );
add_action( 'wp_head', __NAMESPACE__ . '\\display', 100, 0 );

function display() {

	$output = '<style type="text/css">#wp-admin-bar-show-git-branch .ab-item:before { content: "\f237"; top: 2px; }</style>';

	$_branch             = branch();
	$_resticted_branches = restricted_branches();

	if ( in_array( $_branch, $_resticted_branches, true ) ) {
		$output .= '<style type="text/css">#wp-admin-bar-show-git-branch .ab-item { background: #c00; }</style>';
	}

	// PHPCS Ok - the outputted text contains no variables and cannot be manipulated
	echo $output;

}

// Display warning if on restricted branches

add_action( 'wp_footer', function() {

	$_branch             = branch();
	$_resticted_branches = restricted_branches();

	if ( in_array( $_branch, $_resticted_branches, true ) ) {

		echo '<div style="position: absolute; top: 150px; right: 50px; width: 500px; padding: 50px; background: #c00; font-size: 50px; color: #fff;">
		You are on the <strong>' . esc_attr( strtoupper( $_branch ) ) . '</strong> branch.
		</div>';
	}

}, 100, 0 );