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