Question for the git users here

Question for the git users here

by Tim Hunt -
Number of replies: 11
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I read 'Pragmatic version control with Git last night' (recommended) and as a result was thinking about changing my dev environment as I walked into work. There is one of my current CVS setup I can't see how to replicate using git.

Let me explain what I have now.

> ls ~/Workspace
moodle_16 moodle_18 moodle_17
moodle_19 moodle_head

That is my Eclipse workspace, and each of those folders is a CVS checkout of a different Moodle branch. As I understand it, you would not do that with git. Instead, the official line is that git makes jumping from one branch to the other is so quick and easy, so you only need one checked out copy of the code. Also, the workspace contains a copy of your repository, so just one is easier.


However, in order to be able to conveniently test each version, I have my web server set up like this:

tim@tim:/var/www$ ls -l /var/www
lrwxrwxrwx 1 root root 29 2009-01-12 12:54 1.6 -> /home/tim/Workspace/moodle_16
drwxr-xr-x 8 www-data www-data 4096 2009-01-12 12:52 1.6data
lrwxrwxrwx 1 root root 29 2009-01-12 12:54 1.7 -> /home/tim/Workspace/moodle_17
drwxr-xr-x 9 www-data www-data 4096 2009-01-12 12:52 1.7data
lrwxrwxrwx 1 root root 29 2009-01-12 12:54 1.8 -> /home/tim/Workspace/moodle_18
drwxr-xr-x 9 www-data www-data 4096 2009-01-12 12:52 1.8data
lrwxrwxrwx 1 root root 29 2009-01-12 12:54 1.9 -> /home/tim/Workspace/moodle_19
drwxr-xr-x 18 www-data www-data 4096 2009-02-17 13:54 1.9data
lrwxrwxrwx 1 root root 31 2009-01-12 12:54 moodle -> /home/tim/Workspace/moodle_head
drwxr-xr-x 16 www-data www-data 4096 2009-02-11 15:51 moodledata

That means that I can test Moodle 1.9 by going to http://example.com/1.9/ and test Moodle 2.0 dev by going to http://example.com/moodle/ and so on. In each of my workspaces, I have a config.php like /home/tim/moodle_19/config.php with the appropriate paths and database prefix, and the config.php files are in .cvsignore. This is very convenient.

(I am also being bad, since moodledata if accessible from the web, but it's only a dev box.)

Obviously, you could not do this with a single git workspace. Any suggestions?

(I think I could write a config.php that reads version.php and sets paths and DB prefix according to which branch you currently have checked out, which would let you always test the code you are currently working on, but sometimes when I am changing something in HEAD I want to see how it used to work in 1.9.)


(Why does the HTML editor screw up whitespace in <pre>?)

Average of ratings: -
In reply to Tim Hunt

Re: Question for the git users here

by Shane Elliott -
Picture of Core developers Picture of Plugin developers
I'm just adding my +1 to Tim's request for help as I'm looking at doing exactly the same thing.
In reply to Tim Hunt

Re: Question for the git users here

by Penny Leach -
Here's what I do

I actually have subdirectory checkouts for branches that I work on a lot as well, but I also have a generic checkout that has config.php munging to set paths.

I use that one for a quick way to check the state at any given time.

The config.php munging doesn't read version.php, it reads the branch information:

$currentbranch = exec('git branch | grep ^* | awk \' { print $2 }\'');

But an important thing to look into if you're having multiple checkouts, is references.

I have ~/cache/git/moodle/ which is a "bare" checkout (no files, just git info) which is updated from a cronjob.

Then, all new clones, I do with --reference ~/cache/git/moodle/ and it uses the object directory from that checkout.

Which looks like this:

790M cachedirectory
67M somecheckout
67M some other checkout
In reply to Penny Leach

Re: Question for the git users here

by Jonathan Newman -
For multiple simultaneous checkouts, I also use a reference repository (ie, using references that Penny elucidated).

I'm often looking at custom code in many of our branches, and at the moment I have about twenty clones that point to my reference repository.

The size of my shared-git-moodle-r2 directory is 322MB, while a clone of MOODLE_19_STABLE is 111MB.

It's a pain to manually create a new config.php file, moodle-data directory, and postgres database for each checkout, so I wrote a shell script:

-------
#!/bin/sh
# clone BRANCH [INSTANCE]

branch="$1"
instance="$2";

if [ "z$branch" = "z" ] ; then
 echo "Usage: $0 BRANCH [INSTANCE]"
 exit 1
fi

if [ "z$instance" = "z" ] ; then
 instance="$branch"
fi

cd /var/www
echo 'cloning repo'
git clone --reference /var/www/shared-git-moodle-r2/.git git+ssh://git.catalyst.net.nz/var/git/moodle-r2.git $instance
cd $instance
git branch --track $branch origin/$branch
echo 'checking out repo'
git checkout $branch
cp ../config.php .
echo 'creating config.php file'
replace moodle $instance < ../config.php > config.php

echo 'creating db'
createdb $instance

echo 'creating moodledata directory'
sudo mkdir /var/data/$instance
sudo chown www-data:www-data /var/data/$instance
sudo chmod 777 /var/data/$instance

echo 'launching in firefox'
firefox http://localhost/$instance &
------

Note on the usage:

I use 'branch' to reference the branch I'm cloning, and 'instance' for the name of the checkout I want. Say I want to checkout the branch 'MOODLE_19_STABLE' and call it 'moodle_19_stable' or 'mdl19-test':
sudo clone MOODLE_19_STABLE moodle_19_stable
sudo clone MOODLE_19_STABLE mdl19-test

I use a config.php file in /var/www/ for the replace command:

<?php /// Moodle Configuration File

unset($CFG);
$CFG = new stdClass();

$CFG->dbtype = 'postgres7';
$CFG->dblibrary = 'adodb';
$CFG->dbhost = 'user=\'general\' password=\'\' dbname=\'moodle\'';
$CFG->dbname = '';
$CFG->dbuser = '';
$CFG->dbpass = '';
$CFG->prefix = 'mdl_';

$CFG->wwwroot = 'http://localhost/moodle';
$CFG->dirroot = '/var/www/moodle';
$CFG->dataroot = '/var/data/moodle/';
$CFG->admin = 'admin';

$CFG->directorypermissions = 00777; // try 02777 on a server in Safe Mode

require_once("$CFG->dirroot/lib/setup.php");
// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,
// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.
?>

A big thanks to MartinL for presenting the reference repository method to the newer Moodle developers at Catalyst a couple years ago.
In reply to Jonathan Newman

Re: Question for the git users here

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I've shot myself in the foot with this in the past, so I think displaying it in big bold lettes here is a Good Idea(tm). One note of warning for the git newbies with respect to referece repositories (seasoned git users will surely know this smile), directly taken from the git-clone manual page:

NOTE: this is a possibly dangerous operation; do not use it unless you understand what it does. If you clone your repository using this option and then delete branches (or use any other git command that makes any existing commit unreferenced) in the source repository, some objects may become unreferenced (or dangling). These objects may be removed by normal git operations (such as git-commit) which automatically call git gc --auto. (See git-gc(1).) If these objects are removed and were referenced by the cloned repository, then the cloned repository will become corrupt.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Question for the git users here

by Penny Leach -
Thanks Iñaki!

I haven't ever actually done that - I rarely delete branches. It's good to have it here as a warning though smile
In reply to Penny Leach

Re: Question for the git users here

by Dan Poltawski -

Hmm that seems quite a neat solution.

Will it be possible to share commits between checkouts when you use --reference? Err to explain:

  • Create a moodle19 and moodle20 checkout based on a single object directory
  • Commit to moodle19 (and don't push/cvs commit it)
  • git cherry-pick the commit in to the moodle20 checkout
In reply to Dan Poltawski

Re: Question for the git users here

by Penny Leach -
As someone else said, you can add the 19/.git directory as a remote for the 2.0 checkout.

Not sure that's the most elegant approach, but I'm not sure of a better one.
In reply to Tim Hunt

Re: Question for the git users here

by Ashley Holman -
Hey looks like there's lots of people using git! Nice..

So in answer to the original question, use multiple repos if you need multiple working dirs. I think I have about 30 or so on my workstation and so do others by the sounds of it.

The --reference options is just for saving on disk space (and network traffic if you're cloning/fetching from another host). To share commits between the repos, add them as remotes (man git-remote).
In reply to Ashley Holman

Re: Question for the git users here

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Great thanks. It seems there is a clear consensus here.

An actually, I don't think the scary warning applies if you are cloning something like catalyst's moodle-r2.git. I doubt that contains many branches that are deleted. And any branch that is pushed there temporarily, e.g. a dev branch, and later deleted, it is very unlikely that other people would have branched off it.
In reply to Tim Hunt

Re: Question for the git users here

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Tim,

I wasn't refering to catalyst's branches, but your own local branches smile

Saludos. Iñaki.