Permalink
Browse files

initial commit of code examples

  • Loading branch information...
0 parents commit 4407117eeb2c2847ee8d9d219921e849d40ef88b @schacon schacon committed Jul 27, 2009
Showing with 197 additions and 0 deletions.
  1. +10 −0 acl/commit-msg
  2. +26 −0 acl/pre-commit
  3. +21 −0 acl/pre-rebase
  4. +75 −0 acl/update
  5. +65 −0 fast-import.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+message_file = ARGV[0]
+message = File.read(message_file)
+
+$regex = /\[ref: (\d+)\]/
+
+if !$regex.match(message)
+ puts "[POLICY] Your message is not formatted correctly"
+ exit 1
+end
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+
+$user = ENV['USER']
+
+# [ insert acl_access_data method from above ]
+
+# only allows certain users to modify certain subdirectories in a project
+def check_directory_perms
+ access = get_acl_access_data('.git/acl')
+
+ files_modified = `git diff-index --cached --name-only HEAD`.split("\n")
+ files_modified.each do |path|
+ next if path.size == 0
+ has_file_access = false
+ access[$user].each do |access_path|
+ if !access_path || (path.index(access_path) == 0)
+ has_file_access = true
+ end
+ if !has_file_access
+ puts "[POLICY] You do not have access to push to #{path}"
+ exit 1
+ end
+ end
+end
+
+check_directory_perms
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+base_branch = ARGV[0]
+if ARGV[1]
+ topic_branch = ARGV[1]
+else
+ topic_branch = "HEAD"
+end
+
+target_shas = `git rev-list #{base_branch}..#{topic_branch}`.split("\n")
+remote_refs = `git branch -r`.split("\n").map { |r| r.strip }
+
+target_shas.each do |sha|
+ remote_refs.each do |remote_ref|
+ shas_pushed = `git rev-list ^#{sha}^@ refs/remotes/#{remote_ref}`
+ if shas_pushed.split(“\n”).include?(sha)
+ puts "[POLICY] Commit #{sha} has already been pushed to #{remote_ref}"
+ exit 1
+ end
+ end
+end
@@ -0,0 +1,75 @@
+#!/usr/bin/env ruby
+
+$refname = ARGV[0]
+$oldrev = ARGV[1]
+$newrev = ARGV[2]
+$user = ENV['USER']
+
+puts "Enforcing Policies... \n(#{$refname}) (#{$oldrev[0,6]}) (#{$newrev[0,6]})"
+
+$regex = /\[ref: (\d+)\]/
+
+# enforced custom commit message format
+def check_message_format
+ missed_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
+ missed_revs.each do |rev|
+ message = `git cat-file commit #{rev} | sed '1,/^$/d'`
+ if !$regex.match(message)
+ puts "[POLICY] Your message is not formatted correctly"
+ exit 1
+ end
+ end
+end
+check_message_format
+
+def get_acl_access_data(acl_file)
+ # read in ACL data
+ acl_file = File.read(acl_file).split("\n").reject { |line| line == '' }
+ access = {}
+ acl_file.each do |line|
+ avail, users, path = line.split('|')
+ next unless avail == 'avail'
+ users.split(',').each do |user|
+ access[user] ||= []
+ access[user] << path
+ end
+ end
+ access
+end
+
+# only allows certain users to modify certain subdirectories in a project
+def check_directory_perms
+ access = get_acl_access_data('acl')
+
+ # see if anyone is trying to push something they can't
+ new_commits = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
+ new_commits.each do |rev|
+ files_modified = `git log -1 --name-only --pretty=format:'' #{rev}`.split("\n")
+ files_modified.each do |path|
+ next if path.size == 0
+ has_file_access = false
+ access[$user].each do |access_path|
+ if !access_path # user has access to everything
+ || (path.index(access_path) == 0) # access to this path
+ has_file_access = true
+ end
+ end
+ if !has_file_access
+ puts "[POLICY] You do not have access to push to #{path}"
+ exit 1
+ end
+ end
+ end
+end
+check_directory_perms
+
+# enforces fast-forward only pushes
+def check_fast_forward
+ missed_refs = `git rev-list #{$newrev}..#{$oldrev}`
+ missed_ref_count = missed_refs.split("\n").size
+ if missed_ref_count > 0
+ puts "[POLICY] Cannot push a non fast-forward reference"
+ exit 1
+ end
+end
+check_fast_forward
@@ -0,0 +1,65 @@
+#! /usr/bin/env ruby
+require 'digest/sha1'
+
+last_mark = nil
+$author = 'Scott Chacon <schacon@example.com>'
+
+$marks = []
+def convert_dir_to_mark(dir)
+ if !$marks.include?(dir)
+ $marks << dir
+ end
+ ($marks.index(dir) + 1).to_s
+end
+
+def convert_dir_to_date(dir)
+ if dir == 'current'
+ return Time.now().to_i
+ else
+ dir = dir.gsub('back_', '')
+ (year, month, day) = dir.split('_')
+ return Time.local(year, month, day).to_i
+ end
+end
+
+def export_data(string)
+ print "data #{string.size}\n#{string}"
+end
+
+def inline_data(file, code = 'M', mode = '644')
+ content = File.read(file)
+ puts "#{code} #{mode} inline #{file}"
+ export_data(content)
+end
+
+def print_export(dir, last_mark)
+ mark = convert_dir_to_mark(dir)
+ date = convert_dir_to_date(dir)
+
+ # print the import information
+ puts 'commit refs/heads/master'
+ puts 'mark :' + mark
+ puts "committer #{$author} #{date} -0700"
+ export_data('imported from ' + dir)
+ puts 'from :' + last_mark if last_mark
+
+ puts 'deleteall'
+ Dir.glob("**/*").each do |file|
+ next if !File.file?(file)
+ inline_data(file)
+ end
+
+ return mark
+end
+
+# loop through the directories
+Dir.chdir(ARGV[0]) do
+ Dir.glob("*").each do |dir|
+ next if File.file?(dir)
+
+ # move into the target directory
+ Dir.chdir(dir) do
+ last_mark = print_export(dir, last_mark)
+ end
+ end
+end

0 comments on commit 4407117

Please sign in to comment.