Announcement

Collapse
No announcement yet.

pre-commit lock check script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • pre-commit lock check script

    Does anybody have a pre-commit (python) script that checks whether the user performing the commit has locks on the files, and rejects the commit if any of the files are not locked by that user (excluding any files that are new, of course)?

    I know Subversion will automatically check if the file is locked by somebody else, but I want it to check that the user actually committing the file(s) has locked them.

    Many thanks.

  • #2
    If you've properly set svn:needs-lock, the files will be read-only for the user on their system until such time as the lock the files. So they shouldn't (unless they change the files's attributes) be editing those files unless they have a lock in the first place.

    If no one else has the lock already, why do you need to check that they've got a lock, if after the commit it'll be unlocked?

    Comment


    • #3
      Good questions.

      1. svn:needs-lock may not be properly set because we want to automate this, rather than relying on users to remember to set it, and are still working on a way to do that - we can't use client auto-props because we have multiple repositories in use and this is the only repository that uses a lock-modify-unlock model.

      2. So svn:needs-lock may not be set, and/or a user may change the read only file attribute. This would mean that multiple users could forget to get a lock on the same file(s). We want to stop the users from committing their changes on the unlocked file(s).

      Comment


      • #4
        Originally posted by rdekenah
        1. svn:needs-lock may not be properly set because we want to automate this, rather than relying on users to remember to set it, and are still working on a way to do that - we can't use client auto-props because we have multiple repositories in use and this is the only repository that uses a lock-modify-unlock model.
        Install a pre-commit hook in that repository which verifies that any file being added/committed of a type which requires svn:needs-lock has that attribute set. This eliminates relying upon auto-props and users remembering, because the repository is the authority.

        Originally posted by rdekenah
        2. So svn:needs-lock may not be set, and/or a user may change the read only file attribute. This would mean that multiple users could forget to get a lock on the same file(s). We want to stop the users from committing their changes on the unlocked file(s).
        My answer above eliminates the "svn:needs-lock not set" problem. And if they have manipulated the read-only bit, they still can't commit unless they get the lock first.

        At some point, you have to trust the users and realize that there's only so much you can do to protect them from themselves. If they're going so far as to manually unset the read-only bit, they're actively attempting to bypass policy & procedure; you need social solutions to social problems, not technical solutions for social problems.

        Comment


        • #5
          pre-commit.bat (batch file)

          I have been searching high and low for any type of information on a pre-commit script. Eventually all of the pieces came together in the script below. I took pieces from a bunch of different sources and came up with this script below (windows based batch file).

          This checks for the user who is committing and cross references it with the files in the transaction, and makes sure that the user has the files locked. If the user is adding a file, it doesn't check for a lock. If the user is deleting a file, it makes sure that the lock isn't kept (avoiding a zombie lock). This also checks for a log message, and makes sure that one is entered.

          I figured someone may be able to use this code. I hope someone can.

          Enjoy!

          Code:
          @echo off
          : *******************************************************
          :  Get values from action (Set USER from the transaction)
          : *******************************************************
          set REPOS=%1
          set TXN=%2
          set SVNLOOK=C:\progra~1\Subversion\bin\svnlook.exe
          
          : *******************************************************
          :  If the file doesn't have the correct amount of
          :  arguments then go to the end
          : *******************************************************
          if DEFINED TXN (
          	echo.
          ) else (
          	echo.
          	echo This file must be run by the repository!
          	echo.
          	pause
          	goto EndAll
          )
          
          : *******************************************************
          :  Get the User who is committing
          : *******************************************************
          for /f "tokens=*" %%a in ('%SVNLOOK% author --transaction %TXN% %REPOS%') do set USER=%%a
          
          : *******************************************************
          :  Lock check - Verify that the files being committed
          :  have been locked by the committing user.
          : *******************************************************
          :LockCheck
          for /f "tokens=1,2" %%b in ('%SVNLOOK% changed --transaction %TXN% %REPOS%') do (
          	if "%%b" == "U" (
          		set pass=
          		for /f "tokens=1,2*" %%f in ('%SVNLOOK% lock %REPOS% %%c') do (
          			set pass=*
          
          			: *******************************************************
          			:  Go through the path's properties to get the owner of
          			:  the lock.
          			: *******************************************************
          			if "Owner:" == "%%f" (
          				if "%USER%" == "%%g" (
          					echo.
          				) else (
          					goto Err
          				)
          			)
          		)
          		if DEFINED pass (
          			echo.
          		) else (
          			goto Err
          		)
          	)
          	: *******************************************************
          	:  Make sure the files being deleted do not have locks.
          	:  Otherwise you will have a zombie lock.
          	: *******************************************************
          	if "%%b" == "D" (
          		for /f "tokens=1,2*" %%f in ('%SVNLOOK% lock %REPOS% %%c') do (
          			goto Err
          		)
          	)
          )
          goto LogMessageCheck
          
          : *******************************************************
          :  If there's an error display error dialogue
          : *******************************************************
          :Err
          echo. 1>&2
          echo. 1>&2
          echo Please follow these guidlines: 1>&2
          echo  - Files being updated should be locked. 1>&2
          echo  - Files being deleted should NOT be locked. 1>&2
          echo  - Files being added should NOT be locked. 1>&2
          echo. 1>&2
          echo Please lock / unlock the files according to the guidlines above 1>&2
          echo and then try committing again. 1>&2
          echo. 1>&2
          echo NOTE: DO NOT LOCK FILES THAT ARE BEING ADDED OR DELETED. 1>&2
          echo. 1>&2
          echo. 1>&2
          exit 1
          
          
          : *******************************************************
          :  Make sure that the log message contains some text.
          : *******************************************************
          :LogMessageCheck
          for /f "tokens=*" %%i in ('%SVNLOOK% log --transaction %TXN% %REPOS%') do set LOGMSG="%%i"
          IF DEFINED LOGMSG goto EndAll
          
          : *******************************************************
          :  If there's an error display error dialogue
          : *******************************************************
          echo. 1>&2
          echo. 1>&2
          echo Please enter a descriptive log message and then try 1>&2
          echo committing again. 1>&2
          echo. 1>&2
          echo. 1>&2
          exit 1
          
          :EndAll

          Comment


          • #6
            Thanks, paguilar.

            We made use of this to make a file in a working copy local.

            We used your script so that a file with svn:needs-lock could be locked to edit, saved, and then unlocked. After the file is unlocked, it cannot be committed by accident. We use this to create put a file in "Local Mode", meaning that its changes are only for the working copy. We do not want these changes to end up in the repository.

            Thank You very much.

            Earl
            Last edited by EarlArmstrong; 07-15-2015, 06:25 PM.

            Comment

            Working...
            X