Skip to content

Commit

Permalink
* New Model GitFile to manage the files inside a repository.
Browse files Browse the repository at this point in the history
* Added a TODO file with the things to do.
* Some changes and bugfixes en git.php and git_source.php
  • Loading branch information
maurobender committed Oct 31, 2010
1 parent 8d8b693 commit 175a195
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 62 deletions.
11 changes: 11 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TODO:
[More Priority]
* Model Tag to manage the repository tags.
* Model Branch to manage the repository branchs.
* Move all the functions that have an "exec('git_command')" inside them to the git library.
* Make the associations between models work.
* Rename the models to "GitModel" format for prevent posible problems with others models in the application.

[Minor Priority]
* Make helpers to display the commits history and graphs for the log.
* Document the classes and functions. (Doxygen)
7 changes: 3 additions & 4 deletions controllers/repositories_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ class RepositoriesController extends AppController {
var $name = 'Repositories';

function index() {
$this->set('repositories', $this->Repository->find('first'));
$commits = $this->Repository->Commit->find('first', array('conditions' => array('repository' => 'project2')));
debug($commits);
$this->set('commits', $commits);
$this->set('repositories', $this->Repository->find('all'));
$this->set('commits', $this->Repository->Commit->find('all', array('conditions' => array('repository' => 'project2'), 'limit' => 4)));
$this->set('files', $this->Repository->GitFile->find('all', array('conditions' => array('repository' => 'gitosis-admin', 'path' => 'keydir'))));
}
}
?>
80 changes: 62 additions & 18 deletions libs/git.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function getCommits($conditions = array(), $limit = 0) {
if($limit > 0)
$options['count'] = $limit;

if(isset($conditions['hash'])) {
$options['since'] = $conditions['hash'];
$options['count'] = 1;
}

if(isset($conditions['repository'])) {
$repo_path = $this->config['repo_directory'] . $conditions['repository'] . $this->config['repo_suffix'];
$results = $this->getLastNCommits($repo_path, $options);
Expand All @@ -86,6 +91,60 @@ public function getCommits($conditions = array(), $limit = 0) {

return $results;
}

public function getFiles($conditions = array(), $limit = 0) {
$files = array();

$commit = (isset($conditions['commit']) ? $conditions['commit'] : 'HEAD');
$repository = '';
if(isset($conditions['repository']))
$repository = $this->config['repo_directory'] . $conditions['repository'] . $this->config['repo_suffix'];
$path = (isset($conditions['path']) ? $conditions['path'] : '');
$name = (isset($conditions['name']) ? $conditions['name'] : '');
$recursive = $name == '';

if($repository == '') {
} else {
$files = $this->lsTree($repository, $commit . ':' . $path, $name, $recursive);

foreach($files as $file_k => $file_v)
$files[$file_k]['repository'] = $conditions['repository'];
}

foreach($files as $file_k => $file_v) {
$files[$file_k]['file'] = basename($file_v['path']);
$files[$file_k]['path'] = dirname($file_v['path']);
$files[$file_k]['commit'] = $commit;
}

return $files;
}

public function lsTree($repo, $tree, $file = '', $recursive = false) {
$out = array();
$cmd = "GIT_DIR=" .$repo . " {$this->config['git_binary']} ls-tree " . $tree . " 2>&1";

if($recursive)
$cmd .= ' -r -t';

if($file != '')
$cmd .= " | grep {$file}";

//Have to strip the \t between hash and file
$cmd .= " | sed -e 's/\t/ /g'";

exec($cmd, &$out);

$results = array();
foreach ($out as $line) {
$results[] = array_combine(
array('perm', 'type', 'hash', 'path'),
explode(" ", $line, 4)
);
}

return $results;
}

public function getOwner($path) {
$out = array();
Expand Down Expand Up @@ -172,8 +231,8 @@ public function repoPath($proj) {
return false;
}

public function shortlogs($config, $proj) {
return self::getLastNCommits($config, $proj);
public function shortlogs($proj) {
return self::getLastNCommits($proj);
}

public function commit($config, $proj, $commit) {
Expand All @@ -184,22 +243,7 @@ public function commit($config, $proj, $commit) {
return self::getLastNCommits($config, $proj, $options);
}

public static function lsTree($config, $proj, $tree) {
$out = array();
//Have to strip the \t between hash and file
$cmd = "GIT_DIR=" . self::$repos[$proj] . $config['repo_suffix'] . " {$config['git_binary']} ls-tree " . $tree . " 2>&1 | sed -e 's/\t/ /g'";

exec($cmd, &$out);

$results = array();
foreach ($out as $line) {
$results[] = array_combine(
array('perm', 'type', 'hash', 'file'),
explode(" ", $line, 4)
);
}
return $results;
}


public static function diff($config, $proj, $commit) {
$out = array();
Expand Down
95 changes: 56 additions & 39 deletions models/datasources/git_source.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class GitSource extends DataSource {
'key' => 'primary',
'length' => 100,
),
'id' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 100,
),
'path' => array(
'type' => 'string',
'null' => true,
Expand Down Expand Up @@ -40,7 +46,7 @@ class GitSource extends DataSource {
'key' => 'primary',
'length' => 100,
),
'parenta' => array(
'parent' => array(
'type' => 'array(string)',
'null' => true
),
Expand All @@ -64,12 +70,51 @@ class GitSource extends DataSource {
'type' => 'datetime',
'null' => true
),
),
'git_files' => array(
'hash' => array(
'type' => 'string',
'null' => true,
'key' => 'primary',
'length' => 100,
),
'perm' => array(
'type' => 'string',
'null' => true,
'length' => 10,
),
'type' => array(
'type' => 'string',
'null' => true,
'length' => 10,
),
'path' => array(
'type' => 'string',
'null' => true,
'length' => 500
),
'name' => array(
'type' => 'string',
'null' => true,
'length' => 200,
),
'commit' => array(
'type' => 'string',
'null' => true,
'length' => 200
),
'repository' => array(
'type' => 'string',
'null' => true,
'length' => 200
)
)
);

protected $_model_table_map = array(
'Repository' => 'repositories',
'Commit' => 'commits'
'Commit' => 'commits',
'GitFile' => 'git_files'
);

public function __construct($config) {
Expand Down Expand Up @@ -97,8 +142,6 @@ public function read($model, $queryData = array()) {
'description' => file_get_contents("{$repository_path}description"),
'owner' => $this->fileOwner($repository_path),
'last_change' => $this->lastChange($repository_path),
'today' => $stats['today'],
'total' => $stats['total']
);

$results[] = $repo;
Expand All @@ -112,6 +155,14 @@ public function read($model, $queryData = array()) {
$results[] = $temp;
}
break;
case 'GitFile':
$files = $git->getFiles($queryData['conditions']);

foreach($files as $file) {
$temp['GitFile'] = $file;
$results[] = $temp;
}
break;
case 'Default':
debug('No se encuentra la tabla para el modelo "' . $model->name . '".');
break;
Expand All @@ -131,51 +182,17 @@ public function describe($model) {

return $result;
}

/* function findAll() {
list($repositories, $valid) = Git::loadRepositories($this->_config);
$repos = array();
foreach ($repositories as $repository) {
$repo = array(
'link' => $this->link($repository),
'description' => file_get_contents("{$repository}{$this->_config['repo_suffix']}description"),
'owner' => $this->fileOwner($repository),
'last_change' => $this->lastChange($repository),
'download' => $this->link($repository, array('download' => true)),
);
$stats = $this->getStats($repository, 0, 0);
$repo['today'] = $stats['today'];
$repo['total'] = $stats['total'];
$repos[] = $repo;
}
return $repos;
}*/

function link($repo, $options = array()) {
$options = array_merge(array(
'download' => false,
'tag' => 'HEAD',
), $options);

$path = basename($repo);
if ($options['download']) {
return sprintf('<a href="/%s/downloads/%s">snapshot</a>', $path, $options['tag']);
}
return sprintf('<a href="/%s">%s</a>', $path, $path);
}

function fileOwner($repo) {
$out = array();
//$cmd = "GIT_DIR=" . escapeshellarg($repo) . " {$this->_config['git_binary']} rev-list --header --max-count=1 HEAD 2>&1 | grep -a committer | cut -d' ' -f2-3";
$cmd = "GIT_DIR=" . escapeshellarg($repo) . " {$this->_config['git_binary']} rev-list --pretty=format:'commiter: %ce' --max-count=1 HEAD 2>&1 | grep commiter | cut -c11-";
$own = exec($cmd, &$out);
return $own;
}

function lastChange($repo) {
$out = array();
$cmd = "GIT_DIR=" . escapeshellarg($repo . $this->_config['repo_suffix']) . " {$this->_config['git_binary']} rev-list --header --max-count=1 HEAD 2>&1 | grep -a committer | cut -d' ' -f5-6";
$cmd = "GIT_DIR=" . escapeshellarg($repo) . " {$this->_config['git_binary']} rev-list --pretty=format:'date: %at' --header HEAD --max-count=1 | grep date | cut -d' ' -f2-3";
$date = exec($cmd, &$out);
return date('d-m-Y', (int) $date);
}
Expand Down
13 changes: 13 additions & 0 deletions models/git_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
class GitFile extends AppModel {
var $name = 'GitFile';
public $useDbConfig = 'git';

var $belongsTo = array(
'Repository' => array(
'classname' => 'Repository'
),
'Commit'
);
}
?>
3 changes: 2 additions & 1 deletion models/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Repository extends AppModel {
'Commit' => array(
'classname' => 'Commit',
'limit' => 5
)
),
'GitFile'
);
}
?>
1 change: 1 addition & 0 deletions views/repositories/index.ctp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
debug($repositories);
debug($commits);
debug($files);
?>

0 comments on commit 175a195

Please sign in to comment.