3

I'm trying to write a post commit hook script for svn to export repository to team foundation server. (So when you commit a code, that latest version of the code gets copied to team foundation repo )

Only language I use is C++ and C and I have never written a script. Can anybody give me step by step instruction for this? which language to use, what to read etc... along with some example code possibly ??

Is it possible to write a hook script with c++? or should I learn how to use python or ruby etc..

kiki
  • 205
  • 1
  • 6
  • 15
  • 1
    Firstly, [answers are already available here](http://stackoverflow.com/questions/139315/can-i-run-a-script-when-i-commit-to-subversion). Secondly: no. It doesn't have to be a "script". Anything with an executable bit works. You could write your hooks in C or C++, too. – Linus Kleen Mar 19 '12 at 16:16

3 Answers3

8

You can write your hook using C or C++ if you like. Most people use Perl or Python.

The main thing is that svnlook should be used in your hook script and not svn. svnlook is faster and safer than svn. In fact, in pre-commit scripts, you have to use svnlook since you don't have a repository revision.

Here are some things to keep in mind about your post-commit hook:

  • You cannot change a revision, and don't try to commit changes. Your post-commit hook will end up calling itself. If you want your developers to follow certain parameters, you need to have a pre-commit hook that fails if the developers don't do it right.
  • When Subversion calls your post-commit hook, the PATH environment variable has been deleted. If you need to access another command or a file, you'll have to provide the full path yourself.
  • Subversion will pass you certain parameters from the command line. Not an actual command line, but it means you can find these parameters in ARGV. Two parameters will be passed to you:
    1. The repository path on the server (for the svnlook command).
    2. The revision of the repository.

Everything else you must deduce though svnlook (which you can run via the system command. However, since you're actually writing things in C and C++, you might be able to use the built in Subversion API).

Another possibility: Use a continuous build system like Jenkins to do the dirty work for you. You might find it easier to work through Jenkins instead of worrying how a post-commit hook might accomplish this task. One of the advantages of Jenkins is that when things go wrong, you're not sending back failed post-commit messages to the user (who is probably not at fault). Instead, you cat get a complete log of what happened, and alert the person who can actually fix the issue.

Here's a sample of one I wrote which is in Perl. Don't know if it'll do you any good.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • We are considering using Jenkins or Team foundation server and if we use team foundation server we need to sync svn and TFS that's why i'm looking into post-commit hook. Is there a way to use syncing functionality for team foundation server (svn - team foundation server)?? and Thank you for the insightful reply – kiki Mar 19 '12 at 17:32
  • @kiki - Actually, you can use Jenkins to do more than just builds, and sometimes not even using it for builds. I meant solely to use Jenkins in this case just to do your syncing between Subversion and TeamFoundation. – David W. Mar 19 '12 at 18:02
2

You can find instructions on how to define a pre-commit hook here: http://wordaligned.org/articles/a-subversion-pre-commit-hook

The creation of a post-commit is similar to that instruction.

Once you have copied the post-commit template to a new file, you could simply add a line to call a program you have written in c++ and simply pass the arguments to your program.

It depends on what you are up to, but most of the time it is more time saving to use a scripting language as shell-script or python to do your job.

devsnd
  • 7,382
  • 3
  • 42
  • 50
  • Is there an existing post-commit hook that exports the result to somewhere else or copy the codes to somewhere else (back up)? – kiki Mar 19 '12 at 16:52
  • Yes, there is a template in the svn sources: http://svn.apache.org/repos/asf/continuum/trunk/continuum-release/src/test/scm/hooks/post-commit.tmpl Now that you have the repository and the revision, you can simply call `svn` to get all the information you need of the commit. – devsnd Mar 19 '12 at 16:54
  • I guess I need help with actually writing a script. Is there a good resource I can read or look at? with small examples to try it and get the feel of it? – kiki Mar 19 '12 at 17:11
0

Here is a stackoverflow answer here with a sample for post-commit hook on windows, using native windows shell commands - https://stackoverflow.com/a/3630534/7984

Community
  • 1
  • 1
Philibert Perusse
  • 4,026
  • 5
  • 24
  • 26