Cgit download site

Today I was working on a friends git repository when I noticed that the cgit website is down.  Thankfully I have a backup of the current repository if anyone needs it.

This mirror will auto update if the repository comes back, but will also let you download and install cgit on your own server via the following command.

git clone http://git.mattrude.com/mirror/cgit.git

If you run into any problems with this mirror, please let me know in the comments.

Using the Git Stash command

The git stash command is used to store changes to a dirty git tree when pulling changes[ref]http://man.github.com/git/git-stash.html[/ref].

For example, if you are working on a repository, but are not ready to commit your changes, you may run.

git stash

This will store the changes you have made since the last revision and allow you to start back fresh at the point of your last commit.

Once you are done, you may run

git stash pip

That will restore your repository back to where you were when you ran git stash.

Git: Add all remote branches

Adding each remote branch to a local git repository sometimes can be a pain. IF there are many, you have to repeat your self over and over. Here is a quick, copy and past drop into you console, way to add all the remote branches to your local repository.

for b in `git remote show origin |grep tracked |awk '{print $1}'`
do
    LOCALBRANCH=`git branch |sed 's/* //g' |sed 's/  //g' |grep $b`
    if [ "$LOCALBRANCH" != "$b" ]; then
        git branch -t $b origin/$b
    fi
done

Once your done, you should still be in your original branch were you started. You will still need to update each branch by it self. You may also use something like git-up to update all the branches at once.

Git: Configuring native git commit emails

To start out, move the post-receive.sample script from your git hooks directory to just plain post-receive like so:

mv post-receive.sample post-receive

Then in your git config file add the below, below any existing configuration items. Note, you will need to change the URL and repository name (currently named “git-repository”) before this will work.

[hooks]
        mailinglist = email@example.com
        emailprefix = "git-repository: "
        showrev = "t=%s; printf 'https://example.com/secure/?p=git-repoistory;a=commitdiff;h=%%s' $t; echo;echo; git show -C $t; echo;"

And there you go, when you now push to the repository, you will receive an email send to “mailinglist” with the changes made in a nice diff and a link back to your main git repository site.

Setting up CGIT to show root directories only

CGIT is a nice, quick, and easy way of displaying git repositories. After much fighting I figured out how to allow CGIT to display URLs such as http://code.example.com/repository.git (the git is optional).

Below is the Apache config for code.example.com:

<VirtualHost *:80>
    ServerName code.example.com
    DocumentRoot /var/www/code.example.com
    CustomLog logs/code.example.com.access_log combined
    ErrorLog logs/code.example.com.error_log
    SetEnv CGIT_CONFIG          /var/www/code.example.com/cgitrc
    Alias /cgit.css             /var/www/code.example.com/cgit.css
    Alias /cgit.png             /var/www/code.example.com/cgit.png
    Alias /favicon.ico          /var/www/code.example.com/favicon.ico
    Alias /robots.txt           /var/www/code.example.com/robots.txt
    Alias /                     /var/www/code.example.com/cgit.cgi/
    <Directory /var/www/code.example.com>
      Options Indexes FollowSymLinks
      Options +ExecCGI
      Order allow,deny
      Allow from all
      AddHandler cgi-script .cgi
      DirectoryIndex cgit.cgi
    </Directory>
</VirtualHost>

Importing a Subversion repo into Git

To import a svn repo create a new git repo and run

git svn clone http://svn.foo.com/svn/project/ project -s

Then once your done, repack it.

git gc project/

SVN Branches

If you wish to also map your branches, you may run something like the following

for a in `cat .git/packed-refs |grep remotes |grep -v pack-refs |grep -v tags |grep -v trunk |grep -v '@'|awk '{print $2}'`
 do
   b=`echo "$a" |sed 's/// /g' |awk '{print $3}'`
   git branch -t $b $a
 done

SVN Tags

From a checked out SVN directory, the following will give you a list of all the users in the SVN log. You will still need to updated this list before you may use it.

for a in `cat .git/packed-refs |grep remotes |grep tags |grep -v '@' |awk '{print $2}'`
 do
   b=`echo "$a" |sed 's/// /g' |awk '{print $4}'`
   echo "creating tag $b"
   git tag -a $b -m "Converting SVN tag to GIT tag" $a
   sleep 5
 done

SVN Authors list

#!/usr/bin/env bash
authors=$(svn log -q | grep -e '^r' | awk 'BEGIN { FS = "|" } ; { print $2 }' | sort | uniq)
for author in ${authors}; do
   echo "${author} = ${author} ";
done

Once you have your list built, need to add it to your .git config. I store my authors file in the .git directory.

[svn]
     authorsfile = .git/authors

GIT/SVN Notes

Git: Creating a new user for git secure remote push

Here’s a quick how-to to create a new user for git. The assumes you already have a functioning secure git server already running.

Start out by creating the user, note were changing the shell to git’s own git-shell.

adduser -b /home/git -g git -m -s /usr/bin/git-shell kelsey -p

After the user is created, create a openssh key for the new user.

mkdir /home/git/kelsey/.ssh
ssh-keygen -n kelsey -t dsa -f kelsey/.ssh/id_dsa

And lastly secure the directory back down.

chown -R kelsey:kelsey /home/git/kelsey
chmod -R u=rwX,go-rwx /home/git/kelsey/.ssh
chmod +rw /home/git/kelsey/.ssh/id_dsa.pub

Kelsey will now be able to push git updates to her branches.

Git Commit eMail Notifications

Git’s Native eMail Notifier

From the repository you wish to send email on commits.

cd .git/hooks
mv post-receive post-receive.bkp
ln -s /usr/share/git-core/contrib/hooks/post-receive-email post-receive git config hooks.mailinglist "mailinglist@example.com"
git config hooks.emailprefix "[SUBJECT PREFIX] " # note the trailing space 

Git Commit Notifier

This ‘plugin’ will allow you to send an email every time a commit is committed to the repository. You may use this on the central repository to keep everyone following the project up to date.

To use this plugin, you first need to compile the script, then add the config information to each repository you wish to use it on.

To Install Git Commit Notifier

On Fedora 12 you first need to install Ruby and a few other dependencies. After you download the needed dependencies, you may compile the script.

yum -y install ruby ruby-devel rubygems hpricot rubygem-hpricott
gem install git-commit-notifier

To Configure Git Commit Notifier

In your repository’s .git/hooks folder or if it’s a “bare” repository, just the hooks folder. Create a file named
post-receive with the following content.

#!/bin/sh
git-commit-notifier ../git-commit-notifier.yml

Once you have saved the file, you need to make it executable.

chmod 775 post-receive

After you have made the hook executable, check up one directory to the repository’s .git directory. From here you need to create and modify your git-commit-notifier’s config file. Start out by creating a file named git-commit-notifier.yml, and copy the below config to it.

Git Commit Notifier Config File

# The recipient for the commit:
mailinglist: developers@example.com

# set to true if you want to ignore empty merge messages
ignore_merge: false

# Optional parameter for the subject-line of the mail
# emailprefix: GIT

# Decorate files with link to a webview. Possible values: none or gitweb link_files: none

# select the delivery method: smtp or sendmail
delivery_method: sendmail

# settings for the smtp server
smtp_server:
address: localhost
port: 25
domain: localhost
user_name: user@localhost
password: password
authentication: plain
enable_tls: false

# settings for sendmail
sendmail_options:
location: /usr/sbin/sendmail
arguments: -i -t

# If link_files is set to "gitweb", you need to configure the path to your gitweb
# instance and the project name.
gitweb:
path: <a href="http://developerserver/path_to_gitweb">http://developerserver/path_to_gitweb</a>
project: test.git

Git: Creating and Importing Patches

Git allows you to create patch files that may be email or transfered in other ways the imported into a repository from outside the git chain.

Creating a patch

To create a patch file of the last commit

git format-patch -1 
gzip 0001-name_of_patch_file.patch
Patching a file or directory
gzip -dc 0001-name_of_patch_file.patch.gz |git apply

Git: Allow remote pushes to a checked out repository

In the remote repository you are planing on pushing to, run the following:

git config receive.denycurrentbranch ignore

Then download the Post-update.zip and unzip it. Now copy this file to your .git/hooks/ folder and make it executable.

wget http://wiki.mattrude.com/images/d/de/Post-update.zip
unzip Post-update.zip
rm -rf Post-update.zip
chmod 775 post-update
mv post-update .git/hooks/post-update

You should now be able to remotely push to this repository without errors.

Installing Git from Source

First install all needed dependence:

yum install gcc perl-devel perl perl-DBD-Multi zlib-devel zlib openssh-server 
openssh libcurl expat expat-devel xinetd

Now Download Git via a tarball:

mkdir -p /var/src
cd /var/src
rm -rf git-git*
wget --no-check-certificate https://github.com/git/git/tarball/master
tar -xzf git-git*
cd git-git*

Or if you already have git installed, via git it’s self:

mkdir -p /var/src
git clone git://git.kernel.org/pub/scm/git/git.git
cd git

Now compile the install.

make configure
./configure --prefix=/usr
make
make install

Git: Creating an unattached branch with no history

From inside your git repository, after you have committed all you changes, run:

git symbolic-ref HEAD refs/heads/name-of-new-branch
rm .git/index
git clean -fdx

You will now have an empty directory waiting for your first commit.

So if your creating a new branch for github pages for example, you would run:

git symbolic-ref HEAD refs/heads/gh-pages
rm -f .git/index
git clean -fdx

And your ready to start creating your page.

Git Commit eMail Notifications

Git’s Native eMail Notifier

From the repository you wish to send email on commits.

cd .git/hooks
mv post-receive post-receive.bkp
ln -s /usr/share/git-core/contrib/hooks/post-receive-email post-receive
git config hooks.mailinglist "mailinglist@example.com"
git config hooks.emailprefix "[SUBJECT PREFIX] "  # note the trailing space

Git Commit Notifier

This ‘plugin’ will allow you to send an email every time a commit is committed to the repository. You may use this on the central repository to keep everyone following the project up to date.

To use this plugin, you first need to compile the script, then add the config information to each repository you wish to use it on.

To Install Git Commit Notifier

On Fedora 12 you first need to install Ruby and a few other dependences. After you download the needed dependences, you may compile the script.

yum install ruby rubygems hpricot rubygem-hpricott
gem install git-commit-notifier

To Configure Git Commit Notifier

In your repository’s .git/hooks folder or if it’s a “bare” repository, just the hooks folder. Create a file named post-receive with the following content.

#!/bin/sh
git-commit-notifier ../git-commit-notifier.yml

Once you have saved the file, you need to make it executable.

chmod 775 post-receive

After you have made the hook executable, check up one directory to the repository’s .git directory. From here you need to create and modify your git-commit-notifier’s config file. Start out by creating a file named git-commit-notifier.yml, and copy the below config to it.

Git Commit Notifier Config File

# The recipient for the commit:
mailinglist: developers@example.com

# set to true if you want to ignore empty merge messages
ignore_merge: false

# Optional parameter for the subject-line of the mail
# emailprefix: GIT

# Decorate files with link to a webview. Possible values: none or gitweb
link_files: none

# select the delivery method: smtp or sendmail
delivery_method: sendmail

# settings for the smtp server
smtp_server:
address: localhost
port: 25
domain: localhost
user_name: user@localhost
password: password
authentication: plain
enable_tls: false

# settings for sendmail
sendmail_options:
location: /usr/sbin/sendmail
arguments: -i -t

# If link_files is set to "gitweb", you need to configure the path to your gitweb
# instance and the project name.
gitweb:
path: http://developerserver/path_to_gitweb
project: test.git

Installing the git, cgit web interface on a Fedora

cgit is fast web interface for the git. cgit has built a cache and is compiled in c so it’s very quick.

To start out, download the current version of cgit via git

git clone git://hjemli.net/pub/git/cgit

Next you need to setup the git submodule

cd cgit
git submodule init
git submodule update

Once your done, run get-git

make get-git

Then compile the software

make

And install it

make install

After you have installed cgit, you will need to setup Apache to run cgit. This is pretty easy, just edit your /etc/httpd/conf/httpd.conf file and add the following for the site you wish to run cgit on.

<Directory "/var/www/code.mattrude.com/">
      AllowOverride None
      Options ExecCGI
      Order allow,deny
      Allow from all
</Directory>

Once your done setting up cgit in Apache, you may configure cgit by creating a cgitrc file at /etc/cgitrc. Below is the example config file.

EXAMPLE CGITRC FILE
-------------------

# Enable caching of up to 1000 output entriess
cache-size=1000

# Specify some default clone prefixes
clone-prefix=git://foobar.com ssh://foobar.com/pub/git http://foobar.com/git

# Specify the css url
css=/css/cgit.css

# Show extra links for each repository on the index page
enable-index-links=1

# Show number of affected files per commit on the log pages
enable-log-filecount=1

# Show number of added/removed lines per commit on the log pages
enable-log-linecount=1

# Add a cgit favicon
favicon=/favicon.ico

# Use a custom logo
logo=/img/mylogo.png

# Enable statistics per week, month and quarter
max-stats=quarter

# Set the title and heading of the repository index page
root-title=foobar.com git repositories

# Set a subheading for the repository index page
root-desc=tracking the foobar development

# Include some more info about foobar.com on the index page
root-readme=/var/www/htdocs/about.html

# Allow download of tar.gz, tar.bz2 and zip-files
snapshots=tar.gz tar.bz2 zip

##
## List of common mimetypes
##

mimetype.git=image/git
mimetype.html=text/html
mimetype.jpg=image/jpeg
mimetype.jpeg=image/jpeg
mimetype.pdf=application/pdf
mimetype.png=image/png
mimetype.svg=image/svg+xml

##
## List of repositories.
## PS: Any repositories listed when section is unset will not be
##     displayed under a section heading
## PPS: This list could be kept in a different file (e.g. '/etc/cgitrepos')
##      and included like this:
##        include=/etc/cgitrepos
##

repo.url=foo
repo.path=/pub/git/foo.git
repo.desc=the master foo repository
repo.owner=fooman@foobar.com
repo.readme=info/web/about.html

repo.url=bar
repo.path=/pub/git/bar.git
repo.desc=the bars for your foo
repo.owner=barman@foobar.com
repo.readme=info/web/about.html

# The next repositories will be displayed under the 'extras' heading
section=extras

repo.url=baz
repo.path=/pub/git/baz.git
repo.desc=a set of extensions for bar users

repo.url=wiz
repo.path=/pub/git/wiz.git
repo.desc=the wizard of foo

# Add some mirrored repositories
section=mirrors

repo.url=git
repo.path=/pub/git/git.git
repo.desc=the dscm

repo.url=linux
repo.path=/pub/git/linux.git
repo.desc=the kernel

# Disable adhoc downloads of this repo
repo.snapshots=0

# Disable line-counts for this repo
repo.enable-log-linecount=0# Restrict the max statistics period for this repo
repo.max-stats=month

Installing the GIT Daemon for Read Only Access to Repoistory

The idea here is to allow anyone to download your GIT repository using the native git protocol.  This is similar to the post I wrote about Creating a secure Git repository server, but the below method allows full read only public access to the repositories.

Start out by modifying the new file git-daemon.

vim /etc/xinetd.d/git-daemon

Add the below text to the file.

# description: The git daemon offers anonymous access to git repositories
service git
{
    disable        = no
    type           = UNLISTED
    port           = 9418
    socket_type    = stream
    wait           = no
    user           = git
    server         = /usr/libexec/git-core/git-daemon
    server_args    = --inetd --export-all --base-path=/var/git
    log_on_failure += USERID
}

You may need to change the location of the git-daemon (the above example is from Fedora) and you will need to update the location of your git repository directory.

The above configuration will share ALL the git repositories in the /var/git directory.

If you do not wish for the all repositories to be public, you may remove the --export-all flag and add a empty file named git-daemon-export-ok to the git repository you wish to still share.