Skip to content

Commit

Permalink
Added custom Git helper
Browse files Browse the repository at this point in the history
  • Loading branch information
antogno committed Mar 2, 2023
1 parent 06f4fc4 commit 74eef55
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 15 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ CI_ENV = development # "development", "testing" or "production"

BLOGSONIC_BASE_URL = "http://localhost/blogsonic/"
BLOGSONIC_ENCRYPTION_KEY = "F7z4zM0L3ua6e9rdZgy0StgIYA8xIFai" # https://randomkeygen.com/#ci_key
BLOGSONIC_VERSION = HEAD # "HEAD" or a valid GitHub tag (https://github.com/antogno/blogsonic/tags)

# Email address to be used publicly for contact purposes
BLOGSONIC_IN_EMAIL = "example@gmail.com"
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ The valid variables are:
- `CI_ENV`: defines the current environment. The available options are: `development`, `testing` and `production`. Setting the variable to a value of `development` will cause all PHP errors to be rendered to the browser when they occur. Conversely, setting the variable to `production` will disable all error output;
- `BLOGSONIC_BASE_URL`
- `BLOGSONIC_ENCRYPTION_KEY`: to generate an encryption key, you can go to [RandomKeygen][7] and scroll down to "CodeIgniter Encryption Keys";
- `BLOGSONIC_VERSION`: `HEAD` or a [valid GitHub tag][14];
- `BLOGSONIC_IN_EMAIL`: email address to be used publicly for contact purposes;
- `BLOGSONIC_OUT_EMAIL`: the outgoing email (e.g.: sending the forgotten password email);
- `BLOGSONIC_OUT_EMAIL_SMTP_PORT`
Expand Down Expand Up @@ -244,4 +243,3 @@ For more information, see the [Creative Commons website][12].
[11]: https://github.com/antogno/blogsonic/blob/master/LICENSE "License"
[12]: https://creativecommons.org/publicdomain/zero/1.0/ "Creative Commons"
[13]: https://twitter.com/AGranaldi "AGranaldi - Twitter"
[14]: https://github.com/antogno/blogsonic/tags "Tags"
2 changes: 1 addition & 1 deletion application/config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url');
$autoload['helper'] = array('url', 'git');

/*
| -------------------------------------------------------------------
Expand Down
210 changes: 210 additions & 0 deletions application/helpers/git_helper.php
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';
}
}
22 changes: 11 additions & 11 deletions application/views/partials/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
&ndash;
<?= $this->lang->line('designed_and_created_by'); ?> <a target="_blank" href="https://www.linkedin.com/in/antonio-granaldi/">Antonio Granaldi</a>
&vert;
<?php
if ($_ENV['BLOGSONIC_VERSION'] == 'HEAD') {
?>
<a target="_blank" href="https://github.com/antogno/blogsonic/tree/master"><span class="badge bg-primary"><?= $_ENV['BLOGSONIC_VERSION']; ?></span></a>
<?php
} else {
?>
<a target="_blank" href="https://github.com/antogno/blogsonic/releases/tag/<?= $_ENV['BLOGSONIC_VERSION']; ?>"><span class="badge bg-primary"><?= $_ENV['BLOGSONIC_VERSION']; ?></span></a>
<?php
}
?>

<?php $git_info = getCurrentGitInfo(); ?>
<a
target="_blank"
href="<? $git_info['url']; ?>"
>
<span class="badge bg-primary">
<i class="fa fa-code-fork"></i>
<?= $git_info['label']; ?>
</span>
</a>
</p>
</div>
<div style="width: 30%; float: right">
Expand Down

0 comments on commit 74eef55

Please sign in to comment.