/
Git.php
106 lines (97 loc) · 3.32 KB
/
Git.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Git directory class that stores information about the files in a single
* directory in the repository.
*
* Copyright 2008-2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @package Vcs
*/
class Horde_Vcs_Directory_Git extends Horde_Vcs_Directory_Base
{
/**
* The current branch.
*
* @var string
*/
protected $_branch;
/**
* Constructor.
*
* @todo Throw exception if not a valid directory (ls-tree doesn't really
* list directory contents, but all objects matching a pattern, so it
* returns an empty list when used with non-existant directories.
*
* @param Horde_Vcs_Base $rep A repository object.
* @param string $dn Path to the directory.
* @param array $opts Any additional options:
* - 'rev': (string) Generate directory list for
* a certain branch or revision.
*
* @throws Horde_Vcs_Exception
*/
public function __construct(Horde_Vcs_Base $rep, $dn, $opts = array())
{
parent::__construct($rep, $dn, $opts);
$this->_branch = empty($opts['rev'])
? $rep->getDefaultBranch()
: $opts['rev'];
// @TODO See if we have a valid cache of the tree at this revision
$dir = $this->_dirName;
if (substr($dir, 0, 1) == '/') {
$dir = (string)substr($dir, 1);
}
if (strlen($dir) && substr($dir, -1) != '/') {
$dir .= '/';
}
list($stream, $result) = $rep->runCommand(
'ls-tree --full-name ' . escapeshellarg($this->_branch)
. ' ' . escapeshellarg($dir));
$octchr = function($oct) {
return chr(octdec($oct[1]));
};
/* Create two arrays - one of all the files, and the other of all the
* dirs. */
while (!feof($result)) {
$line = rtrim(fgets($result));
if (!strlen($line)) {
continue;
}
list(, $type, , $file) = preg_split('/\s+/', $line, -1,
PREG_SPLIT_NO_EMPTY);
$file = preg_replace_callback('/\\\\(\d+)/', $octchr, $file);
$file = str_replace(array('\\t', '\\n', '\\\\'),
array("\t", "\n", '\\'),
$file);
$file = trim($file, '"');
if ($type == 'tree') {
$this->_dirs[] = basename($file);
} else {
$this->_files[] = $rep->getFile(
$file,
array('branch' => $this->_branch));
}
}
fclose($result);
proc_close($stream);
}
/**
* Returns a list of all branches in this directory.
*
* @return array A branch list.
*/
public function getBranches()
{
$blist = array_keys($this->_rep->getBranchList());
if (!in_array($this->_branch, $blist)) {
$blist[] = $this->_branch;
}
return $blist;
}
}