[from now] 2012/09/04 15:15:59

diff --git a/Rakefile b/Rakefile
index 9d3f23e..7c36494 100644
--- a/Rakefile
+++ b/Rakefile
@@ -15,6 +15,10 @@ def commit
   run 'git commit -m "." -q'
 end

+def commit0
+  run 'git commit --allow-empty -m "Initial commit" -q'
+end
+
 def push
   run 'git push origin master -q'
 end
@@ -23,59 +27,46 @@ def branch(branch_name)
   run "git branch #{branch_name}"
 end

+def tag(tag_name)
+  run "git tag #{tag_name}"
+end
+
+def clone(repo_name)
+  run "git clone test.git #{repo_name} -q"
+end
+
+def install_hook(hook_name)
+  run "cp #{$pwd}/#{hook_name} test.git/hooks/#{hook_name}"
+  run "chmod a+x test.git/hooks/#{hook_name}"
+end
+
 desc 'git hooks test detect force update'
 task :default do
-  pwd = Dir.getwd
+  $pwd = Dir.getwd
+
   Dir.mktmpdir do |tmp|
     Dir.chdir(tmp) do
       run 'git init --bare test.git -q'

-      run 'git clone test.git test_1 -q'
-      Dir.chdir('test_1') do
-        run 'git commit --allow-empty -m "Initial commit" -q'
-        push
+      clone('test1')
+      Dir.chdir('test1') do
+        commit0
+        write; commit; tag('B'); push # HEAD is 'B'
       end

-      Dir.chdir('test_1') do
-        write; commit; push;
-        write; commit; push;
-      end
-
-      run 'git clone test.git test_2 -q'
-
-      Dir.chdir('test_1') do
-        2.times do
-          write; commit; push;
-          write; commit; push;
-        end
-
-        puts '--> IN test_1'
-        run 'git log --oneline'
-      end
+      clone('test2') # HEAD is 'B'

-      Dir.chdir('test_2') do
-        branch('foo'); branch('bar'); branch('baz');
+      Dir.chdir('test1') do
+        write; commit; write; commit; tag('O'); push # HEAD is 'O'
       end

-      Dir.chdir('test_2') do
-        write; commit; push;
-        write; commit; push;
+      install_hook('pre-receive')

-        puts '--> IN test_2'
-        run 'git log --oneline'
-      end
-
-      run "cp #{pwd}/pre-receive test.git/hooks/pre-receive"
-      run 'chmod a+x test.git/hooks/pre-receive'
-
-      puts '-' * 80
-      Dir.chdir('test_2') do
-        run 'git push origin master', verbose: true
-      end
+      Dir.chdir('test2') do
+        write; commit; write; commit; tag('X')
+        write; commit; write; commit; tag('N') # HEAD is 'N'

-      puts '-' * 80
-      Dir.chdir('test_2') do
-        run 'git push -f origin master', verbose: true
+        run 'git push -f origin master'
       end
     end
   end
diff --git a/pre-receive b/pre-receive
index 8ba5eb0..4a5e6fd 100755
--- a/pre-receive
+++ b/pre-receive
@@ -1,49 +1,46 @@
 #!/usr/bin/env ruby

 def line(m)
-  puts '-' * 5 + " #{m} " + '-' * 60
+  puts
+  puts '-----' + " #{m} " + '-----'
 end

 def show(oldrev, newrev, refname)
-  command = "git merge-base #{oldrev} #{newrev}"
+  command = "git rev-parse --short $(git merge-base #{oldrev} #{newrev})"
   puts command
   system command

-  line('oldrev')
-  command = "git rev-list #{oldrev}"
+  command = "git rev-list --abbrev-commit #{oldrev}"
   line command
   system command

-  line('newrev')
-  command = "git rev-list #{newrev}"
+  command = "git rev-list --abbrev-commit #{newrev}"
   line command
   system command

-  line('oldrev ^newrev')
-  command = "git rev-list #{oldrev} ^#{newrev}"
+  command = "git rev-list --abbrev-commit #{oldrev} ^#{newrev}"
   line command
   system command

-  line('^oldrev newrev')
-  command = "git rev-list ^#{oldrev} #{newrev}"
+  command = "git rev-list --abbrev-commit ^#{oldrev} #{newrev}"
   line command
   system command
-
-  # puts "oldrev=#{oldrev}"
-  # puts "newrev=#{newrev}"
-  # puts "refname=#{refname}"
-
-  # puts '-' * 80
-  # # puts `git rev-list #{oldrev}`
-
-  # puts '-' * 80
-  # # puts `git rev-list master | grep $(git rev-parse master@{1})`
 end

 while line = STDIN.gets
   oldrev, newrev, refname = line.strip.split(/ /)
-  show(oldrev, newrev, refname)
+  line 'arguments'
+  puts "oldrev = #{oldrev}"
+  puts "newrev = #{newrev}"
+  puts "refname = #{refname}"
+  puts
+
+  o = `git rev-parse --short #{oldrev}`.chomp
+  n = `git rev-parse --short #{newrev}`.chomp
+  puts "o = #{o}"
+  puts "n = #{n}"
+
+  puts
+  show(o, n, refname)
+  puts                          # space
 end
-
-
-
[from now] 2012/09/04 15:18:06

diff --git a/Rakefile b/Rakefile
index 7c36494..49ac086 100644
--- a/Rakefile
+++ b/Rakefile
@@ -40,8 +40,7 @@ def install_hook(hook_name)
   run "chmod a+x test.git/hooks/#{hook_name}"
 end

-desc 'git hooks test detect force update'
-task :default do
+task :do_task do
   $pwd = Dir.getwd

   Dir.mktmpdir do |tmp|
@@ -57,7 +56,9 @@ task :default do
       clone('test2') # HEAD is 'B'

       Dir.chdir('test1') do
-        write; commit; write; commit; tag('O'); push # HEAD is 'O'
+        if $is_forced_update
+          write; commit; write; commit; tag('O'); push # HEAD is 'O'
+        end
       end

       install_hook('pre-receive')
@@ -71,3 +72,15 @@ task :default do
     end
   end
 end
+
+desc 'git hooks test'
+task :normal_push do
+  $is_forced_update = false
+  do_task
+end
+
+desc 'git hooks test detect forced update'
+task :forced_push do
+  $is_forced_update = true
+  do_task
+end
[from now] 2012/09/04 15:22:04

diff --git a/Rakefile b/Rakefile
index 49ac086..6e481bd 100644
--- a/Rakefile
+++ b/Rakefile
@@ -76,11 +76,11 @@ end
 desc 'git hooks test'
 task :normal_push do
   $is_forced_update = false
-  do_task
+  Rake::Task['do_task'].invoke
 end

 desc 'git hooks test detect forced update'
 task :forced_push do
   $is_forced_update = true
-  do_task
+  Rake::Task['do_task'].invoke
 end
diff --git a/pre-receive b/pre-receive
index 4a5e6fd..2e105b4 100755
--- a/pre-receive
+++ b/pre-receive
@@ -1,4 +1,5 @@
 #!/usr/bin/env ruby
+# -*- coding: utf-8 -*-

 def line(m)
   puts
@@ -20,6 +21,7 @@ def show(oldrev, newrev, refname)

   command = "git rev-list --abbrev-commit #{oldrev} ^#{newrev}"
   line command
+  puts '# If this command outputs any commit (sha1), push is --force !'
   system command

   command = "git rev-list --abbrev-commit ^#{oldrev} #{newrev}"