Activity log is now working
authorPaul J R <me@pjr.cc>
Tue, 2 Oct 2012 00:07:04 +0000 (10:07 +1000)
committerPaul J R <me@pjr.cc>
Tue, 2 Oct 2012 00:07:04 +0000 (10:07 +1000)
bin/gwvpminicmdtool.php
gwvpmini/gwvpmini_activity.php
gwvpmini/gwvpmini_db.php
gwvpmini/gwvpmini_gitbackend.php
gwvpmini/gwvpmini_gitrepo.php

index 1b53265..c42c8bf 100644 (file)
@@ -13,8 +13,12 @@ else $noconfig = true;
 if(file_exists("$WEB_ROOT_FS/../gwvpmini/gwvpmini.php")) require_once("$WEB_ROOT_FS/../gwvpmini/gwvpmini.php");
 else if(file_exists("/usr/share/gwvpmini/lib/gwvpmini/gwvpmini.php")) require_once("/usr/share/gwvpmini/lib/gwvpmini/gwvpmini.php");
 
-if(isset($argv["1"])) {
-       switch($argv["1"]) {
+
+echo "ARGS: ".print_r($argv,true);
+echo "CWD: ".getcwd()."\n";
+
+if(isset($argv["3"])) {
+       switch($argv["3"]) {
                case "update":
                        gwvpcmdtool_UpdateHook();
                        break;
@@ -28,13 +32,39 @@ if(isset($argv["1"])) {
 return;
 
 
+/*
+ * remote: ARGS: Array
+remote: (
+remote:     [0] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
+remote:     [1] => asfd
+remote:     [2] => admin
+remote:     [3] => pre-receive
+remote:     [4] => fc781c4ef5bfeae8ec01bb527db1b6ce6f65d03c
+remote:     [5] => 7d45d43f04276fc9addb77ba8bf753329eab018d
+remote:     [6] => refs/heads/master
+remote: )
+remote: ARGS: Array
+remote: (
+remote:     [0] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
+remote:     [1] => asfd
+remote:     [2] => admin
+remote:     [3] => pre-receive
+remote:     [4] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
+remote:     [5] => asfd
+remote:     [6] => admin
+remote:     [7] => update
+remote:     [8] => refs/heads/master
+remote:     [9] => fc781c4ef5bfeae8ec01bb527db1b6ce6f65d03c
+remote:     [10] => 7d45d43f04276fc9addb77ba8bf753329eab018d
+
+ */
+
 
 function gwvpcmdtool_Usage()
 {
        global $argv;
        
-       echo "Usage: ".$argv[0]."\n";
-       echo "\tupdatehook <user> <ref> <firstupdate> <lastupdate>\n";
+       echo "Usage: ".$argv[0]." this tool should not be called directly by user\n";
 }
 
 
@@ -43,15 +73,20 @@ function gwvpcmdtool_UpdateHook()
 {
        global $argv;
        //echo "got ".$argv[2].", ".$argv[3].", ".$argv[4]."\n";
-       if(preg_match("/^000000+$/", $argv[3])) {
+       if(preg_match("/^000000+$/", $argv[5])) {
                // createion of tag or branch
-               $vals = explode("/", $argv[2]);
+               $vals = explode("/", $argv[4]);
                $type = "unknowncreate";
-               if($vals == "heads") $type = "branchcreate";
-               if($vals == "tags") $type = "tagcreate";
-               gwvpmini_AddActivityLog("$type", "1", "1", $argv[4], "somelogs");
+               if($vals[1] == "heads") $type = "branchcreate";
+               if($vals[1] == "tags") $type = "tagcreate";
+               
+               //gwvpmini_AddRefActivityForRepo();
+               gwvpmini_AddRefActivityForRepo($argv[1], $argv[2], $vals[2], $type);
+               echo "REFSUP: ".$vals[2].", $type\n";\r
+               
        }
        //gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog)
+       //gwvpmini_AddRefActivityForRepo($reponame, $byusername, $branchname, $acttype="branch")
 }
 
 // pre-receive logs all commit info
@@ -61,6 +96,44 @@ function gwvpcmdtool_PreReceive()
 
        //echo "got from prereceive ".$argv[2].", ".$argv[3].", ".$argv[4]."\n";\r
        
-       //passthru("git rev-list --reverse ".$argv[3]." --not --all ");
+       $lns = 0;
+       $ref = $argv[6];
+
+       $regspl = explode("/", $ref);
+       $branch = $regspl[2];
+       
+       $fp = popen("git rev-list --reverse ".$argv[5]." --not --all ", "r");
+       if($fp) while(!feof($fp)) {
+               $line = trim(fgets($fp));
+               if($line != "") {
+                       $cdd[$lns] = gwvpcmdtool_getCommitIdDetails($line);
+                       echo "FORCID $line we get \n".print_r($cdd[$lns], true);
+                       gwvpmini_AddCommitActivityForRepo($argv[1], $argv[2], $line, $cdd[$lns]["log"], $branch);
+                       $lns++;
+               }
+       }
+       
+       
+       //echo "Called git rev-list --reverse ".$argv[5]." --not --all\n\n";
+       //gwvpmini_AddCommitActivityForRepo($reponame, $byusername, $commitid, $desc)
+}
+
+function gwvpcmdtool_getCommitIdDetails($commitid)
+{
+       $rs = popen("git log --pretty=format:\"%at%n%ce%n%an%n%s\" $commitid -1 2> /dev/null", "r");
+       $ret = array();\r
+       if($rs) {\r
+               $ret["date"] = trim(fgets($rs));\r
+               $ret["email"] = trim(fgets($rs));\r
+               $ret["fullname"] = trim(fgets($rs));
+               $ret["log"] = "";\r
+               while(!feof($rs)) {\r
+                       $ret["log"] .= fread($rs, 8192);\r
+               }\r
+       } else {
+               $ret = false;
+       }
+       
+       return $ret;
 }
 ?>
\ No newline at end of file
index 4f07fc5..107cb48 100644 (file)
@@ -1,4 +1,8 @@
 <?php
+global $HOME_PAGE_PROVIDERS;\r
+\r
+\r
+$HOME_PAGE_PROVIDERS["10activity"] = "gwvpmini_HomePageActivityLog";\r
 
 
 if($IS_WEB_REQUEST) {
@@ -11,23 +15,133 @@ if($IS_WEB_REQUEST) {
                $activityloglength = true;\r
        } else {\r
                $activityloglength = false;\r
-       }\r
+       }
+       
+       global $activity_log_length;\r
 }      \r
 
 
 // this will add a repo activity (a commit for eg)
 // and auto populate the field in the activity
 // log
-function gwvpmini_AddActivityForRepo($desc, $userbyid)
+function gwvpmini_AddCommitActivityForRepo($reponame, $byusername, $commitid, $desc, $branch)
 {
+       // gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog, $visibleto="a")
+       $rid = gwvpmini_GetRepoId($reponame);
+       $uid = gwvpmini_GetUserId($byusername);
+       
+       if($rid < 1 || $uid < 1) return false;
+       
+       $vis = gwvpmini_GetVisibilityForRepo($rid);
        
+       gwvpmini_AddActivityLog("commit", $uid, $rid, "$branch:$commitid", $desc, $vis);
+}
+
+function gwvpmini_AddRefActivityForRepo($reponame, $byusername, $branchname, $acttype="branch")
+{
+       $rid = gwvpmini_GetRepoId($reponame);\r
+       $uid = gwvpmini_GetUserId($byusername);\r
+       \r
+       if($rid < 1 || $uid < 1) return false;
+       
+       $vis = gwvpmini_GetVisibilityForRepo($rid);
+       \r
+       gwvpmini_AddActivityLog("refs", $uid, $rid, "$acttype:$branchname", "", $vis);
+}
+
+function gwvpmini_GetVisibilityForRepo($repoid)
+{
+       $perms = gwvpmini_GetRepoPerms($repoid);
+       $dets = gwvpmini_getRepo($repoid);
+       $oid = $dets["ownerid"];
+       
+       if($perms["b"] == "a") return "a";
+       if($perms["b"] == "r") return "r";
+       
+       $st = ":$oid:";
+       foreach($perms as $key => $val) {
+               if($key!="b") $st .= "$key:";
+       }
+       return $st;
 }
 
 // gets the activity log as it would be viewed by
 // the user id of "$forid"
-function gwvpmini_GetActivityLogFor($forid)
+function gwvpmini_HomePageActivityLog()
+{
+       global $BASE_URL;
+       
+       $id = $_SESSION["id"];
+       
+       if($id < 0) {
+               $ents = gwvpmini_GetActivityLog();
+       } else { 
+               $ents = gwvpmini_GetActivityLog(20, $id);
+       }
+       
+       echo "<h2>News</h2>";
+       echo "<table border=\"1\">";
+       foreach($ents as $vals) {
+               /*
+                *              $ret[$nent]["type"] = $vals["activity_type"];
+               $ret[$nent]["date"] = $vals["activity_date"];
+               $ret[$nent]["userid"] = $vals["activity_user"];
+               $ret[$nent]["repoid"] = $vals["activity_repo"];
+               $ret[$nent]["commitid"] = $vals["activity_commitid"];
+               $ret[$nent]["commitlog"] = $vals["activitiy_commitlog"];
+
+                */
+               $type = $vals["type"];
+               //$rest = $vals["date"].", ".$vals["userid"].", ".$vals["repoid"].", ".$vals["commitid"].", ".$vals["commitlog"];
+               
+               if($vals["type"] == "commit") {
+                       $udets = gwvpmini_getUser(null, null, $vals["userid"]);
+                       $rdets = gwvpmini_getRepo(null, null, $vals["repoid"]);
+                       $reponame = $rdets["name"];
+                       $uname = $udets["username"];
+                       $fname = $udets["fullname"];
+                       $br_spl = explode(":", $vals["commitid"]);
+                       $branch = $br_spl[0];
+                       $cid = $br_spl[1];
+                       $compressedcid = gwvpmini_CompressCommitId($cid);
+                       $log = $vals["commitlog"];
+                       $tdiff = gwvpmini_TimeDiffText($vals["date"]);
+                       $col1 = "<font size=\"+1\"><a href=\"$BASE_URL/view/$reponame\">$reponame</a></font><br>".gwvpmini_HtmlGravatar($udets["email"], 30, "<br>")."<a href=\"$BASE_URL/user/$uname\">$uname</a>";
+                       $col2 = $tdiff."<br>Commited change <b>$compressedcid</b><br><table border=\"1\"><tr><td bgcolor=\"#eeeeee\"><pre>$log</pre></td></tr></table><br>";
+               } else if($vals["type"] == "refs") {
+                       $udets = gwvpmini_getUser(null, null, $vals["userid"]);\r
+                       $rdets = gwvpmini_getRepo(null, null, $vals["repoid"]);\r
+                       $reponame = $rdets["name"];\r
+                       $uname = $udets["username"];\r
+                       $fname = $udets["fullname"];
+                       $tdiff = gwvpmini_TimeDiffText($vals["date"]);
+                       $tp_spl = explode(":", $vals["commitid"]);
+                       $col1 = "<font size=\"+1\"><a href=\"$BASE_URL/view/$reponame\">$reponame</a></font><br>".gwvpmini_HtmlGravatar($udets["email"], 30, "<br>")."<a href=\"$BASE_URL/user/$uname\">$uname</a>";
+                       if($tp_spl[0] == "tagcreate") {
+                               $colapp = "$tdiff<br>Created Tag <b>".$tp_spl[1]."</b>";
+                       } else if($tp_spl[0] == "branchcreate") {
+                               $colapp = "$tdiff<br>Created Branch <b>".$tp_spl[1]."</b>";
+                       } else {
+                               $colapp = "$tdiff<br>Performed some unknown action.";
+                       }
+                       $col2 = $colapp;
+                       
+               }
+               
+               echo "<tr><td>$col1</td><td>$col2</td></tr>";
+       }
+       echo "</table>";
+}
+
+function gwvpmini_TimeDiffText($time)
 {
+       $tdiff = time() - $time;
        
+       if($tdiff < 10) return "Now";
+       if($tdiff < 60) return "$tdiff Seconds Ago";
+       if($tdiff < 3600) return "".(int)($tdiff/60)." Minutes Ago";
+       if($tdiff < 86400) return "".(int)($tdiff/3600)." Hours Ago";
+       return "".(int)($tdiff/86400)." Days Ago";
 }
 
 ?>
\ No newline at end of file
index 0f11ec9..82abfda 100644 (file)
@@ -55,6 +55,46 @@ function gwvpmini_getUser($username=null, $email=null, $id=null)
 \r
 }
 
+function gwvpmini_GetActivityLog($nentries = 20, $forid=-1)
+{
+       /*
+        *              "activity_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+               "activity_type" TEXT,
+               "activity_date" TEXT,
+               "activity_user" TEXT,
+               "activity_repo" TEXT,
+               "activity_commitid" TEXT,
+               "activity_commitlog" TEXT,
+               "activity_visibleto" TEXT
+
+        */
+       $conn = gwvpmini_ConnectDB();\r
+       
+       if($forid == -1) {
+               // we're searching for anonymous ones
+               $sql = "select * from activity where activity_visibleto='a' order by activity_date desc limit $nentries";
+       } else {
+               $sql = "select * from activity where activity_visibleto='a' or activity_visibleto='r' or activity_visibleto like '%:$forid:%' order by activity_date desc limit $nentries";
+       }
+       
+       $vls = $conn->query($sql);
+       
+       $nent = 0;
+       $ret = array();
+       if($vls !== false) foreach($vls as $vals) {
+               $ret[$nent]["type"] = $vals["activity_type"];
+               $ret[$nent]["date"] = $vals["activity_date"];
+               $ret[$nent]["userid"] = $vals["activity_user"];
+               $ret[$nent]["repoid"] = $vals["activity_repo"];
+               $ret[$nent]["commitid"] = $vals["activity_commitid"];
+               $ret[$nent]["commitlog"] = $vals["activity_commitlog"];
+               $nent++;
+       }
+       
+       if($nent == 0) return false;
+       else return $ret;
+}
+
 
 function gwvpmini_getRepo($ownerid=null, $name=null, $id=null)\r
 {\r
@@ -431,31 +471,7 @@ function gwvpmini_AddUser($username, $password, $fullname, $email, $desc, $level
        return $retval;
 }\r
 
-function gwvpmini_GetActivityLog($nentries = 100)
-{
-       $conn = gwvpmini_ConnectDB();\r
-       
-       $sql = "select * from activity order by activity_date desc limit 100";
-
-       $res = $conn->query($sql);
-       
-       $logs = null;
-       $i = 0;
-       foreach($res as $row) {
-               $logs[$i]["type"] = $row["activity_type"];
-               $logs[$i]["date"] = $row["activity_date"];
-               $logs[$i]["userid"] = $row["activity_user"];
-               $logs[$i]["repoid"] = $row["activity_repo"];
-               $logs[$i]["commitid"] = $row["activity_commitid"];
-               $logs[$i]["commitlog"] = $row["activity_commitlog"];
-               $logs[$i]["visibleto"] = $row["activity_visibleto"];
-               $i++;
-       }
-       
-       return $logs;
-}
-
-function gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog)
+function gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog, $visibleto="a")
 {
        /*
         *      CREATE TABLE "activity" (
@@ -474,7 +490,7 @@ function gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog)
        $conn = gwvpmini_ConnectDB();
        
        // TODO: implement visibility
-       $visibleto = "a";\r
+       //$visibleto = "a";\r
        \r
        $sql = "insert into 'activity' values ( null, '$type', '".time()."', '$userid', '$repoid', '$commitid', '$commitlog', '$visibleto')";
        
index 4cb53b8..8e4dc5f 100644 (file)
@@ -26,16 +26,15 @@ function gwvpmini_gitControlCallMe()
 }
 
 
-function gwvpmini_CreateRepoHooks($repopath, $cmdpath)
+function gwvpmini_CreateRepoHooks($repopath, $cmdpath, $reponame)
 {
        $fp = fopen("$repopath/hooks/pre-receive", "w");
        
-       if(!$fp) // error_log("could not create pre-receive hook");
+       if(!$fp) error_log("could not create pre-receive hook");
        
        // TODO: think about this one
-       $script = '#!/bin/bash'."\n\n".'DCOMMIT=`cat`'."\n".'START=`echo $DCOMMIT|cut -d " " -f 1`'."\n".'END=`echo $DCOMMIT|cut -d " " -f 2`'."\n".'REF=`echo $DCOMMIT|cut -d " " -f 3`'."\n\n";
-       $script .= "php $cmdpath pre-receive ";
-       $script .= '$START $END $REF'."\n\n";
+       //$script = '#!/bin/bash'."\n\n".'DCOMMIT=`cat`'."\n".'START=`echo $DCOMMIT|cut -d " " -f 1`'."\n".'END=`echo $DCOMMIT|cut -d " " -f 2`'."\n".'REF=`echo $DCOMMIT|cut -d " " -f 3`'."\n\n";
+       $script = "#!/bin/bash\n\nDCOMMIT=".'`cat`'."\n\nphp $cmdpath $reponame \$REMOTE_USER pre-receive \$DCOMMIT\n\n";
        fwrite($fp, $script);
        
        fclose($fp);
@@ -45,12 +44,11 @@ function gwvpmini_CreateRepoHooks($repopath, $cmdpath)
 
        $fp = fopen("$repopath/hooks/update", "w");
        
-       if(!$fp) // error_log("could not create update hook");
+       if(!$fp) error_log("could not create update hook");
        
        // TODO: think about this one
-       $script = "#!/bin/bash\n\n";
-       $script .= "php $cmdpath update ";
-       $script .= '$1 $2 $3'."\n\n";
+       unset($script);
+       $script = "#!/bin/bash\n\nphp $cmdpath $reponame \$REMOTE_USER update \$1 \$2 \$3\n\n";
        fwrite($fp, $script);
        
        fclose($fp);
@@ -86,7 +84,7 @@ function gwvpmini_gitBackendInterface()
                // error_log("FLAP: donut hole");
        }*/
        
-
+       error_log("REQUESTINBACKEND: ".print_r($_REQUEST, true));
        
        $repo = "";
        $repoid = false;
@@ -113,7 +111,7 @@ function gwvpmini_gitBackendInterface()
        
        if(!file_exists("$repo_base/$repo.git/hooks/pre-receive") || !file_exists("$repo_base/$repo.git/hooks/update")) {
                // error_log("WRITING HOOKS");
-               gwvpmini_CreateRepoHooks("$repo_base/$repo.git", $cmd_line_tool);
+               gwvpmini_CreateRepoHooks("$repo_base/$repo.git", $cmd_line_tool, $repo);
        }
        
        
@@ -128,7 +126,7 @@ function gwvpmini_gitBackendInterface()
                        $write = true;
                }
        }
-       
+       if(preg_match("/.*git-receive-pack$/", $_REQUEST["q"])) $write = true;
        //$write = true;
        // THIS MAY CAUSE ISSUES LATER ON but we do it cause the git client ignores our 403 when it uses git-receive-pack after an auth
        // no, this isnt a solution cause auth'd read attempts will come up as writes...
@@ -182,8 +180,11 @@ function gwvpmini_gitBackendInterface()
        }
        
        // if its a write, we check (before and after) the branch/tag info to see if they were updated
-       //if($write) {
-       //}
+       if($write) {
+               error_log("REQUESTINBACKEND: processed as write");
+       } else {
+               error_log("REQUESTINBACKEND: processed as read");
+       }
        
        gwvpmini_callGitBackend($person, $repo);
        
index edf8fd6..46c4f95 100644 (file)
@@ -3,7 +3,7 @@ global $HOME_PAGE_PROVIDERS;
 
 
 $CALL_ME_FUNCTIONS["repoadmin"] = "gwvpmini_RepoCallMe";
-$HOME_PAGE_PROVIDERS["gitlog"] = "gwvpmini_GitLogProvider";\r
+$HOME_PAGE_PROVIDERS["00gitlog"] = "gwvpmini_GitLogProvider";\r
 \r
 \r
 // the home_page_provders bit is an array\r
@@ -169,15 +169,6 @@ function gwvpmini_GitLogProvider()
                        echo "</table>";
                }\r
        }
-       
-       $logs = gwvpmini_GetActivityLog();
-       if($logs !== null) {
-               echo "<h2>Global Log</h2><table>";
-               foreach($logs as $ents) {
-                       echo "<tr><td>".$ents["type"]."</td><td>".$ents["commitid"]."</td></tr>";
-               }
-               echo "</table>";
-       }
 }
 
 function gwvpmini_GitCreateRepoForm()
@@ -281,5 +272,12 @@ function gwvpmini_RecursiveDelete($fpath)
                return @rmdir($fpath);\r
        }
 }
+
+function gwvpmini_CompressCommitId($cid)
+{
+       $compressedcid = substr($cid, 0, 5)."...".substr($cid, strlen($cid)-5, strlen($cid));
+       
+       return $compressedcid;
+}
        
 ?>
\ No newline at end of file