Solved

SVN: Hook Script to Avoid Filenames with spaces and special chars

Posted on 2010-08-19
4
730 Views
Last Modified: 2013-11-15
Hello,

Is there any SVN Hook script for Linux to avoid users committing filenames with spaces and special chars?

Thanks!

 
0
Comment
Question by:andremta
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:ssapkota
Comment Utility
0
 

Author Comment

by:andremta
Comment Utility
Hello,

Thanks for the reply.

That script checks also inside its source code, no the file name itself. Maybe is too complex.

Anything more simple?

0
 
LVL 3

Accepted Solution

by:
ssapkota earned 500 total points
Comment Utility
Here a quick rough edit to the code in the above link. This should work for you. I haven't tested it though (I don't have SVN repo to test it now).

#!/bin/env python
" Example Subversion pre-commit hook. "

def command_output(cmd):
  " Capture a command's standard output. "
  import subprocess
  return subprocess.Popen(
      cmd.split(), stdout=subprocess.PIPE).communicate()[0]

def files_changed(look_cmd):
  """ List the files added or updated by this transaction.

"svnlook changed" gives output like:
  U   trunk/file1.cpp
  A   trunk/file2.cpp
  """
  def filename(line):
      return line[4:]
  def added_or_updated(line):
      return line and line[0] in ("A", "U")
  return [
      filename(line)
      for line in command_output(look_cmd % "changed").split("\n")
      if added_or_updated(line)]

def check_filesname(look_cmd):
  " Check for invalid filenames. \
  It allows Alphabets, Numbers, dashes(-), Underscores(_) and dots(.) "
  allowedRegex=re.compile("^[a-zA-Z0-9\-_\.]*$")
  invalid_files = [
      ff for ff in files_changed(look_cmd)
      if not allowedRegex.match(ff)]
  if len(invalid_files) > 0:
      sys.stderr.write("The following files have invalid filename:\n%s\n"
                       % "\n".join(invalid_files))
  return len(invalid_files)

def main():
  usage = """usage: %prog REPOS TXN

Run pre-commit options on a repository transaction."""
  from optparse import OptionParser
  parser = OptionParser(usage=usage)
  parser.add_option("-r", "--revision",
                    help="Test mode. TXN actually refers to a revision.",
                    action="store_true", default=False)
  errors = 0
  try:
      (opts, (repos, txn_or_rvn)) = parser.parse_args()
      look_opt = ("--transaction", "--revision")[opts.revision]
      look_cmd = "svnlook %s %s %s %s" % (
          "%s", repos, look_opt, txn_or_rvn)
      errors += check_filesname(look_cmd)
  except:
      parser.print_help()
      errors += 1
  return errors

if __name__ == "__main__":
  import sys
  sys.exit(main())
0
 

Author Closing Comment

by:andremta
Comment Utility
Excellent!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now