Git add -A from shell script - php

After a lot of searching, I'm about to tear my hair out on this one and solution might even be dead simple but I've just overlooked it...
I'm trying run a shell script from PHP to git add -A and commit everything in the repository when a button on a web UI is clicked.
<? php
$commitMsg = 'foo';
$output = shell_exec('/Applications/MAMP/htdocs/gitlist/bash/gitlist-commit '.$commitMsg);
#!/bin/bash
cd /var/www/html/development
sudo -H -u username git add -A
sudo -H -u username git commit -m $1
It works on my MAMP/OSX setup, but not on my Ubuntu LTS box. What might I have overlooked?
On the server, I get returned, which I'm guessing means that the git add -A command is just not working. Am I right?
It also works when running directly from the terminal, but not when running via the web UI.
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Any help would be appreciated.

try git add .; git add -u as this will accomplish the same thing "adding all files" but is a potential workaround based on your shell setup.

Related

Need to trigger a Linux Backend script with WordPress Button

I have made a button on wordpress and linked it to a linux backend script.
This gives me no error, and the default user output is "www-data"
#!/bin/bash
whoami
touch file
But I want to trigger a git commit using my local user through this script.
#!/bin/bash
sudo -u my_username -p my_password -H sh -c "cd /var/www/html/forcetalks_new/; /usr/bin/git add . ; touch test_file"
This is neither touching the file not adding the files. I wonder what could be the possible reason. And any other solution is welcomed.
PS. I tried using GIT commands in the first script after giving GIT sudo permissions to "www-data" and that didnt work as well.
Try the first script approach with:
#!/bin/bash
sudo -u my_username -p my_password -H sh -c "/path/to/script > /path/to/log 2>&1"
So a script calling a script:
#!/bin/bash -x
whoami
git status
git add .
touch file
The goal is to see what is going on with /path/to/log, thanks to the bash -x option (and the git status command)
Note that if you want to add "file" itself, you should touch the file first, then add.
Note also that a git commit might be needed at some point to actually persist those changes in the Git repository

'git pull' command work from terminal but not with php shell_exec() via git repository hook

I have create a webhook in my github repository which post on the hook url on my live server to run pull command for update my repo files on the server.
The problem is the hook file which i have created is in the /var/www/site/web/hookfile.php (the post request is going there. i am getting the body response also)
and my repo files are in /var/www/git-repo/
its not updating the git-repo when i push anything to my github repository.
I run this command using terminal and its working.
cd /var/www/git-repo && git pull
But through my php file its not working
shell_exec('cd /var/www/git-repo && git pull')
shell_exec() fail silently because only report STDOUT and not STDERR.
Try with:
echo shell_exec("cd /var/www/git-repo && /full/path/to/bin/git pull 2>&1");
Normally is a permission error, and could be fixed adding permission to the user that execute php (apache?)
chown -R www-agent:www-agent repository/
But could be also a connection error to the remote repository (authentication, ssh-keys, ...).
First of all in your php file run a test against your server instance to get any error messages output on screen because the exec() family of functions simply fail silently and only report STDOUT and not STDERR:
echo shell_exec("cd /website/root/htdocs && git checkout . && git status 2>&1");
In my case this threw an error that it could not find git command due to lack of binary path defined for apache user. Therefore, a full path needs to be provided to git's binary. It can be obtained by finding it manually or running in shell:
'which git'
It returned (further called YOU_FULL_GIT_BINARY_PATH_HERE):
/usr/local/git/bin/git
A full path with git command e.g. '/usr/local/git/bin/git status' now runs git commands nicely.
Another thing is to ensure your web server user has enough permissions to read/write to your repo folder/files. I have set mine to be owned by the apache user (Centos 6.8; other releases might be www:www or www-data:www-data etc.):
chown -R apache:apache YOUR_WEB_OR_REPO_FOLDER
In order to ensure any newly added files inherit correct permissions run:
chmod -R g+s YOUR_WEB_OR_REPO_FOLDER
The above should get your script to run commands now. Though it doesn't overcome git password prompt to use 'git pull' command for a git user set in YOUR_WEB_OR_REPO_FOLDER/.git/config file. Running below command inside repo:
git config credential.helper store
command will prompt for password and let you store it locally. Please note your stored password will be unencrypted and protected only by file system e.g. in /root/.git-credentials. This will allow to run 'git pull' without prompting for password.
It's not ideal for my fully automated continuous integration environment deploying test VPS on demand as it requires to manually enter git user (defined in repo's .git/config git) password at least once.
Since my environment should always run on code from remote's origin/master copy I am also running
/YOU_FULL_GIT_BINARY_PATH_HERE/git checkout .
before invoking 'git pull' to ensure any local changes are lost forever alternatively do a hard reset instead using:
/YOU_FULL_GIT_BINARY_PATH_HERE/git fetch origin
/YOU_FULL_GIT_BINARY_PATH_HERE/git reset --hard origin/master

Apache permission for php to run `git pull`

I'm running in trouble with my new VPS. I created a file pull.php with this code in my GIT folder:
<?php
$output = shell_exec('git pull');
echo "<pre>$output</pre>";
?>
It was working fine on my shared hosting but on my VPS it's return null and no 'pull' command executed. When I change the command 'git pull' to 'git status' it's show result :
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files: (use "git add ..." to include in what will be
committed)
info.php pull.php test.txt
nothing added to commit but untracked files present (use "git add" to
track)
My folder chmod to 777 and php seems can write on it properly. 'git pull' on SSH's fine. My server : Ubuntu 14.x, Apache2, php5
I would very much appreciate any help!
Probably git pull is throwing an error but the errors are not printed by default. To fix this change this line
$output = shell_exec('git pull');
for this one:
$output = shell_exec('git pull 2>&1');
I find it weird you get no output nothing...
do you have root access?
try as root in the right folder: sudo -u apache git pull
see if you get any output then.
You should add following line to /etc/sudoers
apache2 ALL=(root) NOPASSWD: /usr/bin/git
This allows apache to execute git as root and therefore access the files
then call git command (proper executable location is preferred)
shell_exec('sudo /usr/bin/git pull /path/to/your/repo');

How to change directory and run git commands using PHPseclib?

I'm trying to use PHPseclib to SSH and run commands on the remote server. I want to change directory and commands like git pull or clone. Is there a way to do this? I know that "cd" doesn't work well with exec. So any alternatives to this?
Thanks
You don't need to change folder, only to specify it to your git command.
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo remote add xxx
git --git-dir=/path/to/repo/.git --work-tree=/path/to/repo pull
Since git 1;8.5 (if your server hasd a recent enough git version installed), you even can use the short version (detailed here)
git -C /path/to/repo remote add xxx
git -C /path/to/repo pull

Sever can't git pull but I can

I'm trying to use a post-receive hook to update a remote server. I'm using ssh and everything works great (running git pull does indeed pull). I've started with this deploy.php and the other commands, like git status, do run and output as expected, but the pull doesn't appear to do anything at all.
I've chowned the hell out of everything (the git repo, known_hosts, etc...), so my www-data user should be able to access just about anything on the sever. Is there any way I can at least find some error logging for what's going wrong?
edit: I don't see any change in the output after changing git pull to git pull --verbose as suggested by adder. The output is still:
$ whoami
www-data
$ git pull --verbose
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# deploy.php
nothing added to commit but untracked files present (use "git add" to track)
Solution: the apache user needs to be setup to ssh to github. I was connecting as root then getting confused when my post-receive hook target couldn't make the same update. I was doing it as root, the script was running as www-data.
git help pull
--verbose
Pass --verbose to git-fetch and git-merge.

Categories