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 --work-tree=$this->path"; |
|
|
|
|
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 -q -b $branch 2>&1"; |
75
|
|
|
$result = Cmd::run( $cmd ); |
76
|
|
|
|
77
|
|
|
return $result['output']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Tag new version |
82
|
|
|
* |
83
|
|
|
* @param String $name tag name. |
84
|
|
|
*/ |
85
|
|
|
public function tag_new_version( $name ) { |
86
|
|
|
$cmd = "git $this->dir_arg tag -a $name -m 'New release for $name' 2>&1"; |
87
|
|
|
$result = Cmd::run( $cmd ); |
88
|
|
|
|
89
|
|
|
return $result['output']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get status |
94
|
|
|
*/ |
95
|
|
|
public function status() { |
96
|
|
|
$result = Cmd::run( "git $this->dir_arg status --porcelain --untracked-files=no" ); |
97
|
|
|
|
98
|
|
|
return $result['output']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Commit all changes |
103
|
|
|
*/ |
104
|
|
|
public function commit() { |
105
|
|
|
$result = Cmd::run( "git $this->dir_arg commit -a -m 'Add changes'" ); |
106
|
|
|
|
107
|
|
|
return $result['output']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Checkout to a new branch |
112
|
|
|
* |
113
|
|
|
* @param String $branch branch name. |
114
|
|
|
*/ |
115
|
|
|
public function push_to_remote( $branch ) { |
116
|
|
|
$cmd = "git $this->dir_arg push --set-upstream origin $branch 2>&1"; |
117
|
|
|
$result = Cmd::run( $cmd ); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$cmd = "git $this->dir_arg push origin --tags 2>&1"; |
120
|
|
|
$result = Cmd::run( $cmd ); |
121
|
|
|
|
122
|
|
|
return $result['output']; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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: