-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
222 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
<?php | ||
defined('BASEPATH') OR exit('No direct script access allowed'); | ||
|
||
if (!function_exists('getCurrentGitInfo')) { | ||
/** | ||
* Retrieves the current Git info such as the URL of the current commit, | ||
* the commit hash, the current branch, the possible current tag and | ||
* a label to use in front-end | ||
* | ||
* @return array | ||
*/ | ||
function getCurrentGitInfo() | ||
{ | ||
$url = 'https://github.com/antogno/blogsonic/'; | ||
|
||
$hash = getCurrentGitCommitHash(false); | ||
$tag = getCurrentGitTag(); | ||
$branch = getCurrentGitBranch(); | ||
|
||
if (isCurrentlyInAGitTag()) { | ||
$url .= "tree/$tag"; | ||
$label = $tag; | ||
} else { | ||
$label = $branch; | ||
if (isCurrentlyInGitHead()) { | ||
$url .= "tree/$branch"; | ||
} else { | ||
$url .= "tree/$hash"; | ||
$label .= " ($hash)"; | ||
} | ||
} | ||
|
||
return compact('url', 'hash', 'branch', 'tag', 'label'); | ||
} | ||
} | ||
|
||
if (!function_exists('getCurrentGitCommitHash')) { | ||
/** | ||
* Retrieves the current Git commit hash | ||
* | ||
* @param bool $full whether to get the full hash or | ||
* just its first 10 characters. | ||
* | ||
* @return false|string | ||
*/ | ||
function getCurrentGitCommitHash(bool $full = true) | ||
{ | ||
$output = shell_exec('git rev-parse HEAD'); | ||
|
||
if (!is_string($output)) { | ||
return false; | ||
} | ||
|
||
$output = trim($output); | ||
|
||
if (!isValidGitCommitHash($output)) { | ||
return false; | ||
} | ||
|
||
return $full ? $output : substr($output, 0, 10); | ||
} | ||
} | ||
|
||
if (!function_exists('getCurrentGitBranch')) { | ||
/** | ||
* Retrieves the current Git branch | ||
* | ||
* @return false|string | ||
*/ | ||
function getCurrentGitBranch() | ||
{ | ||
$output = shell_exec('git branch --show-current'); | ||
|
||
if (!is_string($output)) { | ||
return false; | ||
} | ||
|
||
return trim($output); | ||
} | ||
} | ||
|
||
if (!function_exists('getCurrentGitTag')) { | ||
/** | ||
* Retrieves the current Git tag (if in a tag) | ||
* | ||
* @return false|string | ||
*/ | ||
function getCurrentGitTag() | ||
{ | ||
$hash = getCurrentGitCommitHash(); | ||
|
||
if ($hash === false) { | ||
return false; | ||
} | ||
|
||
$tags = getGitTags(); | ||
|
||
if (empty($tags)) { | ||
return false; | ||
} | ||
|
||
foreach ($tags as $tag) { | ||
$output = shell_exec("git rev-list -n 1 \"$tag\""); | ||
|
||
if (!is_string($output)) { | ||
continue; | ||
} | ||
|
||
$output = trim($output); | ||
|
||
if (!isValidGitCommitHash($output)) { | ||
continue; | ||
} | ||
|
||
if ($hash === $output) { | ||
return $tag; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if (!function_exists('isCurrentlyInAGitTag')) { | ||
/** | ||
* Whether you are now in a Git tag or not | ||
* | ||
* @return bool | ||
*/ | ||
function isCurrentlyInAGitTag() | ||
{ | ||
return getCurrentGitTag() !== false; | ||
} | ||
} | ||
|
||
if (!function_exists('isCurrentlyInGitHead')) { | ||
/** | ||
* Whether you are now in the HEAD of the current branch | ||
* or not | ||
* | ||
* @return bool | ||
*/ | ||
function isCurrentlyInGitHead() | ||
{ | ||
$output = shell_exec('git rev-parse HEAD'); | ||
|
||
if (!is_string($output)) { | ||
return false; | ||
} | ||
|
||
$output = trim($output); | ||
|
||
if (!isValidGitCommitHash($output)) { | ||
return false; | ||
} | ||
|
||
return getCurrentGitCommitHash() === $output; | ||
} | ||
} | ||
|
||
if (!function_exists('getGitTags')) { | ||
/** | ||
* Retrieves the Git tags list, if there are any | ||
* | ||
* @return string[] | ||
*/ | ||
function getGitTags() | ||
{ | ||
$tags = []; | ||
|
||
$output = shell_exec("git tag"); | ||
|
||
if (!is_string($output)) { | ||
return $tags; | ||
} | ||
|
||
$output = trim(preg_replace('/\s+/', ' ', $output)); | ||
|
||
$tags = explode(' ', $output); | ||
|
||
return $tags; | ||
} | ||
} | ||
|
||
if (!function_exists('isValidGitCommitHash')) { | ||
/** | ||
* Checks if the given string is a valid Git commit | ||
* hash | ||
* | ||
* @param string $hash string to check. | ||
* | ||
* @return bool | ||
*/ | ||
function isValidGitCommitHash(string $hash) | ||
{ | ||
$hash = trim($hash); | ||
|
||
if (strlen($hash) !== 40) { | ||
return false; | ||
} | ||
|
||
$output = shell_exec("git cat-file -t $hash"); | ||
|
||
if (!is_string($output)) { | ||
return false; | ||
} | ||
|
||
return trim($output) === 'commit'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters