Archive for posts tagged with ‘git’

Current Git Branch for your prompt

I guess most of you that use Git on a more regular base, like the easiness of creating, merging and deleting branches thats comes along with Git, so you will probably tend to create branch over branch. I often change into a Git repository and ask myself: which was the last branch I was working on? Of course you can simply call ‘git status’, but wouldn’t it be more convenient to promptly see in which branch you reside as soon as you enter the repository?

If you are answer is “yes” then you should take a look on bash completion – to be more exact: to the Git related part of it.

The Git bash completion script offers a function called __git_ps1. As the ps1 already indicates, the function is intended to be called in the PS1 definition of your .bashrc. Just open your .bashrc in your favourite editor and search for PS1= which might look like the following:

PS1='[\u@\h \W]\$'

whereby your shell prompt results in:

[username@hostname path]$

To add the current Git branch to your prompt simply add the __git_ps1 function to it:

PS1='[\u@\h \W $(__git_ps1 " (%s)")]\$'

The argument to __git_ps1 will be displayed only if you are currently in a git repository:

  [username@hostname path (branchname)]$
1 Comment

Subversion Post-Commit Bash Madness

As I’ve started to develop a few bigger browser-based projects recently, I thought about setting up a version control system for keeping track of changes to source files, images and so on.

Since CVS ain’t fun when it comes down to web-based projects and GIT feels a bit too overdosed for my purposes, I made the decision to use SubVersion, which affords me to move files around without loosing their history and is still syntactically quite similiar to CVS.

Because of my job, I’m also used to a bug tracking system, namely Mantis, which can be feeded by a CVS post-commit hook with some extra data to each single bug-ID, and of course I wanted the same for my bug tracker. Like in CVS, there’s also a (per-repository) post-commit hook in SubVersion and so I followed the pervasive tutorials on the net, for getting this nifty feature running.

Sadly none of those tutorials worked for me in the way that I expected or that has been promised. Actually the hook was kinda self-explaining; stating the following commit should push a comment with the commit details to a certain id.

1
  $ svn commit -m"working on issue [#mantisid]" file

The corresponding post-commit script looked like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
  #!/bin/bash
  REPOS="$1"
  REV="$2"
 
  SVNAUTH=$(svnlook author -r $REV $REPOS)
  SVNDATE=$(svnlook date -r $REV $REPOS)
  SVNLOG=$(svnlook log -r $REV $REPOS)
  SVNCHANGE=$(svnlook changed -r $REV $REPOS)
  n=$'\n'
 
  LOGMSG="Changeset [${REV}] for repo [${REPOS}] by ${SVNAUTH}${n}${SVNDATE}${n}${n}${SVNLOG}${n}${n}Changed Files:${n}${SVNCHANGE}"
  # echo $LOGMSG >> /tmp/svnchange.log
  /usr/bin/php5 -q /path/to/mantis/scripts/checkin.php <<< "$LOGMSG"

For some odd reason svnlook hasn’t been executed and the shell variables has been empty (and no, it wasn’t the fact that you’ve got a minimal environment in the post-commit script, neither setting up the PATH nor absolute paths changed the behaviour). As this is not strange enough, the following worked:

1
2
3
4
5
6
  #!/bin/bash
  REPOS="$1"
  REV="$2"
 
  SVNVERSION=$(svnlook --version);
  echo $SVNVERSION >> /tmp/svnversion.test

After some confusing and frustrating days, I found the following solution (sorry I know that is more than just mean, actually it’s a bastard):

1
2
3
4
5
6
7
8
9
10
11
12
13
  #!/bin/bash
  REPOS="$1"
  REV="$2"
 
  SVNAUTH=`exec svnlook author -r $REV $REPOS`
  SVNDATE=`exec svnlook date -r $REV $REPOS`
  SVNLOG=`exec svnlook log -r $REV $REPOS`
  SVNCHANGE=`exec svnlook changed -r $REV $REPOS`
  n=$'\n'
 
  LOGMSG="Changeset [${REV}] for repo [${REPOS}] by ${SVNAUTH}${n}${SVNDATE}${n}${n}${SVNLOG}${n}${n}Changed Files:${n}${SVNCHANGE}"
  # echo $LOGMSG >> /tmp/svnchange.log
  /usr/bin/php5 -q /path/to/mantis/scripts/checkin.php <<< "$LOGMSG"

As mentioned this worked for me, but out of pure curiousity: why the fuck I needed to use a back-ticked-exec for svnlook, instead of a $()?

3 Comments