14

Can I set hooks for "pull"/"push"ing from/to a git-svn managed repository?

The situation is that I have a project host on Google Code, and use git to manage the local working copy. I want to set some hooks so that when checking in/out data from/to the SVN repository with git svn fetch and git svn dcommit, I can do some modification to the commit. Since I do not host the SVN repository, I can't set hooks on the server side.

Is there any hook I could use? Or is there a way to "mark" an ordinary branch, so that git pull and git push on that branch will check in/out from/to a SVN repository instead, therefore the normal git hooks could be used?

Thanks in advance.

iamamac
  • 9,632
  • 4
  • 35
  • 30

3 Answers3

9

As illustrated by this thread, you can have hooks on the Subversion side which can actually reject your git svn dcommit based on some criteria.

But if you need git hooks on the Git side, I would recommend setting up an intermediate bare Git repository (bare for easy push/pull).
On that bare repo, you can have any hook you need, as shown in "how do I deploy multiple branches to different directories via git push?" (not about svn but just here to detail a similar setup of an extra repo)

  • if the intermediate repo validate your push, it could trigger the git svn dcommit.
  • it can also, on a git fetch request, trigger a git svn fetch and validate it, before allowing your own git fetch to move forward.

(2 years later)

David Souther proposes a solution in his blog (April 2012)

The solution I have is to have a repository with split heads. Most of the time, the intermediate repository will have an empty working directory. When a push happens, it will check out master, verify the build, and push to svn, before returning to the empty branch.

We’re going to set up an intermediate git repo with two branches.

  • master will still point to the SVN repo,
  • and the new branch stage will let us keep the working directory clean and in sync with downstream changes.

Check out his gist.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Brilliant! An intermediate repository will solve my problem. But I'd like to keep the question open for a few days to see if there is a more elegant solution :) – iamamac Jan 09 '10 at 09:56
  • @lamamac: absolutely. That is what a bounty is for ;) – VonC Jan 09 '10 at 10:14
  • "it can also, on a git fetch request, trigger a git svn fetch and validate it" - Which hook would you use for this - I can't see anything obvious in githooks that would be triggered on the server when the client attempts to pull. – Greg May 04 '11 at 01:48
  • @Greg: true, but since you are setting up both the local repo and then bare repo (with direct access to it) you can call directly the bare repo validation program for `svn fetch` (you can encapsulate both operations through an alias). But on the push side, you won't have to call anything directly, a post-receive hook on the bare repo can take care of the `svn dcommit`. In any case, the intermmediate bare repo allows you to decouple the pure versionning aspect (your local Git repo) from the SVN synchronization (the Google SVN repo). – VonC May 04 '11 at 06:37
  • I must be missing something. I'm trying to `git svn dcommit` from a bare repository, but I'm getting `fatal: This operation must be run in a work tree`. What am I doing wrong? – Daniel Hershcovich Oct 03 '11 at 17:05
  • 1
    @Daniel: any operation requiring a working tree (ie a non-bare repo) will have to be performed in the second intermediate repo, right beside the bare repo. The bare repo is here for you to push to, but that same bare repo would in turn, through a hook, go to (`cd`) the second intermediate (non-bare) repo and triggers a `git pull`, and then would (still from the second non-bare repo) do the `git svn dcommmit`. – VonC Oct 03 '11 at 18:34
  • I've posted a full treatment of how to have your hooks handle svn in a working directory at http://davidsouther.com/2012/04/git-svn-dcommit-hooks/ – David Souther Apr 13 '12 at 20:51
  • @DavidSouther Excellent. I have included a link to your work and blog post in the answer, for more visibility. – VonC Apr 13 '12 at 21:16
  • @VonC Thank you! I was a bit disappointed when this still seemed to be an unsolved problem 2 years later, since SVN is not going away and people really would like to use both. – David Souther Apr 14 '12 at 16:49
3

https://github.com/hkjels/.dotfiles/blob/master/zsh/git-svn.zsh

If you use zsh, I believe this is a good companion for git-svn. Just put your hooks in .git/hooks and prefix the command with svn-.

Eg ".git/hooks/post-svn-rebase"

The script also enables the use of aliases

hkjels
  • 51
  • 2
  • Someone made this bash version based of this zsh script over here: https://github.com/rkitover/git-svn-hooks – tutuDajuju Apr 30 '15 at 14:16
0

There's a git-remote-cvs remote helper in the works; presumably someone will do the same for svn. Normal hooks for remote operations should be invoked before and after calling these helpers.

Tobu
  • 24,771
  • 4
  • 91
  • 98