git.class.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class git
  3. {
  4. public static function checkError(string $_string = NULL){
  5. $return = NULL;
  6. if($_string != NULL){
  7. if(strpos($_string, '-// SUCCESS //-') === TRUE){
  8. $return = TRUE;
  9. } else {
  10. return FALSE;
  11. }
  12. return $return;
  13. } else {
  14. return TRUE;
  15. }
  16. }
  17. public static function getCommitHash($_target = NULL){
  18. if($_target == NULL){
  19. $target = "HEAD";
  20. } else {
  21. $target = "origin/".$_target;
  22. }
  23. return substr(trim(exec('git log --pretty="%H" -n1 ' . $target)), 0, 10);
  24. }
  25. public static function getCommitDate($_target = NULL){
  26. if($_target == NULL){
  27. $target = "HEAD";
  28. } else {
  29. $target = "origin/".$_target;
  30. }
  31. $commitDate = new \DateTime(trim(exec('git log -n1 --pretty=%ci ' . $target)));
  32. $commitDate->setTimezone(new \DateTimeZone('Europe/Paris'));
  33. return $commitDate->format('d/m/Y à H:i:s');
  34. }
  35. public static function getCommitAuthor($_target = NULL){
  36. if($_target == NULL){
  37. $target = "HEAD";
  38. } else {
  39. $target = "origin/".$_target;
  40. }
  41. return exec('git log --pretty="%cn" -n1 ' . $target);
  42. }
  43. public static function getCommitName($_target = NULL){
  44. if($_target == NULL){
  45. $target = "HEAD";
  46. } else {
  47. $target = "origin/".$_target;
  48. }
  49. return exec('git log --pretty="%s" -n1 ' . $target);
  50. }
  51. public static function getBranchName($_target = NULL){
  52. if($_target == NULL){
  53. return exec('git rev-parse --abbrev-ref HEAD');
  54. } else {
  55. return $_target;
  56. }
  57. }
  58. public static function printVersion($_target = NULL){
  59. echo sprintf(
  60. '<h6>%s <span class="badge bg-info">%s</span> <span class="badge bg-success">%s</span></h6>
  61. <code>%s</code>
  62. <div class="media-body pb-3 mb-0 small lh-125">Le %s</div>',
  63. self::getCommitAuthor($_target),
  64. self::getBranchName($_target),
  65. self::getCommitHash($_target),
  66. self::getCommitName($_target),
  67. self::getCommitDate($_target)
  68. );
  69. }
  70. }