support for git tags/branches
[pharext/pharext] / src / pharext / Task / GitClone.php
1 <?php
2
3 namespace pharext\Task;
4
5 use pharext\ExecCmd;
6 use pharext\Task;
7 use pharext\Tempdir;
8
9 /**
10 * Clone a git repo
11 */
12 class GitClone implements Task
13 {
14 /**
15 * @var string
16 */
17 private $source;
18
19 /**
20 * @var string
21 */
22 private $branch;
23
24 /**
25 * @param string $source git repo location
26 */
27 public function __construct($source, $branch = null) {
28 $this->source = $source;
29 $this->branch = $branch;
30 }
31
32 /**
33 * @param bool $verbose
34 * @return \pharext\Tempdir
35 */
36 public function run($verbose = false) {
37 if ($verbose !== false) {
38 printf("Fetching %s ...\n", $this->source);
39 }
40 $local = new Tempdir("gitclone");
41 $cmd = new ExecCmd("git", $verbose);
42 if (strlen($this->branch)) {
43 $cmd->run(["clone", "--depth", 1, "--branch", $this->branch, $this->source, $local]);
44 } else {
45 $cmd->run(["clone", $this->source, $local]);
46 }
47 return $local;
48 }
49 }