2
0

git.class.php 2.5 KB

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