|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Git shell commands |
|
4
|
|
|
* |
|
5
|
|
|
* @package automattic/jetpack-scripts |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Scripts; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Wrapper around some git commands |
|
12
|
|
|
*/ |
|
13
|
|
|
class Git_Shell_Command { |
|
14
|
|
|
/** |
|
15
|
|
|
* Constructor! |
|
16
|
|
|
* |
|
17
|
|
|
* @param String $name repository name. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct( $name ) { |
|
20
|
|
|
$this->name = $name; |
|
|
|
|
|
|
21
|
|
|
$this->path = explode( '/', $name )[1]; |
|
|
|
|
|
|
22
|
|
|
$this->dir_arg = "--git-dir=$this->path/.git"; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Returns the latest repo tag |
|
27
|
|
|
*/ |
|
28
|
|
|
public function get_latest_tag() { |
|
29
|
|
|
$cmd = "git $this->dir_arg describe --abbrev=0"; |
|
30
|
|
|
$result = Cmd::run( $cmd ); |
|
31
|
|
|
|
|
32
|
|
|
return $result['output']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a `shortstat` diff between two tags |
|
37
|
|
|
* |
|
38
|
|
|
* @param String $source git tag or branch. |
|
39
|
|
|
* @param String $target git tag or branch. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function get_diff_between( $source, $target ) { |
|
42
|
|
|
$cmd = "git $this->dir_arg diff $source $target --shortstat"; |
|
43
|
|
|
$result = Cmd::run( $cmd ); |
|
44
|
|
|
|
|
45
|
|
|
return $result['output']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Clones a repository |
|
50
|
|
|
* |
|
51
|
|
|
* @param String $type URL type to use. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function clone_repository( $type = 'ssh' ) { |
|
54
|
|
|
if ( 'ssh' === $type ) { |
|
55
|
|
|
$url = "[email protected]:$this->name.git"; |
|
56
|
|
|
} else { |
|
57
|
|
|
$url = "https://github.com/$this->name.git"; |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
Cmd::run( "rm -rf $this->path" ); |
|
61
|
|
|
|
|
62
|
|
|
$cmd = "git clone $url 2>&1"; |
|
63
|
|
|
$result = Cmd::run( $cmd ); |
|
64
|
|
|
|
|
65
|
|
|
return $result['output']; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checkout to a new branch |
|
70
|
|
|
* |
|
71
|
|
|
* @param String $branch branch name. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function checkout_new_branch( $branch ) { |
|
74
|
|
|
$cmd = "git $this->dir_arg checkout -b release-$branch 2>&1"; |
|
75
|
|
|
$result = Cmd::run( $cmd ); |
|
76
|
|
|
|
|
77
|
|
|
return $result['output']; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: