sync index.cgi with the changes from pk910
[gitweb.git] / git_graph.php
index fa8d1bd31eac2092b9e04bdcd8d88a082210fbdf..689b851659f2ac124443ee7517a2b032e7a0a279 100644 (file)
@@ -1,18 +1,21 @@
 <?php
 
 /* CONFIG */
-$repo_path = "/srv/gitosis/repositories/";
-$project_list = "/srv/gitosis/gitosis/projects.list";
+$repo_path = "/home/git/";
+$project_list = null;
 
 $size = 20;
+$header_height = false; //false = dynamic
 $tile_size = 20; /* do not edit */
 
 $max_branches = 30;
+$use_local_branches = true;
+$use_remote_branches = true;
 
 $colors = array(
     NULL,
     array(255, 0, 0),
-    array(0, 255, 0),
+    array(array(0, 255, 0), array(0, 192, 0)),
     array(0, 0, 255),
     array(128, 128, 128),
     array(128, 128, 0),
@@ -76,37 +79,41 @@ if($_SESSION['git_graph']) {
         $data = $old_data;
 }
 
+function parse_commit($commit_data) {
+    $commit = array();
+    $rev_lines = explode("\n", str_replace("\r", "", $commit_data));
+    $commit['id'] = $rev_lines[0];
+    foreach($rev_lines as $rev_line) {
+        if(substr($rev_line, 0, 4) == "    ") {
+            if($commit['text'])
+                $commit['text'] .= "\n";
+            $commit['text'] .= substr($rev_line, 4);
+        } else {
+            $opt = explode(" ", $rev_line);
+            if($opt[0] == "tree")
+                $commit['tree'] = $opt[1];
+            else if($opt[0] == "parent")
+                $commit['parent'][] = $opt[1];
+            else if($opt[0] == "author") {
+                $commit['author'] = $opt[1];
+                $commit['author_mail'] = $opt[2];
+                $commit['author_time'] = $opt[3];
+            } else if($opt[0] == "committer") {
+                $commit['committer'] = $opt[1];
+                $commit['committer_mail'] = $opt[2];
+                $commit['committer_time'] = $opt[3];
+            }
+        }
+    }
+    return $commit;
+}
+
 //load rev-list
 if(!$data['commits']) {
-    $cmd = "git --git-dir=".$repo_path.$project." rev-list --header --max-count=".($_GET['to'] + 1)." ".$_GET['h'];
+    $cmd = "git --git-dir=".$repo_path.$project." rev-list --header --max-count=".($_GET['to'] + 1)." ".($_GET['h'] =="all" ? "--all" : $_GET['h']);
     $rev_list = shell_exec($cmd);
     foreach(explode("\000", $rev_list) as $rev) {
-        $commit = array();
-        $rev_lines = explode("\n", str_replace("\r", "", $rev));
-        $commit['id'] = $rev_lines[0];
-        foreach($rev_lines as $rev_line) {
-            if(substr($rev_line, 0, 4) == "    ") {
-                if($commit['text'])
-                    $commit['text'] .= "\n";
-                $commit['text'] .= substr($rev_line, 4);
-            } else {
-                $opt = explode(" ", $rev_line);
-                if($opt[0] == "tree")
-                    $commit['tree'] = $opt[1];
-                else if($opt[0] == "parent")
-                    $commit['parent'][] = $opt[1];
-                else if($opt[0] == "author") {
-                    $commit['author'] = $opt[1];
-                    $commit['author_mail'] = $opt[2];
-                    $commit['author_time'] = $opt[3];
-                } else if($opt[0] == "committer") {
-                    $commit['committer'] = $opt[1];
-                    $commit['committer_mail'] = $opt[2];
-                    $commit['committer_time'] = $opt[3];
-                }
-            }
-        }
-        $data['commits'][] = $commit;
+        $data['commits'][] = parse_commit($rev);
     }
 }
 
@@ -117,10 +124,120 @@ $_SESSION['git_graph'] = serialize($data);
 $data['branches'] = array();
 $data['ubranches'] = array();
 $brach_id = 1;
-$brach_uid = 1;
+$branch_uid = 1;
 $graph_commit = NULL;
 $first_commit = true;
 
+if($_GET['h'] == "all") {
+    //set master branch as first branch
+    $cmd = "git --git-dir=".$repo_path.$project." rev-list --header --max-count=1 HEAD";
+    $head_list = shell_exec($cmd);
+    $head = parse_commit($head_list);
+    $first_commit = false;
+    $data['branches'][count($data['branches'])] = array();
+    $branch = &$data['branches'][count($data['branches'])-1];
+    $branch['id'] = $brach_id++;
+    $branch['uid'] = $branch_uid++;
+    $branch['active'] = true;
+    $branch['sticky'] = true;
+    $branch['next'] = $head['id'];
+    unset($branch);
+    if($use_local_branches) {
+        $cmd = "git --git-dir=".$repo_path.$project." for-each-ref --sort=-committerdate refs/heads/";
+        $head_list = shell_exec($cmd);
+        foreach(explode("\n", str_replace("\r", "", $head_list)) as $head) {
+            $head = explode(" ", $head);
+            $name = explode("/", $head[1]);
+            $name = $name[count($name)-1];
+            if(!$head[0])
+                continue;
+            $existing = false;
+            foreach($data['branches'] as &$branch) {
+                if($branch['next'] == $head[0]) {
+                    $existing = true;
+                    $branch['name'][] = $name;
+                    break;
+                }
+            }
+            unset($branch);
+            if($existing)
+                continue;
+            $data['branches'][count($data['branches'])] = array(
+                "id" => $brach_id++,
+                "uid" => $branch_uid++,
+                "active" => true,
+                "sticky" => true,
+                "name" => array($name),
+                "next" => $head[0]
+            );
+        }
+    }
+    if($use_remote_branches) {
+        $cmd = "git --git-dir=".$repo_path.$project." for-each-ref --sort=-committerdate refs/remotes/";
+        $head_list = shell_exec($cmd);
+        foreach(explode("\n", str_replace("\r", "", $head_list)) as $head) {
+            $head = explode(" ", $head);
+            $name = explode("/", $head[1]);
+            $name = $name[count($name)-2];
+            if(!$head[0])
+                continue;
+            $existing = false;
+            foreach($data['branches'] as &$branch) {
+                if($branch['next'] == $head[0]) {
+                    $existing = true;
+                    $branch['name'][] = $name;
+                    break;
+                }
+            }
+            unset($branch);
+            if($existing)
+                continue;
+            $data['branches'][count($data['branches'])] = array(
+                "id" => $brach_id++,
+                "uid" => $branch_uid++,
+                "active" => true,
+                "sticky" => true,
+                "name" => array($name),
+                "next" => $head[0]
+            );
+        }
+    }
+}
+
+if($_GET['c'] == "header") {
+    $count = count($data['branches']);
+    if($count > $max_branches)
+        $count = $max_branches;
+    if(!$header_height) {
+        $maxlen = 0;
+        foreach($data['branches'] as $branch) {
+            if($branch['sticky'] && count($branch['name'])) {
+                if(strlen($branch['name'][0]) > $maxlen)
+                    $maxlen = strlen($branch['name'][0]);
+            }
+        }
+        $header_height = $maxlen * 2 + 15;
+    }
+    $image = imagecreatetruecolor($count * $size + 60, $header_height);
+    $transparentIndex = imagecolorallocate($image, 217, 216, 209);
+    imagefill($image, 0, 0, $transparentIndex);
+    $branches = 0;
+    foreach($data['branches'] as $branch) {
+        if($branch['sticky'] && count($branch['name'])) {
+            $branches++;
+            $color = get_color($branch['id'], true);
+            $color = imagecolorallocatealpha($image, $color[0], $color[1], $color[2], 0);
+            imagettftext($image, 8, 28, ($branch['id']-1) * $size + 10, $header_height-2, $color, $_SERVER['DOCUMENT_ROOT']."/arial.ttf", $branch['name'][0]);
+        }
+    }
+    if(!$branches) die();
+    imagecolortransparent($image, $transparentIndex);
+    header('Content-Type: image/png');
+    imagepng($image);
+    imagedestroy($image);
+    die();
+}
+
 for($i = 0; $i <= intval($_GET['to']); $i++) {
 //for($i = 0; $i < count($data['commits']); $i++) {
     $commit = &$data['commits'][$i];
@@ -160,8 +277,13 @@ for($i = 0; $i <= intval($_GET['to']); $i++) {
             }
         }
         unset($cbranch);
-        if($first)
-            continue;
+        if($first) {
+            $data['branches'][count($data['branches'])] = array();
+            $branch = &$data['branches'][count($data['branches'])-1];
+            $branch['id'] = $brach_id++;
+            $branch['uid'] = $branch_uid++;
+            $branch['active'] = true;
+        }
     }
     
     if(count($commit['parent']) > 1) {
@@ -219,14 +341,15 @@ for($i = 0; $i <= intval($_GET['to']); $i++) {
     }
 }
 
-if($graph_commit == NULL)
-    die("ERROR 0x07");
-
 //DEV Breakpoint
 //print_r($data);
 //die();
 
 //generate image
+
+if($graph_commit == NULL)
+    die("ERROR 0x07");
+
 $count = count($data['branches']);
 if($count > $max_branches)
     $count = $max_branches;
@@ -259,14 +382,17 @@ function overlay_image($name, $left, $color = false) {
     imagecopyresampled($image, $image2, $left, 0, 0, 0, $size, $size, $tile_size, $tile_size);
 }
 
-function get_color($id) {
+function get_color($id, $text = false) {
     global $colors, $graph_commit, $data;
     if($graph_commit['branches'][($id-1)]['pre_merge']) {
         $branch = $data['ubranches'][$graph_commit['branches'][($id-1)]['uid']];
         if($branch['pre_merge'] && $branch['pre_merge_start'])
             $id = $branch['pre_merge_id'];
     }
-    return $colors[($id - 1) % count($colors)];
+    $color_array = $colors[($id - 1) % count($colors)];
+    if($text && is_array($color_array[0]) && $color_array[1])
+        return $color_array[1];
+    return (is_array($color_array[0]) ? $color_array[0] : $color_array);
 }
 
 foreach($graph_commit['branches'] as $branch) {