notification of repo remote cloning complete via db message
[gwvp-mini.git] / bin / gwvpminicmdtool.php
1 <?php
2
3 $WEB_ROOT_FS = realpath(dirname(__FILE__));
4 $BASE_URL = "/";\r
5
6 global $WEB_ROOT_FS, $BASE_URL, $IS_WEB_REQUEST, $data_directory, $db_type, $db_name, $db_username, $db_password, $IS_WEB_REQUEST, $cmd_line_tool;
7 $IS_WEB_REQUEST = false;\r
8
9 if(file_exists("$WEB_ROOT_FS/../www/config.php")) require_once("$WEB_ROOT_FS/../www/config.php");
10 else if(file_exists("/etc/gwvpmini/config.php")) require_once("/etc/gwvpmini/config.php");
11 else $noconfig = true;
12
13 if(file_exists("$WEB_ROOT_FS/../gwvpmini/gwvpmini.php")) require_once("$WEB_ROOT_FS/../gwvpmini/gwvpmini.php");
14 else if(file_exists("/usr/share/gwvpmini/lib/gwvpmini/gwvpmini.php")) require_once("/usr/share/gwvpmini/lib/gwvpmini/gwvpmini.php");
15
16
17 echo "ARGS: ".print_r($argv,true);
18 echo "CWD: ".getcwd()."\n";
19
20 if(isset($argv["3"])) {
21         switch($argv["3"]) {
22                 case "update":
23                         gwvpcmdtool_UpdateHook();
24                         break;
25                 case "pre-receive":
26                         gwvpcmdtool_PreReceive();
27                         break;
28                 case "backgroundclone":
29                         gwvpcmdtool_BackGroundClone();
30                         break;
31                 default:
32                         gwvpcmdtool_Usage();
33         }
34 } else gwvpcmdtool_Usage();
35 return;
36
37
38 /*
39  * remote: ARGS: Array
40 remote: (
41 remote:     [0] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
42 remote:     [1] => asfd
43 remote:     [2] => admin
44 remote:     [3] => pre-receive
45 remote:     [4] => fc781c4ef5bfeae8ec01bb527db1b6ce6f65d03c
46 remote:     [5] => 7d45d43f04276fc9addb77ba8bf753329eab018d
47 remote:     [6] => refs/heads/master
48 remote: )
49 remote: ARGS: Array
50 remote: (
51 remote:     [0] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
52 remote:     [1] => asfd
53 remote:     [2] => admin
54 remote:     [3] => pre-receive
55 remote:     [4] => /nfs/export/src/local/eclipse-workspace/gwvp-mini/bin/gwvpminicmdtool.php
56 remote:     [5] => asfd
57 remote:     [6] => admin
58 remote:     [7] => update
59 remote:     [8] => refs/heads/master
60 remote:     [9] => fc781c4ef5bfeae8ec01bb527db1b6ce6f65d03c
61 remote:     [10] => 7d45d43f04276fc9addb77ba8bf753329eab018d
62
63  */
64
65
66 function gwvpcmdtool_Usage()
67 {
68         global $argv;
69         
70         echo "Usage: ".$argv[0]." this tool should not be called directly by user\n";
71 }
72
73
74 function gwvpcmdtool_BackGroundClone()
75 {
76         // here we parse arguments and have stuff with things and its 6am why am i doing this right now?
77         global $data_directory, $argv;
78         
79         $repo_base = gwvpmini_getConfigVal("repodir");
80         
81         sleep(60);
82         
83         $from = $argv[1];
84         $to = $argv[2];
85         echo "Would actually clone $from to $to in $data_directory\n";
86         //sleep(20);
87         
88         $cmd = "git clone --bare $from $repo_base/$to.git";
89         exec($cmd);
90         $rn = gwvpmini_getRepo(null, $to, null);
91         $rid = $rn["id"];
92         $uid = $rn["ownerid"];
93         gwvpmini_EnableRepo($rid);
94         
95         //gwvpmini_SendMessageByDb($type, $data, $forid)
96         gwvpmini_SendMessageByDb("info", "Repo $to has finished cloning and now ready", $uid);
97         echo "update $to ($rid) and enabled it\n";
98 }
99
100 // update will log things like branch and tag creations
101 function gwvpcmdtool_UpdateHook()
102 {
103         global $argv;
104         //echo "got ".$argv[2].", ".$argv[3].", ".$argv[4]."\n";
105         if(preg_match("/^000000+$/", $argv[5])) {
106                 // createion of tag or branch
107                 $vals = explode("/", $argv[4]);
108                 $type = "unknowncreate";
109                 if($vals[1] == "heads") $type = "branchcreate";
110                 if($vals[1] == "tags") $type = "tagcreate";
111                 
112                 //gwvpmini_AddRefActivityForRepo();
113                 gwvpmini_AddRefActivityForRepo($argv[1], $argv[2], $vals[2], $type);
114                 echo "REFSUP: ".$vals[2].", $type\n";\r
115                 
116         }
117         //gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog)
118         //gwvpmini_AddRefActivityForRepo($reponame, $byusername, $branchname, $acttype="branch")
119 }
120
121 // pre-receive logs all commit info
122 function gwvpcmdtool_PreReceive()
123 {
124         global $argv;
125
126         //echo "got from prereceive ".$argv[2].", ".$argv[3].", ".$argv[4]."\n";\r
127         
128         $lns = 0;
129         $ref = $argv[6];
130
131         $regspl = explode("/", $ref);
132         $branch = $regspl[2];
133         
134         $fp = popen("git rev-list --reverse ".$argv[5]." --not --all ", "r");
135         if($fp) while(!feof($fp)) {
136                 $line = trim(fgets($fp));
137                 if($line != "") {
138                         $cdd[$lns] = gwvpcmdtool_getCommitIdDetails($line);
139                         echo "FORCID $line we get \n".print_r($cdd[$lns], true);
140                         gwvpmini_AddCommitActivityForRepo($argv[1], $argv[2], $line, $cdd[$lns]["log"], $branch);
141                         $lns++;
142                 }
143         }
144         
145         
146         //echo "Called git rev-list --reverse ".$argv[5]." --not --all\n\n";
147         //gwvpmini_AddCommitActivityForRepo($reponame, $byusername, $commitid, $desc)
148 }
149
150 function gwvpcmdtool_getCommitIdDetails($commitid)
151 {
152         $rs = popen("git log --pretty=format:\"%at%n%ce%n%an%n%s\" $commitid -1 2> /dev/null", "r");
153         $ret = array();\r
154         if($rs) {\r
155                 $ret["date"] = trim(fgets($rs));\r
156                 $ret["email"] = trim(fgets($rs));\r
157                 $ret["fullname"] = trim(fgets($rs));
158                 $ret["log"] = "";\r
159                 while(!feof($rs)) {\r
160                         $ret["log"] .= fread($rs, 8192);\r
161                 }\r
162         } else {
163                 $ret = false;
164         }
165         
166         return $ret;
167 }
168 ?>