Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a message to users pre-commit in Tortoise SVN

We use Tortoise SVN for source control, and have already set up a commit message template.

I would also like to display some text to the user when they commit, that doesn't get included in their commit message, along the lines of "Don't forget to do X!".

Is this possible?

like image 502
Fiona - myaccessible.website Avatar asked Oct 24 '22 22:10

Fiona - myaccessible.website


1 Answers

I have set up a similar environment using the Tortoise Docs and can say: Yes, it is! Operation involves a Start-Commit Hook that fills in the lines that the user shall read and a Pre-Commit Hook that removes thee lines again:

Start-Commit Hook
This hook gets passed three parameters: PATH MESSAGEFILE CWD. MESSAGEFILE is the path to a temporary file that will be used for storing the commit message. You can fill this temporary file with your message Don't forget to do X! Or, you prefix your message with something that you will treat as comment in the commit message and gets filtered out. Since Git uses # as comment in the commit message, I did the same: every line that starts with # gets filtered out of the commit message. And therefore I'd write the message # Don't forget to do X!. Sample implementation in Perl (untested):

use strict;                         # what we always have
use warnings;                       # what we always have
use Fcntl ':flock';                 # lock files when writing
use Carp;                           # use croak instead of die
use English qw( -no_match_vars );   # words instad of cryptic variables

sub startcommit_hook{
  # open the logfile
  my $logfilename       = $ARGV[1];
  # write hint line about supported tags
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;
    print {$handle} "# Don't forget to do X!\n";
  flock $handle, LOCK_UN;
  return close $handle or croak "unable to close $OS_ERROR";
}

startcommit_hook();

Pre-Commit Hook
This hook gets passed four parameters: PATH DEPTH MESSAGEFILE CWD. The job of the pre-commit hook is to filter out the message that you filled into MESSAGEFILE in in the start-commit hook (otherwise it will go as part of the commit message to the server and this probably isn't what you want). Either just delete your message Don't forget to do X! or – if you use the comment approach as I wrote above – delete every line that starts with a # sign (or that matches the pattern ^\s*#) since it's a comment in our world.

We could extend our file for the start-commit hook to handle also the pre-commit stuff since the number of parameters is different. The decision on which hook to call is made up of the parameter count passed to the script (also untested):

use strict;                         # what we always have
use warnings;                       # what we always have
use feature 'switch';               # for given-when construct
use Fcntl ':flock';                 # lock files when writing
use Carp;                           # use croak instead of die
use English qw( -no_match_vars );   # words instad of cryptic variables

sub startcommit_hook{
  # open the logfile
  my $logfilename       = $ARGV[1];
  # write hint line about supported tags
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;
    print {$handle} "# Don't forget to do X!\n";
  flock $handle, LOCK_UN;
  return close $handle or croak "unable to close $OS_ERROR";
}

sub precommit_hook{
  my $logfilename       = $ARGV[2];
  # first, read the logfile
  open my $handle,'<:utf8',$logfilename or croak "Error reading file contents of $logfilename: $OS_ERROR\n";
  my @content = <$handle>;
  close $handle or croak "unable to close: $OS_ERROR";
  chomp @content;

  # now, write it, ignoring the comment lines
  open my $handle, '>:utf8', $logfilename
    or croak "Opening $logfilename for writing failed\n";
  flock $handle, LOCK_EX;

  foreach my $line(@content){
    if($line !~ /^\s*#/){   # line has no comment, print it.
      print {$handle} $line . "\n";
    }
  }

  flock $handle, LOCK_UN;
  close $handle or croak "unable to close $OS_ERROR";
  return;
}

given($#ARGV){
  when (3){startcommit_hook();}
  when (4)   {precommit_hook();}  # no user supplied -> auto lookup
  default  {croak "Invalid number of parameters";}
}

To activate the hooks, open the settings of TortoiseSVN, go to hook scripts and add the script once as start-commit hook and once as pre-commit hook. The command line to call would be perl /path/to/script. And also check Wait for the script to finish and Hide script while running.

Note
If you need further information passed to the hooks, you could also pass custom parameters when you assign the hooks in the settings of TortoiseSVN. If you assign custom parameters, these get passed to the hook before the default parameters (as stated in the docs) get passed.

like image 66
eckes Avatar answered Oct 30 '22 19:10

eckes