php cygwin ? how to shell_exec - php

ok i know this is stupid but atleast im trying :)
$result = shell_exec('C:/cygwin/bin/bash.exe /c --login -i git');
var_dump($result);
somehow i cant get git command from cygwin, anyone ?
Adam Ramadhan
edit*
it should give
usage: git [--version]
[--exec-path[=GIT_EXEC_PATH]]
[--html-path]
[-p|--paginate|--no-pager] [--no-replace-objects]
[--bare] [--git-dir=GIT_DIR]
[--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]
The most commonly used git commands
are: add Add file contents
to the index bisect Find by
binary search the change that
introduced a bug branch List,
create, or delete branches checkout
Checkout a branch or paths to the
working tree clone Clone a
repository into a new directory
commit Record changes to the
repository diff Show changes
between commits, commit and working
tree, etc fetch Download
objects and refs from another
repository grep Print lines
matching a pattern init
Create an empty git repository or
reinitialize an existing one log
Show commit logs merge Join
two or more development histories
together mv Move or rename
a file, a directory, or a symlink
pull Fetch from and merge with
another repository or a local branch
push Update remote refs along
with associated objects rebase
Forward-port local commits to the
updated upstream head reset
Reset current HEAD to the specified
state rm Remove files from
the working tree and from the index
show Show various types of
objects status Show the working
tree status tag Create,
list, delete or verify a tag object
signed with GPG
See 'git help COMMAND' for more
information on a specific command.
as the value

-i starts an interactive shell, you don't want that
/c does probably not work
use -c command to run a command, you have /c --login
try c:\\cygwin\\bin\\git.exe, or the complete path to git.

Related

How can I get a list of git repositories, sorted by commit time?

I set up my own server to host some private git repositories.
I am working on a visual representation (using a php backend) of my private 'repo base'.
It all works fine and I may open-source it at some point.
My current issue: Creating a list of all repositories (I did that already), which is sorted by the last commit times (to the master-branches).
I currently use this function to get a list of my (bare) git repositories:
function listRepositories() {
global $SYSTEM_PATH;
exec("ls $SYSTEM_PATH/repos/ -t", $repoDirectories);
$repos = array_map(function($repo) {
return str_replace("$SYSTEM_PATH/repos/", "", $repo);
}, $repoDirectories);
return $repos;
}
However, the ls $SYSTEM_PATH/repos/ -t command I currently use to get a list of repos does not sort them the way I want.
If I copy older repos into the repository directory ($SYSTEM_PATH/repos/), they appear on top of the list.
This command returns the last commit time (unix timestamp):
git log -1 --format=%ct
However, I did not yet manage to combine all that to achieve what I want in a efficient way.
Thank you for helping me achieve it, I am grateful for everything this community taught me!
Related Questions:
Last git commit time: How do I get last commit date from git repository?
The same for branches: How can I get a list of Git branches, ordered by most recent commit?
You need to go into every repository in a loop, output the last commit timestamp in a sortable format (let's use unix time), sort and output sorted repo names:
find $SYSTEM_PATH/repos/ -maxdepth 1 -type d |
while read repo; do
git -C "$repo" --no-pager log -1 --format="%ct $repo" 2>/dev/null
done | sort -nr | awk '{print $2}'

How do I give cmd permissions to apache?

I'm working on a php application that pulls github repositories to the server. Github webhooks call the php files.
I want to execute a cmd command using php. I assume I need apache permissions but I don't know how to give them.
The php code below creates an mkdir.bat and a gitclone.bat and runs them. The mkdir runs successfully, it creates an empty folder, but the gitclone doesn't create any folders or files. When I run the gitclone manually it does create folders and files.
file_put_contents("mkdir.bat", "mkdir test");
exec("mkdir.bat");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat");
I executed your code in my computer
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
the extra code is to check the output from cmd and it returned "git command not exsists"
so you only have to add a line as
<?php
putenv("PATH=C:\Program Files\Git\cmd");
file_put_contents("gitclone.bat", "git clone https://github.com/gutyina700/WPTG.git");
exec("gitclone.bat 2>&1",$o);
print_r($o);
?>
This was working properly.

Get git commit details without "git" command

First, I've seen and read Git commit date.
How can I get the date of the current HEAD commit in git repo without execution any "git" command? Example: I'm execution commands from PHP and I cannot call exec, but I want to get current commit details.
I have access to .git folder of this repo, but it doesn't seem to be useful for commit details. I was only able to get:
Current branch from .git/HEAD
Current commit id (SHA) from .git/refs/heads/<branch>
But no commit message and no commit date. Is there any way to get it from the fileset?
You can't.
Well, you might be able to, but there are too many complex issues.
Specifically, each Git object is stored in a database of all objects, indexed by hash-ID keys. Some objects are loose: stored as separate files. Such a file is zlib-deflated. Any zlib-inflator can read this file and turn the compressed data into readable, useful data, and what you will see if you do this with a commit hash ID is the actual commit object content:
$ git cat-file -p HEAD | sed 's/#/ /'
tree 1fd4a47af4942cbdee0bdcb4375612ab521a4a51
parent 5571d085b3c9c2aa9470a10bcf2b8518d3e4ec99
author Junio C Hamano <gitster pobox.com> 1531941857 -0700
committer Junio C Hamano <gitster pobox.com> 1531941857 -0700
Third batch for 2.19 cycle
Signed-off-by: Junio C Hamano <gitster pobox.com>
(Here I have used a Git command to do the job, which violates your rule.) The dates are those two time stamps on the author and committer lines. You can simply decode them however you like, though remember to use the time zone offset, -0700 here, as well. (The internal content is prefixed with a header, in this case commit <size>\0, as all Git objects have headers.)
But loose is not the only way objects get stored. Once an object has been lying around loose for a while, Git will eventually pack the object into a pack file. The format of pack files is quite complicated. It is documented—see the Git technical documentation file—but it's subject to change and not worth coding when git cat-file -p already does all the work for you.
So, just use a Git command. This does mean you need Git installed, but you probably need that anyway.
Can't you do something like this. ORS is the line separator just in case you have multiple rows in the file.
awk 'BEGIN { ORS=" " }; { print $1 }' ./.git/FETCH_HEAD

php exec() - git push origin master not working, but other git commands are working

I used below code :
echo exec("git add . "); //this is working
echo exec("git commit -am 'first commit' "); //also working
echo exec("git push origin master"); //NOT WORKING, also not showing any error .
I chowned folder permissions from user to www-data . So, some git commands are working but
GIT PUSH ORIGIN MASTER
is not working from php exec . What is the solution ?
Also, please tell me why PUSH in exec NOT showing any error or msg, how can i see those msgs .
Also,if possible, please provide me any good links for more advanced use of git commands from php exec .
Update :
I also tried this : I added post-commit hook by creating file .git/hooks/post-commit
I added this code to it :
git push origin master
But I didnt get any msg or error after commiting, it just commited but didnt do any push.
Thanks !
I assume the push command will try to push to a remote repository (i.e. not another folder on your system but a remote server behind SSH/HTTPS).
In this case you are most likely missing a HTTPS client certificate HTTPS or an SSH key. Your webserver (and therefore PHP) most likely runs as a different user and does not have access to the private key. Next to that if the private key needs a password it will not work because the exec command is not an interactive session.
Anyway I heavily recommend you to use some sort of bindings and not calling the binaries. Any direct call will start a new process and this is way more inefficient that calls into a library.
Have you checked which branch your are on?
'git status' will tell you.
I'd also recommend working on a branch and not directly on master.
git branch dev_test
git checkout dev_test
git add .
git commit -a -m "first comment"
git push origin dev_test
git checkout master
git merge dev_test
git push origin master

git post-receive hook in php

I want to be able to execute a php hook on post-receive hook, to copy files from the git repo to web folder on the same server and only run if it was pushed was made on a master branch ignoring other branches. Below is what I've got so far.
!/usr/bin/php
<?php
exec("git archive master | tar -x -C /var/www/", $output);
?>
Basically, im not sure how to access git arguments using php.
Don't forget a post-receive hook doesn't take arguments: it reads data on stdin.
The "post-receive" script is run after receive-pack has accepted a pack and the repository has been updated. It is passed arguments in through stdin in the form:
<oldrev> <newrev> <refname>
So you will need to read said arguments to extract the branch (the example is in bash but you can adapt it)

Categories