Skip to content

Instantly share code, notes, and snippets.

@peterchester
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterchester/d2833d354e2af5d8dca4 to your computer and use it in GitHub Desktop.
Save peterchester/d2833d354e2af5d8dca4 to your computer and use it in GitHub Desktop.
Git Info WP Plugin
<?php
/*
Plugin Name: Git Info
Plugin URI: https://github.com/jbrinley/wp-git-deploy
Description: Git info (abstracted from jbrinley's git deploy plugin)
Author: Modern Tribe, Inc.
Author URI: http://tri.be
Contributors: jbrinley
Version: 1.0
*/
class Git_Info_Plugin {
/** @var Git_Info_Plugin */
private static $instance;
public function print_version_info() {
if ( ! ($revision = $this->get_revision()) ) {
return '';
}
$timestamp = $this->get_timestamp($revision);
$branch = $this->get_branch();
$branch = wp_basename($branch);
$output = '<span class="git-info-admin">';
$output .= __('Git Info: ','git-info');
if ( $branch ) {
$output .= sprintf( __( 'On branch %s', 'git-info' ), $branch );
$output .= " &mdash; ";
}
if ( $timestamp ) {
$output .= date('Y-m-d H:i:s', (int)$timestamp);
$output .= " &mdash; ";
}
$output .= sprintf('<abbr title="%s">%s</abbr>', $revision, substr($revision, 0, 8));
$output .= '</span>';
return $output;
}
public function get_timestamp( $revision = '' ) {
if ( !function_exists('exec') ) {
return FALSE;
}
if ( !$revision ) {
$revision = $this->get_revision();
}
$cmd = sprintf("git show --pretty=format:%%ct --summary %s", $revision);
$output = array();
exec($cmd, $output);
if ( isset( $output[0] ) ) return $output[0];
}
public function get_revision() {
$info = $this->get_info();
return empty($info['revision'])?'':$info['revision'];
}
public function get_branch() {
$info = $this->get_info();
return empty($info['branch'])?'':$info['branch'];
}
private function get_info() {
static $info = array();
if ( $info ) {
return $info;
}
if ( class_exists('Git_Deploy') ) {
$root_dir = Git_Deploy::get_instance()->get_abspath();
}
if ( empty($root_dir) || !file_exists($root_dir) ) {
$root_dir = ABSPATH;
}
// the easy way: ask git
$revision = exec("cd $root_dir && git rev-parse HEAD");
$branch = exec("cd $root_dir && git rev-parse --abbrev-ref HEAD");
// the hard way: try to parse the git plumbing
if ( !$revision && !$branch ) {
if ( !is_dir($root_dir.DIRECTORY_SEPARATOR.'.git') ) {
return $info; // probably a submodule, so doesn't have a proper .git directory
}
$head_file = $root_dir.DIRECTORY_SEPARATOR.'.git/HEAD';
if ( !file_exists($head_file) ) {
return $info;
}
$branch = trim(file_get_contents($head_file));
$branch = str_replace('ref: ', '', $branch);
$revision_file = $root_dir.DIRECTORY_SEPARATOR.'.git'.DIRECTORY_SEPARATOR.$branch;
$revision = file_exists($revision_file)?file_get_contents($revision_file):'';
}
$info = array(
'branch' => rtrim($branch),
'revision' => rtrim($revision),
);
return $info;
}
private function add_hooks() {
add_filter('admin_footer_text', array($this, 'print_version_info'));
}
/********** Singleton *************/
/**
* Create the instance of the class
*
* @static
* @return void
*/
public static function init() {
self::$instance = self::get_instance();
}
/**
* Get (and instantiate, if necessary) the instance of the class
* @static
* @return Git_Info_Plugin
*/
public static function get_instance() {
if ( !is_a( self::$instance, __CLASS__ ) ) {
self::$instance = new self();
}
return self::$instance;
}
final public function __clone() {
trigger_error( "No cloning allowed!", E_USER_ERROR );
}
final public function __sleep() {
trigger_error( "No serialization allowed!", E_USER_ERROR );
}
protected function __construct() {
$this->add_hooks();
}
}
Git_Info_Plugin::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment