0

I've got an SVN repo running on a Windows machine. I need a script that will upload any committed file to a remote web server. I've read up on pre-commit hooks, but I don't know how to actually write the script, or even in what language it should be written in. Seems like the script needs to be a .bat or .exe to run in windows.

Does anyone have a script that will run on a Windows machine? I'm not looking to upload the entire site, but the individual file(s) committed. I want a pre-commit hook because that will give the user feedback should the ftp upload fail.

If you've done this before, please explain the whole process to a newb.

2 Answers2

3

A pre-commit hook is used when you want to be able to fail the commit for some reason. Do you really want to fail a commit be cause your FTP site was down, or the disk was full?

You should only use pre-commit scripts to fail stuff that is under the developer's control such as files that are missing required properties, etc. This gives the developer a chance to correct the problem, then resubmit the commit.

A post-commit script still ties up the user's terminal as they wait for it to complete, but the script can fail and notify the user that something failed, but the commit happens anyway. If you want to do this with hooks, you should be doing it with a post-commit hook and not a pre-commit hook.


For what you want to do, hooks aren't a good way to go. What if the user commits a lot of files? Do they have to sit there for ten or so minutes while your hook script executes?

A better idea is to use a continuous build system like Hudson. Hudson will watch your Subversion repository, and can do the FTP task you want every time a user does a commit. With Hudson, you're not tying up the developer as they wait for the hook to complete, and you have a complete log of what happened with every single commit.

In fact, every site should be using a continuous build server. Even if you don't compile code, you still want to run tests against every commit.

Hudson even has FTP and SFTP plugins which may make your task easier to do. And, you can call pre-build and post-build scripts in Hudson, so you have the flexibility to do almost anything your heart desires.

Hudson runs on Windows servers and Unix servers. It is a Java process.


If you're on Windows, and you don't have PowerShell, you should download some scripting language you and use that to write your hooks. The standard batch programming language in the Windows command.exe processor is not very flexible or powerful. Since hooks run on the server, you only have to worry whether or not your hooks run on a single machine. Some programs use client-side hooks and if you write a hook, it must be capable on running on every single system a client might use.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • I've spent the whole day researching this topic and the consensus seems to be: use a hook! – Liquidgraph Dec 02 '10 at 00:09
  • I'm basically trying to set up what you said Hudson does, where every time a developer commits, it gets uploaded to the staging server. If he's committing multiple files, doesn't that still count as one commit? Why would that slow you down? – Liquidgraph Dec 02 '10 at 00:11
  • I agree with @David W. that this seems an odd way for implementing what you're trying to do... – jeroenh Dec 02 '10 at 08:41
  • 1
    @Liquidgraph "If he's committing multiple files, doesn't that still count as one commit? Why would that slow you down?". If you're FTPing all the files that got committed, the user has to wait for the FTP to complete before the hook script can return control of the user to their terminal. If the user committed 30Gigabytes worth of files, they'll have to wait 10 to 30 minutes while they wait for either a pre-commit or post-commit hook to finish. With Hudson, the user can commit and continue their work. Meanwhile, Hudson will handle the 30Gb transfer. – David W. Dec 02 '10 at 21:09
1

I think you need a post-commit hook.

A svn hook can indeed be any executable or script file. Look here for some samples

You can use the built-in command line ftp client on windows. One way is to put your FTP commands in a text file, which you can generate from a batch file. Example:

echo [username]>ftp.txt
echo [password]>>ftp.txt
echo put [filename]>>ftp.txt
echo quit>>ftp.txt

ftp -s:ftp.txt [server]

Of course, you can also write a program to do this (e.g. in .Net, using the FtpWebRequest class), it doesn't have to be a batch script.

Community
  • 1
  • 1
jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • No, I need pre-commit. Otherwise FTP upload fails but commit is made, and I'm screwed. – Liquidgraph Dec 01 '10 at 22:01
  • None of those samples relate to FTP uploading. Also, can you elaborate on how to use the build-in command line ftp client in windows via hook script? – Liquidgraph Dec 01 '10 at 22:03
  • 2
    The last thing you want is a pre-commit hook. Pre-commit hooks should only be used for user issues. That way, the user can correct the problem and recommit. A problem with the FTP server is your problem. – David W. Dec 02 '10 at 21:13