cvs: SVNROOT / pre-commit

[email protected] ("Gwynne Raskind") Mon, 22 Jun 2009 12:22:26 -0000
Newsgroups svn.migration
Message-ID <cvsgwynne1245673346@cvsserver>
gwynne		Mon Jun 22 12:22:26 2009 UTC

  Modified files:              
    /SVNROOT	pre-commit 
  Log:
  - Changed debug handling a bit
  - [UNTESTED UNTESTED UNTESTED] Added check for svn:keywords being set on textual files
  
  
http://cvs.php.net/viewvc.cgi/SVNROOT/pre-commit?r1=1.6&r2=1.7&diff_format=u
Index: SVNROOT/pre-commit
diff -u SVNROOT/pre-commit:1.6 SVNROOT/pre-commit:1.7
--- SVNROOT/pre-commit:1.6	Sat Dec 20 18:53:52 2008
+++ SVNROOT/pre-commit	Mon Jun 22 12:22:26 2009
@@ -15,7 +15,8 @@
 
 // -----------------------------------------------------------------------------------------------------------------------------
 // Constants
-$version = substr('$Revision: 1.6 $', strlen('$Revision: '), -2);
+$version = substr('$Revision: 1.7 $', strlen('$Revision: '), -2);
+$is_DEBUG = (isset($_ENV['DEBUG']) && $_ENV['DEBUG'] === 'DEBUG');
 
 // -----------------------------------------------------------------------------------------------------------------------------
 // Version check
@@ -33,9 +34,10 @@
 
 // -----------------------------------------------------------------------------------------------------------------------------
 // Utility
-function run_svnlook($command)
+function run_svnlook($command, $extra_args = array())
 {
-    exec('exec svnlook ' . escapeshellarg($command) . ' -t ' . escapeshellarg($GLOBALS['TXN']) . ' ' . escapeshellarg($GLOBALS['REPOS']), $output, $status);
+    exec('exec svnlook ' . escapeshellarg($command) . ' -t ' . escapeshellarg($GLOBALS['TXN']) . ' ' . 
+        escapeshellarg($GLOBALS['REPOS']) . ' ' . implode(' ', array_map('escapeshellarg', $extra_args)) . ' 2>&1', $output, $status);
     if ($status != 0) {
         fail("svnlook failed with exit code {$status}\nOutput:\n" . implode("\n", $output) . "\n");
     }
@@ -66,11 +68,14 @@
 
 // -----------------------------------------------------------------------------------------------------------------------------
 // Build list of changes
-if (isset($_ENV['DEBUG']) && $_ENV['DEBUG'] == 'DEBUG') {
-    $dirs_changed = explode("\n", trim(stream_get_contents(STDIN)));
+if ($is_DEBUG) {
+    $data = explode("\n::\n", trim(stream_get_contents(STDIN)));
+    $paths_changed = explode("\n", $data[0]);
+    $dirs_changed = explode("\n", $data[1]);
     $log_message = array('test1', 'test2');
     $author = $argv[2];
 } else {
+    $paths_changed = run_svnlook('changed');
     $dirs_changed = run_svnlook('dirs-changed');
     $log_message = run_svnlook('log');
     $author = trim(implode("\n", run_svnlook('author')));
@@ -130,7 +135,7 @@
             $exit_val = $bit ? 0 : 1;
         }
 
-        if (isset($_ENV['DEBUG']) && $_ENV['DEBUG'] == 'DEBUG') {
+        if ($GLOBALS['is_DEBUG']) {
             if ($user_in_list) {
                 print "DEBUG: User {$author} " . ($user_in_list ? "matched" : "did not match") . " user list: " . implode(",", $userlist) . "\n";
             }
@@ -152,10 +157,42 @@
     fail("Access denied: Insufficient karma for {$author} to {$last_dir}.\n");
 }
 
-if (isset($_ENV['DEBUG']) && $_ENV['DEBUG'] == 'DEBUG') {
+if ($is_DEBUG) {
     print "DEBUG: Access granted.\n";
 }
 
+// -----------------------------------------------------------------------------------------------------------------------------
+// Changing text files requires svn:keywords set
+$paths_added = array_filter($paths_changed, create_function('$v', 'return ($v[0] === "A");'));
+$didError = 0;
+foreach ($paths_added as $path_added) {
+    $path_added = substr($path_added, strpos($path_added, ' ') + 1);
+    if ($is_DEBUG) {
+        print "DEBUG: Checking properties for {$path_added}.\n";
+    }
+
+    $properties = array_map('trim', run_svnlook('proplist', FALSE, NULL, array($path_added)));
+    if (in_array($properties, 'svn:mime-type')) {
+        $mimetype = trim(implode("\n", run_svnlook('propget', FALSE, NULL, array('svn:mime-type', $path_added))));
+    } else {
+        $mimetype = 'text/unknown';
+    }
+
+    if (strncmp($mimetype, "text/", 5) === 0) {
+        if ($is_DEBUG) {
+            print "DEBUG: File is non-binary. Checking for keywords.\n";
+        }
+        if (!in_array($properties, 'svn:keywords')) {
+            fail("svn:keywords not set on textual file {$path_added}.\n");
+        }
+        if ($is_DEBUG) {
+            print "DEBUG: File has keywords. Moving on.\n";
+        }
+    } else if ($is_DEBUG) {
+        print "DEBUG: File is binary. Moving on.\n";
+    }
+}
+
 exit(0);
 
 ?>