Display message when diffing binary files with xdiff
Display message when diffing binary files with xdiff

--- a/include/git/Blob.class.php
+++ b/include/git/Blob.class.php
@@ -219,6 +219,26 @@
 	public function SetSize($size)
 	{
 		$this->size = $size;
+	}
+
+	/**
+	 * IsBinary
+	 *
+	 * Tests if this blob is a binary file
+	 *
+	 * @access public
+	 * @return boolean true if binary file
+	 */
+	public function IsBinary()
+	{
+		if (!$this->dataRead)
+			$this->ReadData();
+
+		$data = $this->data;
+		if (strlen($this->data) > 8000)
+			$data = substr($data, 0, 8000);
+
+		return strpos($data, chr(0)) !== false;
 	}
 
 	/**

--- a/include/git/FileDiff.class.php
+++ b/include/git/FileDiff.class.php
@@ -776,10 +776,13 @@
 
 		$fromData = '';
 		$toData = '';
+		$isBinary = false;
 		$fromName = '/dev/null';
 		$toName = '/dev/null';
 		if (($this->status == 'M') || ($this->status == 'D')) {
-			$fromData = $this->GetFromBlob()->GetData(false);
+			$fromBlob = $this->GetFromBlob();
+			$isBinary = $isBinary || $fromBlob->IsBinary();
+			$fromData = $fromBlob->GetData(false);
 			$fromName = 'a/';
 			if (!empty($file)) {
 				$fromName .= $file;
@@ -790,7 +793,9 @@
 			}
 		}
 		if (($this->status == 'M') || ($this->status == 'A')) {
-			$toData = $this->GetToBlob()->GetData(false);
+			$toBlob = $this->GetToBlob();
+			$isBinary = $isBinary || $toBlob->IsBinary();
+			$toData = $toBlob->GetData(false);
 			$toName = 'b/';
 			if (!empty($file)) {
 				$toName .= $file;
@@ -801,10 +806,14 @@
 			}
 		}
 		$output = '';
-		if ($header) {
-			$output = '--- ' . $fromName . "\n" . '+++ ' . $toName . "\n";
-		}
-		$output .= xdiff_string_diff($fromData, $toData, $context);
+		if ($isBinary) {
+			$output = sprintf(__('Binary files %1$s and %2$s differ'), $fromName, $toName) . "\n";
+		} else {
+			if ($header) {
+				$output = '--- ' . $fromName . "\n" . '+++ ' . $toName . "\n";
+			}
+			$output .= xdiff_string_diff($fromData, $toData, $context);
+		}
 		return $output;
 	}
 

comments