Display the current Git ‘version’ in PHP
If you’d like to do it without exec() and you’re using git lightweight (see comments below) tagging:
You can get the current HEAD commit hash from .git/HEAD or .git/refs/heads/master . We then loop to find matching. Reversing the array first for speed because you’re more likely to at a higher recent tag.
So if the current php file sits in a public_html or www folder one level down from the .git folder.
Firstly, some git commands to fetch version information:
- commit hash long
- git log —pretty=»%H» -n1 HEAD
- git log —pretty=»%h» -n1 HEAD
- git log —pretty=»%ci» -n1 HEAD
- git describe —tags —abbrev=0
- git describe —tags
Secondly, use exec() combined with the git commands of your choice from above to build the version identifier:
class ApplicationVersion < const MAJOR = 1; const MINOR = 2; const PATCH = 3; public static function get() < $commitHash = trim(exec('git log --pretty="%h" -n1 HEAD')); $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD'))); $commitDate->setTimezone(new \DateTimeZone('UTC')); return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s')); > > // Usage: echo 'MyApplication ' . ApplicationVersion::get(); // MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)
Display the current Git ‘version’ in PHP
Firstly, some git commands to fetch version information:
- commit hash long
- git log —pretty=»%H» -n1 HEAD
- git log —pretty=»%h» -n1 HEAD
- git log —pretty=»%ci» -n1 HEAD
- git describe —tags —abbrev=0
- git describe —tags
Secondly, use exec() combined with the git commands of your choice from above to build the version identifier:
class ApplicationVersion< const MAJOR = 1; const MINOR = 2; const PATCH = 3; public static function get() < $commitHash = trim(exec('git log --pretty="%h" -n1 HEAD')); $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD'))); $commitDate->setTimezone(new \DateTimeZone('UTC')); return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s')); >>// Usage: echo 'MyApplication ' . ApplicationVersion::get();// MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)
class QuickGit < public static function version() < exec('git describe --always',$version_mini_hash); exec('git rev-list HEAD | wc -l',$version_number); exec('git log -1',$line); $version['short'] = "v1.".trim($version_number[0]).".".$version_mini_hash[0]; $version['full'] = "v1.".trim($version_number[0]).".$version_mini_hash[0] (".str_replace('commit ','',$line[0]).")"; return $version; >>
If you’d like to do it without exec() and you’re using git lightweight (see comments below) tagging:
You can get the current HEAD commit hash from .git/HEAD or .git/refs/heads/master . We then loop to find matching. Reversing the array first for speed because you’re more likely to at a higher recent tag.
So if the current php file sits in a public_html or www folder one level down from the .git folder.
$HEAD_hash = file_get_contents('../.git/refs/heads/master'); // or branch x$files = glob('../.git/refs/tags/*');foreach(array_reverse($files) as $file) < $contents = file_get_contents($file); if($HEAD_hash === $contents) < print 'Current tag is ' . basename($file); exit; >>print 'No matching tag';
Display the current Git ‘version’ in PHP
If you’d like to do it without exec() and you’re using git lightweight (see comments below) tagging:
You can get the current HEAD commit hash from .git/HEAD or .git/refs/heads/master . We then loop to find matching. Reversing the array first for speed because you’re more likely to at a higher recent tag.
So if the current php file sits in a public_html or www folder one level down from the .git folder.
Firstly, some git commands to fetch version information:
- commit hash long
- git log —pretty=»%H» -n1 HEAD
- git log —pretty=»%h» -n1 HEAD
- git log —pretty=»%ci» -n1 HEAD
- git describe —tags —abbrev=0
- git describe —tags
Secondly, use exec() combined with the git commands of your choice from above to build the version identifier:
class ApplicationVersion < const MAJOR = 1; const MINOR = 2; const PATCH = 3; public static function get() < $commitHash = trim(exec('git log --pretty="%h" -n1 HEAD')); $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD'))); $commitDate->setTimezone(new \DateTimeZone('UTC')); return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s')); > > // Usage: echo 'MyApplication ' . ApplicationVersion::get(); // MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)
Display the current Git ‘version’ in PHP
Firstly, some git commands to fetch version information:
- commit hash long
- git log —pretty=»%H» -n1 HEAD
- git log —pretty=»%h» -n1 HEAD
- git log —pretty=»%ci» -n1 HEAD
- git describe —tags —abbrev=0
- git describe —tags
Secondly, use exec() combined with the git commands of your choice from above to build the version identifier:
class ApplicationVersion < const MAJOR = 1; const MINOR = 2; const PATCH = 3; public static function get() < $commitHash = trim(exec('git log --pretty="%h" -n1 HEAD')); $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci HEAD'))); $commitDate->setTimezone(new \DateTimeZone('UTC')); return sprintf('v%s.%s.%s-dev.%s (%s)', self::MAJOR, self::MINOR, self::PATCH, $commitHash, $commitDate->format('Y-m-d H:i:s')); > > // Usage: echo 'MyApplication ' . ApplicationVersion::get(); // MyApplication v1.2.3-dev.b576fd7 (2016-11-02 14:11:22)
Read More
Run git tag in terminal to preview your tags and say you got i.e:
here’s how to get the latest version v1.2.4
function getVersion() < $hash = exec("git rev-list --tags --max-count=1"); return exec("git describe --tags $hash"); >echo getVersion(); // "v1.2.4"
Coincidentally (if your tags are ordered), since exec returns only the last row we could just do:
function getVersion() < return exec("git tag"); >echo getVersion(); // "v1.2.4"
To get all the rows string use shell_exec :
function getVersions() < return shell_exec("git tag"); >echo getVersions(); // "v1.0.0 // v1.1.0 // v1.2.4"
$tagsArray = explode(PHP_EOL, shell_exec("git tag"));
git tag --sort=committerdate
For sorting purposes, fields with numeric values sort in numeric order (objectsize, authordate, committerdate, creatordate, taggerdate). All other fields are used to sort in their byte-value order.
Roko C. Buljan 183793
resource friendly and the same as i have under eclipse shown
Similar Question and Answer
- Short hash: $rev = exec(‘git rev-parse —short HEAD’);
- Full hash: $rev = exec(‘git rev-parse HEAD’);
If you’d like to do it without exec() and you’re using git lightweight (see comments below) tagging:
You can get the current HEAD commit hash from .git/HEAD or .git/refs/heads/master . We then loop to find matching. Reversing the array first for speed because you’re more likely to at a higher recent tag.
So if the current php file sits in a public_html or www folder one level down from the .git folder.
Harry B 2766
More Answer
- How can i display the 5 latests results from the database at the bottom of the page in PHP instead of displaying it at the top?
- Get the h1 tag on current page in PHP
- Is it possible have more than one version of php on the same VirtualHost instance?
- Display pictures from a folder in the specific way — PHP
- AJAX PHP Server side respond but I can not display the response in client side page
- Centos httpd php version is different from the php version command line
- Reload the current page using Jquery and php after a function has been called
- Staying at the current page when switching language in PHP
- Wamp showing the wrong version of php even when updated
- How to only display one option when there are multiple with the same name in PHP dropdown?
- Create a folder with the email of the user that just created an account and display the image(s) that is in the user email’s folder using php
- How can I do so that multiple users can access their current timezone instead of the one provided by my server using PHP
- How to display message in PHP using certain string in the URL?
- How to display the special characters with a combination of PHP code snippet in it
- Can i fetch a php array file on a server using JSON in javascript to display the data?
- How to know the php version associated with particular version of XAMPP without installing on windows 10?
- How to create a drop down list with php from an sql table AND display a dynamic default form value in the drop down list
- Run composer using different version of PHP without changing the environment variables
- PHP Reference the current array / best way to set default options?
- Does not display the message using JSON AJAX php online
- Simple Dom Parser issues with the latest php version
- How can I change the css property display to block from PHP code
- PHP script to dynamically generate links to the directory, subdirector and files present in current diectory
- How can i display GET output of a hyperlink on another php page (that includes the php page that contains GET request)
- how to display the error message as a tooltip in php
- How to format the php code to display this better
- how can i display the records from the SQl server database in an array using PHP
- Working with the latest PHP / MongoDB version
- php code to display the Latest reading from a Array
- Android Database: How to display the Many-to-many Relationship in my PHP file
- Display the argument under the line of user after running php in mac terminal
- How to make a request for the current LDAP Username via PHP
- Does the current mssql drivers for php 5.6 work in windows 10?
- PHP how to make the value of the result display in textbox
- Display errors on the same page php
- Changing The Current Time of a JavaScript Audio Object, When Getting the audio file from a PHP script
More answer with same ag
- how to simplify the same code in two else condition PHP
- Setting the selectedindex property of an HTML dropdown using PHP
- Adding number of days to current date
- SQL: choose a column name dynamically based on its value from the table
- Using PHP explode to create an array of words from $_GET
- JQUERY load from TXT file, show VALUE in dropdown, GET user choice — Logical and JS help needed
- Adding an key=> value pair comparing the **key of one array** with the **value of another array**
- PHP GD Stronger Antialias
- Unexpected results when passing values from PHP to Ajax using json_encode
- I would like to synchronize the php execution to the mysql transaction. (Cause php to wait until the transaction is complete. )
- disable add to cart if custom options are required.Magento
- Sort then split a PHP array?
- Checking multiple empty field in a form
- When I upload image files from a folders I cannot make them draggable
- php class methods used statically without class reference?
- Styling PHP echo table
- Code to forward browser if user is not logged in (checking if session variable isset) is not firing
- Insert target=»_blank» into the link.
- Test if a string has been encrypted using mcrypt_encode in php?
- all button function whenever click in php
- How to get the amount each line appears from a file?
- Parallel execution of PHP
- fopen(«php://input», «r») returns 0
- Need help in displaying the advertisement banner in drupal 6
- How can two scripts use the same port at the same time?
- PHP Request for posting Form to Iframe
- Using KLARNA with Magento via SOAP
- Incrementing numbers with double digits in php
- How to rotate text while creating PDF in Zend Framework?
- Open a new function with PHP
- json object usage with ajax returned array
- PHP/Python — Multithreaded Sleep in Background
- PHP: include and file_exists scope
- PHP: No match when using exclamation mark in regular expression
- Facebook API — login with JSSDK use php sdk in back end
- Registering Application Events in Laravel 5
- Difference b/w Single and double underscore in PHP Zend Framwork
- Handling multiple file downloads from Amazon S3?
- Concatenate string with space mysql php
- PHP $_SESSION[variable] is not getting value on next page
- PHP — Handle upload traffic
- Undefined variable notice in a display.php error when the variable has eiher been fixed or deleted
- How to delete all cookies for facebook.com?
- How to update Mysqli table using OOPHP
- How to build datastore indexes (PHP GAE)
- How do i use Switch for inline keyboard and replykeyboard at the same time #PHP
- How to CACHE query/find results with PAGINATION CakePhp 2.x?
- nodejs equivalent of PHPlivedocx?
- PHP — Best practice to create a multidimensional array
- Matching Array than insert parent array by Index