Qugstart Blog | Ruby, Rails, Nerdy, Life

TAG | Git

This was done on Fedora 10 in my case, but should be similar for all distributions. This is set up on a shared ‘central’ repository, where changes are pushed to. That’s why it’s done using the post-receive hook (the repository receives changes).

Step 1: Copy (or symlink) the post-receive script, or download it here. The script must be in the ‘hooks’ directory like this:
$GIT_DIR/hooks/post-receive

I’ve read that on Debian systems, for example, the hook is stored in /usr/share/doc/git-core/contrib/hooks/post-receive-email but i had no luck finding it here on my system.

Make sure it is named “post-receive” so that Git recognizes it.

Step 2: Make sure that the script is executable:

chmod a+x $GIT_DIR/hooks/post-receive

Step 3: Edit the first line of $GIT_DIR/description to be your repository name. This will be in the subject of the email.

Step 4: Edit $GIT_DIR/config with some email settings such as recipient / sender email and subject-line prefix. Here’s some basic settings (read the script for all possible settings).

[hooks]
mailinglist = "receiver1@receivers.com, receiver2@receivers.com"
envelopesender = sender@senders.com
emailprefix = "[GIT] "

Note: you can also set these by using git-config:

git-config hooks.mailinglist "receiver1@receivers.com, receiver2@receivers.com"
git-config hooks.envelopesender sender@senders.com
git-config hooks.emailprefix "[GIT] "

That’s it ! Try doing a git-push to your shared repository, and see if you get email notifications. If not, try tailing your mail log to see what’s wrong (/var/log/maillog).

, , , , Hide

Just installed Redmine for our project management / code tracking with Git. I must say it’s pretty nice being able to setup multiple projects easily (unlike Trac). The system is designed fairly well with a very user-friendly interface. And plus, it’s Rails !!

Anyways… too much on Redmine! This post is to describe how to setup a Git repository for use with Redmine.

The important thing to note is that the Git repository MUST BE on the same server as your Redmine setup. So, you probably need to have access to that server!
This was not ideal for me because our Git server does not have Ruby/Rails, etc. installed on it. That’s okay! You just need to clone the repository as a BARE repository on the same machine that your Redmine app is running on. Let’s see how:

Pick a place on your Redmine app server to house all your bare Git repos.
I will choose “/var/local/git_copies” for my example.

Note: Make sure that your permissions allow for your web-user to access these Git repos. My web-user is ‘build’.

# Change to build user (see above)
$ su - build
# Create the directory
$ mkdir /var/local/git_copies

Now, create your git clone as a bare repository clone.
Note: a bare repository will not have your actual files. It will just contain the standard git folders

# Goto your git_copies directory
$ cd /var/local/git_copies
# Make a bare clone of the repo
$ git clone --bare ssh://git@reposerver/usr/local/git_root/foo-project.git

Change into your project and configure remote branch tracking for your local copy.

$ cd foo-project.git
$ git remote add origin ssh://git@reposerver/usr/local/git_root/foo-project.git

Now, normally you want to sync up the repo, but you cannot do a normal git fetch && git merge into a bare repo.
Instead, do a fetch and reset the HEAD to point to the remote branch commit. You need the ‘–soft’ flag or else you will see errors!

$ git fetch origin
$ git reset --soft refs/remotes/origin/master

Now, remember you need to manually sync your new Git repo to have the changes appear on Redmine. A better idea would be to create a cronjob that does this automatically. Especially with more than 1 repository, automating this process will save much time. Here’s the basic idea:

# Add the following to your crontab
*/30 * * * * cd /var/local/git_copies/foo-project.git && git fetch origin && git reset --soft refs/remotes/origin/master > /dev/null

Instead of doing many times in your crontab, maybe it would be easier to setup a bash script to run:

#!/bin/bash
# Not tested! Use at your own risk, and change your GIT_ROOT and "*.git" to fit your setup.
GIT_ROOT=/var/local/git_copies

cd $GIT_ROOT
ls *.git | while read repo; do
  cd $repo && git fetch origin && git reset --soft refs/remotes/origin/master > /dev/null && cd $GIT_ROOT
done

Then, just add this one script to your crontab and have it run every N minutes as you desire!

This site is helpful and worth checking out as well:
Reference on synchronizing 2 git repositories

That’s it, let me know how it goes!

, , Hide

So you have some files or a new Rails application, and you want to add this to a new shared remote Git repository. (I’m assuming you have access to your server and are setting up a remote repo over ssh.)

I know I can never remember how to do it, so here’s a post for me and hopefully you!

Create a local Git repository in your application for your local files.

#On local machine
cd foo_project
git init
git add *
git commit -m "My initial commit message"

Now, create the repository on your Git server. All of my git repositories are owned by a user git and located at /usr/local/git_root/. You can change these things accordingly to match your server setup.

#On remote machine (Git remote repository)
sudo su - git
cd /usr/local/git_root/

Create your new project git repo as a bare Git repository

mkdir foo-project.git
cd foo-project.git/
git --bare init

Make sure permissions are set properly. These are common options I use for my shared repositories

git config core.sharedrepository 1
git config receive.denyNonFastforwards true
find objects -type d -exec chmod 02770 {} \;
#The core.sharedrepository flag tells git to keep everything group readable and writable.
#The receive.denyNonFastforwards flag makes sure that merges can't happen when you push to the repo. You have to do the merges on your local machine, and then push the result.

Now, go back to your local repository, and add the newly created remote repository so it tracks from the remote repository (origin).

#On local machine, in your git project
git remote add origin ssh://git@example.com:2227/usr/local/git_root/foo_project.git
git push origin master

Now, to ensure that your local branch is tracking when you do a fetch, you need to use -f option to force a new local branch to be created even though it already exists.

#Switch to origin/master so you don't get any error about "fatal: Cannot force update the current branch."
git checkout origin/master
#Create the local "master" branch that is tracking the "origin/master" branch
git branch -f master origin/master
#Switch back to your "master" branch
git checkout master

There you have it. You should be able to push changes to origin and fetch changes to your local copy!

, , , Hide

Here’s a little trick to get the name of your current git branch in your shell prompt. No more figuring out that you have made changes to the wrong branch once it’s too late! Your shell will not change for normal (non-git) folders. But, once you change directory into a git project, you will see the branch name added to your shell prompt like such:

Macintosh-2:git_project_foo andyuser [master] $

Simply add the following to your ~/.bash_profile or ~/.bashrc file:

function parse_git_branch_and_add_brackets {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\ \[\1\]/'
}
PS1="\h:\W \u\[\033[0;32m\]\$(parse_git_branch_and_add_brackets) \[\033[0m\]\$ "

That’s all there is to it ! Try it out !

, , , Hide

Git is awesome, but for our Rails projects, we like to follow a similar “centralized repository model” that SVN makes use of.
We want to use distributed Git repositories, with one central “shared” repository to use for deployments, etc.
We don’t want to use Git with a shared SVN repository. (git-svn)
And, we have a bunch of SVN repositories already that we need to migrate to Git repositories.

I’ll describe what I did to make this happen:

Notes, quotes, credits all from: http://notahat.com/posts/25

This is a great post to follow, and accounts for most everything I did here.
The difference is that I’m using svn2git, instead of doing manual git clone commands.
The above post will only set up remote branches, and what I wanted was to have all branches and tags local to my new Git repository.
Svn2git will copy all of your branches / tags from the original SVN repository into your new Git repository as well.
(Check it out here [thanks to nirvdrum]: http://github.com/nirvdrum/svn2git/tree/master)

Let’s say I have an SVN repository structured as:

trunk
...
branches
1.x
2.x
tags
1.0.0
1.0.1
1.0.2
1.1.0
2.0.0

Our goal is to have a git repository with native tags/branches taken from our SVN repository.
At the end of my migration, I want my Git repo to look like:

$ git branch
* master
1.x
2.x
$ git tag -l
1.0.0
1.0.1
1.0.2
1.1.0
2.0.0

#Make sure Git is installed:
#If you don’t have the epel yum repository enabled by default, you must specify to enable it at the command line:
#For other distributions, you know the drill (apt-get, port, etc.)

yum --enablerepo epel install 'git'
yum --enablerepo epel install 'git-svn'

#Now, create a system git user and group

sudo adduser git

#Now edit /etc/group, and add any users who’ll need to access the Git repository to the git group:

...
git:x:100:james,paul
...

#Make a directory for Git repositories
#We chose: /usr/local/git_root

sudo mkdir /usr/local/git_root
sudo chown git.git /usr/local/git_root
sudo chmod 2770 /usr/local/git_root

#Create your svn-authors.txt in /usr/local/git_root/svn-authors.txt to tell Git how to convert SVN usernames in the logs:
#Place *ALL* SVN authors in this file so that git doesn’t complain when it cannot find a particular user to convert.

pbunyan = Paul Bunyan <pbunyan@example.com>
jbond = James Bond <jbond@example.com>

On the left are the Subversion user names; on the right are the Git equivalents.

#Make sure your gem sources include github.com (if necessary)

sudo gem sources -a http://gems.github.com

#Install the svn2git gem

sudo gem install nirvdrum-svn2git

#Verify that you have svn2git in your PATH

which svn2git

#We will create a temporary directory in /tmp/
#This is where we will fetch the SVN repo into a git repo.
#We’ll then use this temporary repo to create our bare repository.

mkdir /tmp/foo-project
cd /tmp/foo-project

#I’m assuming you have the standard SVN setup of

-root
---trunk
---branches
---tags

If not, you can look into the options of svn2git to match your SVN repo structure.

#Run the command given your svn-authors file:

svn2git https://svn-repo/foo-project --authors /usr/local/git_root/svn-authors.txt

#You can go into this folder and verify that you see all your local branches and tags by running:

git branch
and
git tag -l

This should print out the lists of your branches and tags if everything is working right so far!

Next, we want to create our bare repository in /usr/local/git_root.

#Become git user and set permissions

sudo su - git
cd /usr/local/git_root/
umask 007

#Clone the temporary repository created by svn2git to a bare repository.
#<project>.git is a local convention.

git clone --bare /tmp/foo-project foo-project.git
cd foo-project.git/

#Make sure permissions are set properly

git config core.sharedrepository 1
git config receive.denyNonFastforwards true
find objects -type d -exec chmod 02770 {} \;

The core.sharedrepository flag tells git to keep everything group readable and writable.
The receive.denyNonFastforwards flag makes sure that merges can’t happen when you push to the repo. You have to do the merges on your local machine, and then push the result.

#Try to do a git clone from your workstation (another computer)

git clone ssh://git-repo:22/usr/local/git_root/foo-project.git

#Clean up
Once you’re happy that everything is working, delete the temporary repo that you created in /tmp, and you’re done!

, , , , , , Hide

Find it!

Theme Design by devolux.org
To top