If you use https instead of ssh you can specify user/password directly in the request:
git clone:
exec("git clone https://user:password@bitbucket.org/user/repo.git");
git pull:
exec("git pull https://user:password@bitbucket.org/user/repo.git master");
Alternatives to exposing your password:
- Use a passwordless ssh key on the target system.
- Use client-side certificates for https.
Update: if you need to get at the exec command output to debug or verify things, you can pass it an array argument, and then output using standard iterative techniques to the location of your choice. Here is an example that simply prints the output:
function execPrint($command) {
$result = array();
exec($command, $result);
print("<pre>");
foreach ($result as $line) {
print($line . "\n");
}
print("</pre>");
}
// Print the exec output inside of a pre element
execPrint("git pull https://user:password@bitbucket.org/user/repo.git master");
execPrint("git status");
Since the $result
array will be appended to, and not overwritten, by exec()
you can also use a single array to keep a running log of the results of exec commands.