Skip to content

Commit d66cace

Browse files
committed
Adds an option to check if the repository is aligned with the remote before the deploy
If in the .ini file the option'check_sync_with_remote' is set to true, it will be checked if the remote is aligned with the local repository. If it fails, the deploy will not be performed. It can be useful when there is more than one person working on the repository, because it pervents inconsistent deploys to the production server. If the remote is called 'origin', it is possible to specify the branch of the remote where to check the alignment, defining it through the option 'remote_branch'.
1 parent 89ff1cd commit d66cace

File tree

5 files changed

+84
-4
lines changed

5 files changed

+84
-4
lines changed

git-deploy

102 KB
Binary file not shown.

tools/src/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,18 @@ public static function getServers($config_file) {
7373
'ignore_files' => array(),
7474
'ignore_directories' => array(),
7575
'upload_untracked' => array(),
76+
'check_sync_with_remote' => false,
77+
'remote_branch' => null
7678
), $options);
7779

80+
if ($options['check_sync_with_remote'])
81+
{
82+
if (empty($options['remote_branch']))
83+
{
84+
$options['remote_branch'] = (!empty($options['branch'])) ? "origin/".$options['branch'] : null;
85+
}
86+
}
87+
7888
if (!isset($options['pass']) && !isset($options['sftp_key'])) {
7989
$options['pass'] = self::promptPassword();
8090
}

tools/src/Git.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,47 @@ public function get_changes($target_commit, $current_commit) {
9090
return $return;
9191
}
9292

93+
// http://stackoverflow.com/a/3278427
94+
public function get_status_towards_remote($local_branch, $remote_branch)
95+
{
96+
if (empty($local_branch))
97+
{
98+
$local_branch = "@";
99+
}
100+
101+
if (empty($remote_branch))
102+
{
103+
$remote_branch = "@{u}";
104+
}
105+
106+
$status;
107+
108+
$this->exec("remote update");
109+
110+
$local = $this->exec("rev-parse ".$local_branch);
111+
$remote = $this->exec("rev-parse ".$remote_branch);
112+
$base = $this->exec("merge-base ".$local_branch." ".$remote_branch);
113+
114+
if ($local == $remote)
115+
{
116+
$status = "up-to-date";
117+
}
118+
else if ($local == $base)
119+
{
120+
$status = "pull-needed";
121+
}
122+
else if ($remote == $base)
123+
{
124+
$status = "push-needed";
125+
}
126+
else
127+
{
128+
$status = "diverged";
129+
}
130+
131+
return $status;
132+
}
133+
93134
protected function get_file_contents($path) {
94135
$temp = tempnam(sys_get_temp_dir(), "git-deploy-");
95136
$this->exec('show "' . $path . '"', "> \"$temp\"");

tools/src/Helpers.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public static function logmessage($message) {
1616
fwrite($log_handle, $log);
1717
}
1818

19-
echo $log;
19+
echo "\033[0m".$log;
2020
}
2121

22-
public static function error($message) {
23-
self::logmessage("ERROR: $message");
24-
die;
22+
public static function error($message, $die = true) {
23+
self::logmessage("\033[0;31mERROR: $message");
24+
25+
if ($die)
26+
{
27+
die();
28+
}
2529
}
2630

2731
public static function get_recursive_file_list($folder, $prefix = '') {

tools/src/Server.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ public function __construct($server, $deploy_script = 'deploy.ini') {
2929
}
3030

3131
public function deploy(Git $git, $target_commit, $is_revert = false, $list_only = false) {
32+
33+
if ($this->server['check_sync_with_remote'])
34+
{
35+
$statusTowardsRemote = $git->get_status_towards_remote($this->server['branch'], $this->server['remote_branch']);
36+
37+
if ($statusTowardsRemote != "up-to-date")
38+
{
39+
Helpers::error("In order to deploy, local and remote repository must be in sync.", false);
40+
if ($statusTowardsRemote == "push-needed")
41+
{
42+
Helpers::logmessage("Push your changes to the remote and come back here to retry.");
43+
}
44+
else if ($statusTowardsRemote == "pull-needed")
45+
{
46+
Helpers::logmessage("Pull the changes from the remote and come back here to retry.");
47+
}
48+
else if ($statusTowardsRemote == "diverged")
49+
{
50+
Helpers::logmessage("Pull the changes from the remote, merge them with yours and then push to the repository. Thereafter come back here to retry.");
51+
}
52+
return;
53+
}
54+
}
55+
56+
3257
if ($target_commit == $this->current_commit) {
3358
Helpers::logmessage("Nothing to update on: $this->host");
3459
return;

0 commit comments

Comments
 (0)