Completed
Push — add/relase-scripts-package ( 6d2bc4 )
by Yaroslav
29:20 queued 22:51
created

Git_Shell_Command::checkout_new_branch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The property name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
		$this->path    = explode( '/', $name )[1];
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
		$this->dir_arg = "--git-dir=$this->path/.git";
0 ignored issues
show
Bug introduced by
The property dir_arg does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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