From cb5fe85d4f8e8a0bcbfda1d699cc8ecfc2571fcd Mon Sep 17 00:00:00 2001 From: Marcos Coelho Date: Thu, 3 Jan 2013 00:40:16 -0200 Subject: [PATCH] improve get blame method retrieving long revisions to avoid ambiguity and omitting the caret of revisions (boundary commits) --- src/GitList/Controller/CommitController.php | 2 +- src/GitList/Git/Repository.php | 38 +++++++++++++++++++++ views/blame.twig | 2 +- views/commits_list.twig | 2 +- views/rss.twig | 2 +- 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/GitList/Controller/CommitController.php b/src/GitList/Controller/CommitController.php index 728b861..2597556 100644 --- a/src/GitList/Controller/CommitController.php +++ b/src/GitList/Controller/CommitController.php @@ -72,7 +72,7 @@ class CommitController implements ControllerProviderInterface ->assert('branch', '[\w-._\/]+') ->bind('searchcommits'); - $route->get('{repo}/commit/{commit}/', function($repo, $commit) use ($app) { + $route->get('{repo}/commit/{commit}', function($repo, $commit) use ($app) { $repository = $app['git']->getRepository($app['git.repos'] . $repo); $commit = $repository->getCommit($commit); diff --git a/src/GitList/Git/Repository.php b/src/GitList/Git/Repository.php index 50d1f37..edd399b 100644 --- a/src/GitList/Git/Repository.php +++ b/src/GitList/Git/Repository.php @@ -37,6 +37,44 @@ class Repository extends BaseRepository return $commit; } + /** + * Blames the provided file and parses the output + * + * @param string $file File that will be blamed + * @return array Commits hashes containing the lines + */ + public function getBlame($file) + { + $blame = array(); + $logs = $this->getClient()->run($this, "blame --root -sl $file"); + $logs = explode("\n", $logs); + + $i = 0; + $previousCommit = ''; + foreach ($logs as $log) { + if ($log == '') { + continue; + } + + preg_match_all("/([a-zA-Z0-9]{40})\s+.*?([0-9]+)\)(.+)/", $log, $match); + + $currentCommit = $match[1][0]; + if ($currentCommit != $previousCommit) { + ++$i; + $blame[$i] = array( + 'line' => '', + 'commit' => $currentCommit, + 'commitShort' => substr($currentCommit, 0, 8) + ); + } + + $blame[$i]['line'] .= PHP_EOL . $match[3][0]; + $previousCommit = $currentCommit; + } + + return $blame; + } + /** * Show the repository commit log with pagination * diff --git a/views/blame.twig b/views/blame.twig index 039de5c..d58d918 100644 --- a/views/blame.twig +++ b/views/blame.twig @@ -14,7 +14,7 @@ {% for blame in blames %} - + {% endfor %} diff --git a/views/commits_list.twig b/views/commits_list.twig index cdab179..dbc6eec 100644 --- a/views/commits_list.twig +++ b/views/commits_list.twig @@ -11,7 +11,7 @@ diff --git a/views/rss.twig b/views/rss.twig index aca671f..e14e783 100644 --- a/views/rss.twig +++ b/views/rss.twig @@ -9,7 +9,7 @@ {{ commit.message }} {{ commit.author.name }} authored {{ commit.shortHash }} in {{ commit.date | date('d/m/Y \\a\\t H:i:s') }} - {{ path('commit', {repo: repo, commit: commit.shortHash}) }} + {{ path('commit', {repo: repo, commit: commit.hash}) }} {{ commit.date | date('r') }} {% endfor %}
{{ blame.commit }}{{ blame.commitShort }}
{{ blame.line }}
- View {{ item.shortHash }} + View {{ item.shortHash }}

{{ item.message }}

{{ item.author.name }} authored in {{ item.date | date('d/m/Y \\a\\t H:i:s') }}