Skip to content

Commit

Permalink
Added a system to enforce git hooks.
Browse files Browse the repository at this point in the history
This should help maintain commit quality.
  • Loading branch information
Fredrik Eldh committed Jul 25, 2011
1 parent 8ab3ef8 commit e20ccf0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Binary file added githooks/junction.exe
Binary file not shown.
70 changes: 70 additions & 0 deletions rules/githooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (C) 2011 Mobile Sorcery AB
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License, version 2, as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to the Free
# Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

require 'FileUtils'
require "#{File.dirname(__FILE__)}/util.rb"

# If run at the root level of a Git working copy,
# this function creates a symbolic link from "githooks" to ".git/hooks"
# If ".git/hooks" already exists and is not the desired symbolic link,
# it is renamed to ".git/hooks.bak", unless the backup already exists,
# in which case an error is raised.
# This works even on Windows NTFS disks, thanks to the program "Junction".

# This function should be run at the beginning of the root build script,
# to ensure that every developer has version-controlled git hooks installed.
def enforceGithooks
target = '.git/hooks'
source = 'githooks'
backup = '.git/hooks.bak'
if(!File.exist?('.git'))
puts '.git not found.'
return
end
if(dirsAreEqual?(target, source))
puts 'Hook directories are equal.'
return
end
if(File.exist?(target))
if(File.exist?(backup))
error "#{target} is broken and #{backup} already exists."+
" Please remove one of the directories."
end
FileUtils.mv(target, backup, :verbose => true)
end
if(HOST == :win32)
sh "githooks/junction.exe #{target} #{source}"
else
FileUtils.ln_s(target, source, :verbose => true)
end
end

def nonDotEntries(dir)
return Dir.entries(dir).reject do |file| file[0,1] == '.' end
end

def dirsAreEqual?(a, b)
return false if(!File.exist?(a))
return false if(!File.exist?(b))
af = nonDotEntries(a)
bf = nonDotEntries(b)
return false if(af != bf) # compare filenames
af.each_with_index do |file, i|
return false if(File.mtime(a+'/'+file) != File.mtime(b+'/'+bf[i]))
end
# We could check the contents of the files, but I think that's overkill.
return true
end
3 changes: 3 additions & 0 deletions workfile.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/ruby

# File.expand_path is used here to ensure the files are really only loaded once.
require File.expand_path('rules/githooks.rb')
require File.expand_path('rules/targets.rb')
require File.expand_path('rules/host.rb')
require File.expand_path('rules/task.rb')
require File.expand_path('rules/mosync_util.rb')

enforceGithooks

PRE_DIRS = ["intlibs/idl-common", "intlibs/filelist"]

if(HOST == :win32) then
Expand Down

0 comments on commit e20ccf0

Please sign in to comment.