#!/bin/bash

t=`pwd`
r="$t/test-repo"
w="$t/test-wc"

rm -rf "$r" "$w"

svnadmin create "$r"
svn co "file://$r" "$w"
cat >"$w/foo" <<EOF
1234567890 - 1234567890 - 1234567890 - hi0
1234567890 - 1234567890 - 1234567890 - hi1
1234567890 - 1234567890 - 1234567890 - hi2
1234567890 - 1234567890 - 1234567890 - hi3
1234567890 - 1234567890 - 1234567890 - hi4
1234567890 - 1234567890 - 1234567890 - hi5
1234567890 - 1234567890 - 1234567890 - hi6
1234567890 - 1234567890 - 1234567890 - hi7
1234567890 - 1234567890 - 1234567890 - hi8
1234567890 - 1234567890 - 1234567890 - hia
1234567890 - 1234567890 - 1234567890 - hib
1234567890 - 1234567890 - 1234567890 - hic
1234567890 - 1234567890 - 1234567890 - hid
1234567890 - 1234567890 - 1234567890 - hie
1234567890 - 1234567890 - 1234567890 - hif
1234567890 - 1234567890 - 1234567890 - lo0
1234567890 - 1234567890 - 1234567890 - lo1
1234567890 - 1234567890 - 1234567890 - lo2
1234567890 - 1234567890 - 1234567890 - lo3
1234567890 - 1234567890 - 1234567890 - lo4
1234567890 - 1234567890 - 1234567890 - lo5
1234567890 - 1234567890 - 1234567890 - lo6
1234567890 - 1234567890 - 1234567890 - lo7
1234567890 - 1234567890 - 1234567890 - lo8
1234567890 - 1234567890 - 1234567890 - lo9
1234567890 - 1234567890 - 1234567890 - loa
1234567890 - 1234567890 - 1234567890 - lob
1234567890 - 1234567890 - 1234567890 - loc
1234567890 - 1234567890 - 1234567890 - lod
1234567890 - 1234567890 - 1234567890 - loe
1234567890 - 1234567890 - 1234567890 - lof
EOF
svn add "$w/foo"
svn ci -m "Original revision" "$w"

cat >$r/hooks/pre-commit <<EOF
#!/usr/bin/env python

import string
import sys
import re
import os.path
from svn import core, fs, delta, repos

class ChangeReceiver(delta.Editor):
  def __init__(self, fs_ptr, txn_root, pool):
    self.fs_ptr = fs_ptr
    self.txn_root = txn_root
    self.pool = pool
    self.do_debug = True
    txn_name = fs.txn_root_name(self.txn_root)
    txn_ptr = fs.open_txn(self.fs_ptr, txn_name)
    base_rev = fs.txn_base_revision(txn_ptr)
    self.base_root = fs.revision_root(fs_ptr, base_rev, pool)

  def dbg(self, msg):
    if self.do_debug:
      sys.stderr.write("@DEBUG@ " + msg + "\n")

  def add_file(self, path, parent_baton, copyfrom_path, copyfrom_rev, file_pool):
    if copyfrom_path is not None:
      self.dbg(("add_file %s (from %s:%d)" % (path, copyfrom_path, copyfrom_rev)))
    else:
      self.dbg(("add_file %s" % (path)))
    return [ path, 0, 0, 0 ]

  def open_file(self, path, parent_baton, base_revision, file_pool):
    self.dbg(("open_file %s" % (path)))
    return [ path, 0, 0, 0 ]

  def apply_textdelta(self, file_baton, base_checksum):
    self.dbg(("apply_textdelta to %s" % (file_baton[0])))
    def txdelta_handler(window):
      if window is None:
	self.dbg("txdelta - stop")
      else:
	self.dbg(("txdelta received on %s src[%d,%d] tgt[X,%d] src_ops %d num_ops %d" %
	    (file_baton[0], window.sview_offset, window.sview_len,
	      window.tview_len, window.src_ops, window.num_ops)))
	self.dbg(("  new_data '%s'" % (window.new_data)))
	for op in window.ops:
	  self.dbg(("  op: %d at %d for %d" % (op.action_code, op.offset, op.length)))
	# WRONG: self.dbg(("  op: %d at %d for %d" % (window.ops.action_code, window.ops.offset, window.ops.length)))
    return txdelta_handler

def verify(pool, repos_path, txn):
  fs_ptr = repos.fs(repos.open(repos_path, pool))
  txn_ptr = fs.open_txn(fs_ptr, txn, pool)
  txn_root = fs.txn_root(txn_ptr, pool)
  editor = ChangeReceiver(fs_ptr, txn_root, pool)
  e_ptr, e_baton = delta.make_editor(editor, pool)
  repos.svn_repos_replay2(txn_root, "", -1, True, e_ptr, e_baton, None, pool)
  if editor.do_debug:
    sys.exit(1)

if __name__ == '__main__':
  assert len(sys.argv) == 3
  core.run_app(verify, sys.argv[1], sys.argv[2])
EOF
chmod +x $r/hooks/pre-commit

cat >"$w/foo" <<EOF
1234567890 - 1234567890 - 1234567890 - hi0
1234567890 - 1234567890 - 1234567890 - hi1
1234567890 - 1234567890 - 1234567890 - hi2
1234567890 - 1234567890 - 1234567890 - hi3
1234567890 - 1234567890 - 1234567890 - hi4
1234567890 - 1234567890 - 1234567890 - hi5
1234567890 - 1234567890 - 1234567890 - hi6
1234567890 - 1234567890 - 1234567890 - hi7
1234567890 - 1234567890 - 1234567890 - hi8
1234567890 - 1234567890 - 1234567890 - hia
1234567890 - 1234567890 - 1234567890 - hib
1234567890 - 1234567890 - 1234567890 - hic
1234567890 - 1234567890 - 1234567890 - hid
1234567890 - 1234567890 - 1234567890 - hie
1234567890 - 1234567890 - 1234567890 - hif
1234567890 - 1234567890 - 1234567890 - med
1234567890 - 1234567890 - 1234567890 - lo0
1234567890 - 1234567890 - 1234567890 - lo1
1234567890 - 1234567890 - 1234567890 - lo2
1234567890 - 1234567890 - 1234567890 - lo3
1234567890 - 1234567890 - 1234567890 - lo4
1234567890 - 1234567890 - 1234567890 - lo5
1234567890 - 1234567890 - 1234567890 - lo6
1234567890 - 1234567890 - 1234567890 - lo7
1234567890 - 1234567890 - 1234567890 - lo8
1234567890 - 1234567890 - 1234567890 - lo9
1234567890 - 1234567890 - 1234567890 - loa
1234567890 - 1234567890 - 1234567890 - lob
1234567890 - 1234567890 - 1234567890 - loc
1234567890 - 1234567890 - 1234567890 - lod
1234567890 - 1234567890 - 1234567890 - loe
1234567890 - 1234567890 - 1234567890 - lof
extra new line
EOF
svn ci -m "Update" "$w"

