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}'
I need to find the number of commits for a directory within a repository. I will be dealing with the result within PHP so would probably need to utilise exec(). It will be running on Linux.
I have a slightly quirky repository where the directory structure looks a little like:
/trunk/section1
/trunk/section2
/trunk/section3 (and so on)
I'm going to use the result as part of the version number for each section. As it stands I use the latest revision but ideally would like to have it numbered sequentially instead of 84 -> 120 -> 400 etc.
I plan on counting the commits and using '3' instead of '400', using the above example.
If you really like to count the number of commits just use the following:
svn log -q --stop-on-copy URL/trunk | grep "^r" | wc -l
will printout the number of commits.
I'm not familiar with how to invoke this with php, but you can use the svn log --xml path/to/repository/trunk/section1 command to get the result in xml format. From there it shouldn't be too difficult to count the number of <logentry> nodes in the output.
I have been trying to get my php to be able to create sql procedure files. My page does everything just as it needs to. It even writes to file correctly, but now I need it to add the file to svn and then also commit. I have been trying
system("svn commit", $trash);
but I cannot seem to get it to work. I have been able to get
system("svn add ".$fileName);
to work and i get common "A................fileName added" or what ever it says. I think that when I try to commit the file svn is waiting for authentication or something, I think this because I don't get anything back in the $trash...
All help is appreciated, thanks.
There is a PECL extension for working with SVN:
svn_commit — Sends changes from the local working copy to the repository
Example from Manual:
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, 'Bob');
svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, 'abc123');
var_dump(svn_commit('Log message of Bob\'s commit', array(realpath('calculator'))));
There is also a PEAR package:
http://pear.php.net/package/VersionControl_SVN
VersionControl_SVN is a simple OO-style interface for Subversion, the free/open-source version control system. VersionControl_SVN can be used to manage trees of source code, text files, image files -- just about any collection of files.
You can go with a full svn commit command line like :
svn commit --username USER --password PASS
Further options available at : http://svnbook.red-bean.com/en/1.1/re06.html
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.
I want to have my PHP application labeled with the revision number which it uses, but I don't want to use CruiseControl or update a file and upload it every time. How should I do it?
SVN keywords is not a good solution. As others pointed out adding $Revision$ in a file only affects the specific file, which may not change for a long time.
Remembering to "edit" a file (by adding or removing a blank line) before every commit is pointless. You could as well just type the revision by hand.
One good way to do it (that I know of) is to have an automated deployment process (which is always a good thing) and using the command svnversion. Here is what I do:
Wherever I need the revision I do an include: <?php include 'version.php'; ?>. This "version.php" file only has the revision number. Moreover it is not part of the repository (it set to be ignored). Here is how I create it:
1) On projects where SVN is installed on the server, I also use it for deployment. Getting the latest version to the server I have a script that among other things does the following (it runs on the server):
cd /var/www/project
svn update
rm version.php
svnversion > version.php
2) On projects where SVN is not installed my deployment script is more complex: it creates the version.php file locally, zips the code, uploads and extracts it
Assuming your webroot is a checked-out copy of the subversion tree, you could parse the /.svn/entries file and hook out the revision number (4th line here)...
In PHP:
$svn = File('.svn/entries');
$svnrev = $svn[3];
unset($svn);
This is how I got it to work.
If your server is setup to allow shell_exec AND you have SVN installed just run:
$revision = `svnversion`;
or
$revision = shell_exec('svnversion');
From this answer:
You can do it by adding the following
anywhere in your code
$Id:$
So for example Jeff did:
<div id="svnrevision">svn revision: $Id:$</div>
and when checked in the
server replaced $Id:$ with the current
revision number. I also found this reference.
There is also $Date:$, $Rev:$,
$Revision:$
Bit late now, but use a Subversion post-commit hook. In your repository's hooks folder, create a shell script like this one:
#!/bin/bash
REPOS="$1"
REV="$2"
cd /web/root
rm -f /web/root/templates/base.html
/usr/bin/svn update
/bin/sed -i s/REVISION/$REV/ /web/root/templates/base.html
This particular example assumes your live site is in /web/root and the development code is held elsewhere. When you commit a dev change, the script deletes the prior live template (to avoid conflict messages), runs the update and replaces occurrences of REVISION in the template with the actual revision number.
More on hooks here
In most cases the code on the server would actually contain an "Export" of the code, not a checkout, and therefore not contain the .svn folders. At least that's the setup I see most often. Do others actually check out their code onto the web server?
You can get close with SVN Keywords. Add $Revision$ where you want the revision to show, but that will only show the last revision that particular file was changed, so you would have to make a change to the file each time. Getting the global revision number isn't possible without some sort of external script, or a post-commit hook.
You could also do it like this:
$status = #shell_exec('svnversion '.realpath(__FILE__));
if ( preg_match('/\d+/', $status, $match) ) {
echo 'Revision: '.$match[0];
}
The easiest way is to use the Subversion "Keyword Substitution". There is a guide here in the SVN book (Version Control with Subversion).
You'll basically just have to add the text $Rev$ somewhere in your file.
Then enable the keyword in your repository. On checkout SVN will substitute the revision number into the file.
See my response to the similar question "Mark" svn export with revision.
If you capture the revision number when you export you can use:
svn export /path/to/repository | grep ^Exported > revision.txt
To strip everything but the revision number, you can pipe it through this sed command:
svn export /path/to/repository | grep ^Exported | sed 's/^[^0-9]\+\([0-9]\+\).*/\1/' > revision.txt
$svn_rev=file_get_contents('/path.to.repository/db/current');
Another possibility to do this is to run a cron that executes the steps described in the "Deploy Process" (assuming it is a *nix/FreeBSD server).
If performance is an issue, then you could do:
exec('svn info /path/to/repository', $output);
$svn_ver = (int) trim(substr($output[4], strpos($output[4], ':')));
This of course depends on your having done a checkout, and the presence of the svn command.