diff options
author | Wave <wave72@users.noreply.github.com> | 2016-07-22 10:55:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-22 10:55:02 +0200 |
commit | 744ad84714fe0f7a3d90250a4ff02dc4327b9061 (patch) | |
tree | 595fb74ec9ea0bc7130d18bd7993d719a222d343 /library/kzykhys/git/src/PHPGit/Command/TreeCommand.php | |
parent | c38c79d71c8ef70ef649f83e322f1984b75ee2dd (diff) | |
parent | 7d897a3f03bd57ed556433eb84a41963ba44e02e (diff) | |
download | volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.tar.gz volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.tar.bz2 volse-hubzilla-744ad84714fe0f7a3d90250a4ff02dc4327b9061.zip |
Merge pull request #6 from redmatrix/dev
Dev
Diffstat (limited to 'library/kzykhys/git/src/PHPGit/Command/TreeCommand.php')
-rw-r--r-- | library/kzykhys/git/src/PHPGit/Command/TreeCommand.php | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/library/kzykhys/git/src/PHPGit/Command/TreeCommand.php b/library/kzykhys/git/src/PHPGit/Command/TreeCommand.php new file mode 100644 index 000000000..ea1040a6a --- /dev/null +++ b/library/kzykhys/git/src/PHPGit/Command/TreeCommand.php @@ -0,0 +1,70 @@ +<?php + +namespace PHPGit\Command; + +use PHPGit\Command; + +/** + * List the contents of a tree object - `git ls-tree` + * + * @author Kazuyuki Hayashi <hayashi@valnur.net> + */ +class TreeCommand extends Command +{ + + /** + * Returns the contents of a tree object + * + * ``` php + * $git = new PHPGit\Git(); + * $git->clone('https://github.com/kzykhys/PHPGit.git', '/path/to/repo'); + * $git->setRepository('/path/to/repo'); + * $tree = $git->tree('master'); + * ``` + * + * ##### Output Example + * + * ``` php + * [ + * ['mode' => '100644', 'type' => 'blob', 'hash' => '1f100ce9855b66111d34b9807e47a73a9e7359f3', 'file' => '.gitignore', 'sort' => '2:.gitignore'], + * ['mode' => '100644', 'type' => 'blob', 'hash' => 'e0bfe494537037451b09c32636c8c2c9795c05c0', 'file' => '.travis.yml', 'sort' => '2:.travis.yml'], + * ['mode' => '040000', 'type' => 'tree', 'hash' => '8d5438e79f77cd72de80c49a413f4edde1f3e291', 'file' => 'bin', 'sort' => '1:.bin'], + * ] + * ``` + * + * @param string $branch The commit + * @param string $path The path + * + * @return array + */ + public function __invoke($branch = 'master', $path = '') + { + $objects = array(); + $builder = $this->git->getProcessBuilder(); + $process = $builder->add('ls-tree')->add($branch . ':' . $path)->getProcess(); + $output = $this->git->run($process); + $lines = $this->split($output); + + $types = array( + 'submodule' => 0, + 'tree' => 1, + 'blob' => 2 + ); + + foreach ($lines as $line) { + list($meta, $file) = explode("\t", $line); + list($mode, $type, $hash) = explode(" ", $meta); + + $objects[] = array( + 'sort' => sprintf('%d:%s', $types[$type], $file), + 'mode' => $mode, + 'type' => $type, + 'hash' => $hash, + 'file' => $file + ); + } + + return $objects; + } + +}
\ No newline at end of file |