Tag Archives: git

Running composer in a git hook

If you’re trying to run composer from a git hook but it’s failing with “Failed to clone http://github.com/fabpot/Pimple.git via git, https and http protocols, aborting”, make sure you unset the GIT_DIR env variable in your script.

Mine looks like this (post-receive hook in bash):

if [ -f “/path/to/composer/directory/composer.phar” ]; then
  unset GIT_DIR && cd “/path/to/composer/directory” && php composer.phar install
fi

 

And full error:

remote: Loading composer repositories with package information[K
remote: Updating dependencies[K
remote:   – Installing pimple/pimple (dev-master b9f27b)[K
remote:     Cloning b9f27b8dc18c08f00627dec02359b46a24791dc3[K
remote: Initialized empty Git repository in /home/bootstrap/publichtml/dtest/php/lib/vendor/pimple/pimple/.git/[K
remote: Initialized empty Git repository in /home/bootstrap/publichtml/dtest/php/lib/vendor/pimple/pimple/.git/[K
remote: Initialized empty Git repository in /home/bootstrap/publichtml/dtest/php/lib/vendor/pimple/pimple/.git/[K
remote:
remote:
remote:                                                                                                     [K
remote:   [RuntimeException]                                                                                [K
remote:   Failed to clone http://github.com/fabpot/Pimple.git via git, https and http protocols, aborting.  [K
remote:                                                                                                     [K
remote:   – git://github.com/fabpot/Pimple.git                                                              [K
remote:     fatal: Not a git repository: ‘.’                                                                [K
remote:                                                                                                     [K
remote:   – https://github.com/fabpot/Pimple.git                                                            [K
remote:     fatal: Not a git repository: ‘.’                                                                [K
remote:                                                                                                     [K
remote:   – http://github.com/fabpot/Pimple.git                                                             [K
remote:     fatal: Not a git repository: ‘.’                                                                [K
remote:                                                                                                     [K
remote:                                                                                                     [K
remote:
remote:
remote: Exception trace:[K
remote:  () at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Downloader/GitDownloader.php:224[K
remote:  Composer\Downloader\GitDownloader->throwException() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Downloader/GitDownloader.php:168[K
remote:  Composer\Downloader\GitDownloader->runCommand() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Downloader/GitDownloader.php:37[K
remote:  Composer\Downloader\GitDownloader->doDownload() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Downloader/VcsDownloader.php:56[K
remote:  Composer\Downloader\VcsDownloader->download() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Downloader/DownloadManager.php:150[K
remote:  Composer\Downloader\DownloadManager->download() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer/LibraryInstaller.php:151[K
remote:  Composer\Installer\LibraryInstaller->installCode() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer/LibraryInstaller.php:85[K
remote:  Composer\Installer\LibraryInstaller->install() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer/InstallationManager.php:132[K
remote:  Composer\Installer\InstallationManager->install() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer/InstallationManager.php:119[K
Done
remote:  Composer\Installer\InstallationManager->execute() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer.php:495[K
remote:  Composer\Installer->doInstall() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Installer.php:180[K
remote:  Composer\Installer->run() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Command/UpdateCommand.php:76[K
remote:  Composer\Command\UpdateCommand->execute() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:239[K
remote:  Symfony\Component\Console\Command\Command->run() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/vendor/symfony/console/Symfony/Component/Console/Application.php:193[K
remote:  Symfony\Component\Console\Application->doRun() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Console/Application.php:96[K
remote:  Composer\Console\Application->doRun() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/vendor/symfony/console/Symfony/Component/Console/Application.php:106[K
remote:  Symfony\Component\Console\Application->run() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/src/Composer/Console/Application.php:72[K
remote:  Composer\Console\Application->run() at phar:///home/bootstrap/publichtml/dtest/php/lib/composer.phar/bin/composer:37[K
remote:  require() at /home/bootstrap/publichtml/dtest/php/lib/composer.phar:15[K

git detaching head on latest commit of particular branch

I was following the answer of this SO question: http://stackoverflow.com/questions/1464642/git-merge-squash-repeatedly

It worked perfectly on my 11.10 machines (git 1.7.4.1), but would not work on a 10.04 machines (git 1.7.0.4)

The problem was this command: git checkout development@{0}
My git could not understand the curly braces – I guess it must be a git version problem, but I’ve got the most updated version of git for Ubuntu 10.04 from the Ubuntu repositories and I didn’t want to upgrade to see.

(Another post mentions use of the curly braces, like this: {now} – http://sitaramc.github.com/concepts/detached-head.html)

Anyway, solution: git checkout `git rev-parse development`

Getting git to deploy your site in branch folders via post-receive

Aim:  get the site deployed like so; [webroot]/[branch_name]

I’m using this on our development server, so the devs can view all branches on it.

I first set our server up as described in this previous post on deploying with virtualmin.  Then use the following in hooks-receive:

#!/bin/sh
#get the ref name
while read oldrev newrev ref
do
refbranch=”$ref”
done
#remove ref path</h1>
branch=`basename $refbranch`
#add full path
branchpath=”[your_webroot_full_path]/$branch”
#create directory if it doesn’t exist
if [ ! -d “$branchpath” ]; then
mkdir “$branchpath”
echo “Created new folder for branch $branch”
fi
cd “$branchpath”

git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” checkout -f “$branch”
git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” clean -fd
echo “Updated files in $branch directory”

Once you’ve done that, you may want to add a file to the web root so people know what branches they can view.  I’m using the following very simple php script (index.php, in my webroot):

<?php

$dirs = scandir(‘.’);
$remove = array(‘.’,’..’,’.gitignore’,’.project’,’index.php’,’git’);
$html = ”;
foreach($dirs as $dir){
  if(!in_array($dir,$remove)){
    $date = date(‘jS M Y’,filemtime($dir));
    $html .= ‘<a href=”/’ . $dir . ‘”>’ . $dir . ‘</a> – ‘ . $date . ‘<br />’;
  }
}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<title>Development site</title>
</head>

<body>
<p>Please choose your branch (branch name – last modified):<br />
<?php echo $html; ?>
</p>
</body>

</html>

Add to the $remove array any files/directories you do not want showing up.

Auto-deploy your Virtualmin website with git

Virtualmin’s non-standard implementation of git can be a little awkward to work with, primarily because git itself assumes too many things about the environment by default. However, there is a very easy way to auto-deploy your website from a virtualmin git repo to your virtualmin website root (public_html) using git hooks.

This assumes you have installed git onto your virtualmin server, and will be accessing the remote (virtualmin) repository over ssh (you can also use any protocol that works with git hooks; http doesn’t!)

  1. Create a new virtual server in virtualmin, and set up a repository for it
  2. On the virtualmin server (ssh into it, or whatever method you prefer), navigate to the git directory (/home/sitename/public_html/git/git.git/), and execute the following commands:
    git config core.bare false
    git config receive.denycurrentbranch ignore
  3. Then go to add the hook; go into the hooks directory, create a file called post-receive, and add the following:
    #!/bin/sh
    cd /home/sitename/public_html/
    git –git-dir=/home/sitename/public_html/git/reponame.git –work-tree=/home/sitename/public_html/ checkout -f
  4. Add execute permissions on the hook, and ensure the ownership is correct (www-data:sitename)
  5. And that’s it…get the url for your repo (something like sitename@siteurl:public_html/git/git.git), and set it up on your local copy of git, then create some files and push from local to remote and it should automatically deploy – by the way, you will need to use “git push origin master” the first time you push to the empty repo

I think this will just deploy the master branch, and I don’t think it will delete files that are deleted locally (not sure exactly how the checkout command works).

I’ll be adding another tutorial that incorporates branches and proper sync between the two.

Getting git working in aptana on Ubuntu

  1. Make sure you have sun java installed (see here)
  2. Obviously install git and any languages you want to use
  3. If you do not have firefox installed you’ll need to download webkit/mozilla packages (I’m not exactly sure which)
  4. Install the following:
    • xulrunner-1.9.2-dev (NOT 2.0 – 2.0 seems to rename some things from mozilla to webkit, and aptana wants mozilla)
    • tk8.5 (might work with tk, I haven’t tried
    • python-tk
    • libswt-gtk-*-java (I have 3.6 version)

If you’re getting an error similar to the following:

 Unhandled event loop exception
 No more handles [MOZILLA_FIVE_HOME='/usr/lib/xulrunner-devel-2.0'] (java.lang.UnsatisfiedLinkError: Could not load SWT library ......

Make sure to install the 1.9.2 version of xulrunner instead of 2.0 (installing it should uninstall 2.0).
And if you’re getting an error similar to this:

exec 3 wish not found

Check your tk and python-tk are installed.

 

Once you’ve got rid of the error messages, you’re ready to start playing with git.  Connecting an existing repo to a remote one ([commands icon] -> more -> add remote) was causing my aptana to crash without logging anything, however, creating a new project by importing a remote git repo worked, so let’s do that:
File > Import > Git > Git Repository

If you import an empty remote repo, the first time you try and push your files to the repo you’ll get the following error:

No refs in common and none specified; doing nothing.
Perhaps you should specify a branch such as 'master'.

To fix this, go to the terminal within aptana (or a normal terminal), make sure you’re in your project folder, and type:
git push origin master

This will sync the files, and from then you should be able to push properly.