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

Asked

Viewed 169 times

1

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

I wonder if I could use that git log GIT and pass it to a array of PHP.

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

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

Sort of 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.

2 answers

4


Basically the command inside the PHP

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

outworking:

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 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);

outworking:

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
        )

)

-1

This will depend on the company, for example Github is: The data below can be provided on : https://github.com/settings/applications

   <?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 her API.

  • I said "local repository" on the question. The script will be inside the folder which is a clone of a reposory. I even put the file .git in the directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.