Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for if git exists #5444

Merged
merged 2 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions includes/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,19 +1086,20 @@ function version_info($remote = true)
{
global $config;
$output = array();
if ($remote === true && $config['update_channel'] == 'master') {
$api = curl_init();
set_curl_proxy($api);
curl_setopt($api, CURLOPT_USERAGENT, 'LibreNMS');
curl_setopt($api, CURLOPT_URL, $config['github_api'].'commits/master');
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
$output['github'] = json_decode(curl_exec($api), true);
}
list($local_sha, $local_date) = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`));
$output['local_sha'] = $local_sha;
$output['local_date'] = $local_date;
$output['local_branch'] = rtrim(`git rev-parse --abbrev-ref HEAD`);

if (check_git_exists() === true) {
if ($remote === true && $config['update_channel'] == 'master') {
$api = curl_init();
set_curl_proxy($api);
curl_setopt($api, CURLOPT_USERAGENT, 'LibreNMS');
curl_setopt($api, CURLOPT_URL, $config['github_api'].'commits/master');
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
$output['github'] = json_decode(curl_exec($api), true);
}
list($local_sha, $local_date) = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`));
$output['local_sha'] = $local_sha;
$output['local_date'] = $local_date;
$output['local_branch'] = rtrim(`git rev-parse --abbrev-ref HEAD`);
}
$output['db_schema'] = dbFetchCell('SELECT version FROM dbSchema');
$output['php_ver'] = phpversion();
$output['mysql_ver'] = dbFetchCell('SELECT version()');
Expand Down Expand Up @@ -1570,3 +1571,12 @@ function set_numeric($value, $default = 0)
}
return $value;
}

function check_git_exists()
{
if (`which git`) {
return true;
} else {
return false;
}
}
40 changes: 24 additions & 16 deletions validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
exit;
}

$git_found = check_git_exists();
if ($git_found !== true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use if (!$git_found){ instead?

print_warn('Unable to locate git. This should probably be installed.');
}

$versions = version_info();
$cur_sha = $versions['local_sha'];

Expand Down Expand Up @@ -103,9 +108,11 @@
print_fail('You need to run this script as root' . (isset($config['user']) ? ' or '.$config['user'] : ''));
}

if ($config['update_channel'] == 'master' && $cur_sha != $versions['github']['sha']) {
$commit_date = new DateTime('@'.$versions['local_date'], new DateTimeZone(date_default_timezone_get()));
print_warn("Your install is out of date, last update: " . $commit_date->format('r'));
if ($git_found === true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use if ($git_found){ here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== is better, it's more explicit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, as long as we are doing it purposefully, you are the master of the php code ;)

if ($config['update_channel'] == 'master' && $cur_sha != $versions['github']['sha']) {
$commit_date = new DateTime('@'.$versions['local_date'], new DateTimeZone(date_default_timezone_get()));
print_warn("Your install is out of date, last update: " . $commit_date->format('r'));
}
}

// Check php modules we use to make sure they are loaded
Expand Down Expand Up @@ -252,21 +259,22 @@
print_list($devices, "\t %s\n");
}

if ($versions['local_branch'] != 'master') {
print_warn("Your local git branch is not master, this will prevent automatic updates.");
}
if ($git_found === true) {
if ($versions['local_branch'] != 'master') {
print_warn("Your local git branch is not master, this will prevent automatic updates.");
}

// check for modified files
$modifiedcmd = 'git diff --name-only --exit-code';
if ($username === 'root') {
$modifiedcmd = 'su '.$config['user'].' -c "'.$modifiedcmd.'"';
}
exec($modifiedcmd, $cmdoutput, $code);
if ($code !== 0 && !empty($cmdoutput)) {
print_warn("Your local git contains modified files, this could prevent automatic updates.\nModified files:");
print_list($cmdoutput, "\t %s\n");
// check for modified files
$modifiedcmd = 'git diff --name-only --exit-code';
if ($username === 'root') {
$modifiedcmd = 'su '.$config['user'].' -c "'.$modifiedcmd.'"';
}
exec($modifiedcmd, $cmdoutput, $code);
if ($code !== 0 && !empty($cmdoutput)) {
print_warn("Your local git contains modified files, this could prevent automatic updates.\nModified files:");
print_list($cmdoutput, "\t %s\n");
}
}

// Modules test
$modules = explode(',', $options['m']);
foreach ($modules as $module) {
Expand Down