How to list the log of a local GIT repository through PHP?

1

git has a command called git log , which shows the commit history of the repository.

I would like to know if I would use this git log of GIT and pass it to array of PHP.

In this case, the PHP script would be inside the repository directory.

My idea was to be able to list, with PHP, the git log of the current branch by PHP.

More or less like this:

projeto
    .git/
    listar_log_git.php

NOTE : The idea is to do this in a local repository, as in the title of the question. That is, I want to list this data from the cloned repository, not remotely.

    
asked by anonymous 12.11.2018 / 11:40

2 answers

4

Basically the command within PHP

exec('git log', $result);
print_r($result);

result:

Array
(
    [0] => commit 693673def6d3cb8f196cf5988af70b87c4ff4cb3
    [1] => Author: None <[email protected]>
    [2] => Date:   Wed Nov 7 23:14:14 2018 -0200
    [3] => 
    [4] =>     Create README.md
    [5] => 
    [6] => commit f0a3ce8f990a693352b597e51ac2a874e07da2fa
    [7] => Author: None <[email protected]>
    [8] => Date:   Wed Nov 7 23:02:55 2018 -0200
    [9] => 
    [10] =>    Create index.php
)

Now is to work the information your way by building a new array , example:

<?php

    exec('git log', $result);
    $new_result = array();
    $j = -1;
    for($i = 0; $i < count($result); $i++)
    {
        if (strpos($result[$i],'commit') !== false)
        {
            $new_result[++$j]['commit'] = substr($result[$i], strlen('commit'));
        }
        else if (strpos($result[$i],'Author:') !== false)
        {
            $new_result[$j]['author'] = substr($result[$i], strlen('Author:'));
        }
        else if (strpos($result[$i],'Date:') !== false)
        {
            $new_result[$j]['date'] = substr($result[$i], strlen('Date:'));
        }
        else if (!empty($result[$i]))
        {
            $new_result[$j]['message'] = trim($result[$i]);
        }
    }

    print_r($new_result);

result:

Array
(
    [0] => Array
        (
            [commit] =>  693673def6d3cb8f196cf5988af70b87c4ff4cb3
            [author] =>  None <[email protected]>
            [date] =>    Wed Nov 7 23:14:14 2018 -0200
            [message] => Create README.md
        )

    [1] => Array
        (
            [commit] =>  f0a3ce8f990a693352b597e51ac2a874e07da2fa
            [author] =>  None <[email protected]>
            [date] =>    Wed Nov 7 23:02:55 2018 -0200
            [message] => Create index.php
        )

)
    
12.11.2018 / 15:10
-1

This will depend on the company, for example GitHub is: The data below can be provided at: link

   <?php  
    $user = 'flesheater'; $token = 'ced38b0e522a5c5e8ab10'; 
    $curl_url = 'https://api.github.com/users/' . $user . '/repos';
    $curl_token_auth = 'Authorization: token ' . $token;


     $ch = curl_init($curl_url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Awesome-Octocat-App', $curl_token_auth));
      $output = curl_exec($ch);
      curl_close($ch);
      $output = json_decode($output);

      if (!empty($output)) {
        foreach ($output as $repo) {
          print '<a href="' . $repo->html_url . '">' . $repo->name . '</a><br />';
        }
      }

Just check the company and access its API.

    
12.11.2018 / 11:54