add side-by-side commit-diff view
add side-by-side commit-diff view

--- a/include/controller/Controller_Commitdiff.class.php
+++ b/include/controller/Controller_Commitdiff.class.php
@@ -9,6 +9,22 @@
  * @package GitPHP
  * @subpackage Controller
  */
+
+/**
+ * Constants for blobdiff modes
+ */
+define('GITPHP_BLOBDIFF_UNIFIED', 1);
+define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);
+
+/**
+ * Constant of the blobdiff mode cookie in the user's browser
+ */
+define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');
+
+/**
+ * Blobdiff mode cookie lifetime
+ */
+define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365);           // 1 year
 
 /**
  * Commitdiff controller class
@@ -61,7 +77,11 @@
 	 */
 	protected function GetCacheKey()
 	{
-		return (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '');
+		$key = (isset($this->params['hash']) ? $this->params['hash'] : '')
+		. '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '')
+		. '|' . (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true) ? '1' : '');
+
+		return $key;
 	}
 
 	/**
@@ -94,6 +114,41 @@
 			$this->params['hash'] = $_GET['h'];
 		if (isset($_GET['hp']))
 			$this->params['hashparent'] = $_GET['hp'];
+
+		if (!isset($this->params['plain']) || $this->params['plain'] != true) {
+
+			$mode = GITPHP_BLOBDIFF_UNIFIED;        // default
+
+
+			/*
+			 * Check cookie
+			 */
+			if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {
+				$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];
+			} else {
+				/*
+				 * Create cookie to prevent browser delay
+				 */
+				setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
+			}
+
+			if (isset($_GET['o'])) {
+				/*
+				 * User is choosing a new mode
+				 */
+				if ($_GET['o'] == 'sidebyside') {
+					$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;
+					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
+				} else if ($_GET['o'] == 'unified') {
+					$mode = GITPHP_BLOBDIFF_UNIFIED;
+					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
+				}
+			}
+
+			if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {
+				$this->params['sidebyside'] = true;
+			}
+		}
 	}
 
 	/**
@@ -129,6 +184,10 @@
 			$this->tpl->assign("hashparent", $this->params['hashparent']);
 		}
 
+		if (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true)) {
+			$this->tpl->assign('sidebyside', true);
+		}
+
 		$treediff = new GitPHP_TreeDiff($this->project, $this->params['hash'], (isset($this->params['hashparent']) ? $this->params['hashparent'] : ''));
 		$this->tpl->assign('treediff', $treediff);
 	}

--- a/templates/commitdiff.tpl
+++ b/templates/commitdiff.tpl
@@ -14,7 +14,12 @@
    {/if}
    {include file='nav.tpl' current='commitdiff' logcommit=$commit treecommit=$commit}
    <br />
-   <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&amp;a=commitdiff_plain&amp;h={$commit->GetHash()}{if $hashparent}&amp;hp={$hashparent}{/if}">{t}plain{/t}</a>
+   {if $sidebyside}
+   | <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&amp;a=commitdiff&amp;h={$commit->GetHash()}&amp;{if $hashparent}&amp;hp={$hashparent}{/if};o=unified">{t}unified{/t}</a>
+   {else}
+   | <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&amp;a=commitdiff&amp;h={$commit->GetHash()}&amp;{if $hashparent}&amp;hp={$hashparent}{/if};o=sidebyside">{t}side by side{/t}</a>
+   {/if}
+   | <a href="{$SCRIPT_NAME}?p={$project->GetProject()|urlencode}&amp;a=commitdiff_plain&amp;h={$commit->GetHash()}{if $hashparent}&amp;hp={$hashparent}{/if}">{t}plain{/t}</a>
  </div>
 
  {include file='title.tpl' titlecommit=$commit}
@@ -50,7 +55,11 @@
        {/if}
      {/if}
      </div>
-     {include file='filediff.tpl' diff=$filediff->GetDiff('', true, true)}
+     {if $sidebyside}
+        {include file='filediffsidebyside.tpl' diffsplit=$filediff->GetDiffSplit()}
+     {else}
+        {include file='filediff.tpl' diff=$filediff->GetDiff('', true, true)}
+     {/if}
    {/foreach}
  </div>
 

comments