1

I am using Laravel for site development purposes. What would be nice is to have a button on the main page that shows the current version of the project. When clicking on the button, one would see all of the changes that have taken place (in the form of git commits) to the system. Is there a way to do this? If so, how?

TIA

Casey Harrils
  • 2,793
  • 12
  • 52
  • 93

2 Answers2

1

On click of the button initiate an ajax call

$("#button").on("click", function () {
    url: window.location.origin + '/fetch-git-commits',
    type: "get",
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    success: function (data) {
        // data contains the git commit informations
        $(".container").html(data);
    }
});

In the Route.php file add an entry

Route::get('fetch-git-commits', 'YourController@getGitCommits');

In the controller

public function getGitCommits ()
{
   exec('git log', $output);
   $history = array();

   foreach($output as $line) {
       if(strpos($line, 'commit') === 0) {
           if(!empty($commit)) {
               array_push($history, $commit);   
               unset($commit);
           }
           $commit['hash']   = substr($line, strlen('commit'));
        }
        else if(strpos($line, 'Author') === 0) {
           $commit['author'] = substr($line, strlen('Author:'));
        }
        else if(strpos($line, 'Date') === 0) {
            $commit['date']   = substr($line, strlen('Date:'));
        }
        else {
            if(isset($commit['message']))
                 $commit['message'] .= $line;
            else
                 $commit['message'] = $line;
        }
    }
    return $history; // Array of commits, parse it to json if you need
}

References:

Reading a git commit message from php

Parse git log with PHP to an array

Community
  • 1
  • 1
linktoahref
  • 7,812
  • 3
  • 29
  • 51
0

There is a little package for that:

https://github.com/tremby/laravel-git-version

blueteck
  • 33
  • 9