3 $CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
5 //$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
6 //$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
8 function gwvp_gitControlCallMe()
10 if(isset($_REQUEST["q"])) {
11 $query = $_REQUEST["q"];
12 $qspl = explode("/", $query);
14 if($qspl[0] == "git") {
15 return "gwvp_gitBackendInterface";
25 function gwvp_repoPermissionCheck($repo, $user)
30 function gwvp_gitBackendInterface_new()
32 // and this is where i re-code the git backend interface from scratch
33 global $repo_base, $BASE_URL;
38 if(isset($_REQUEST["q"])) {
39 $query = $_REQUEST["q"];
40 $qspl = explode("/", $query);
42 for($i=2; $i < count($qspl); $i++) {
43 $newloc .= "/".$qspl[$i];
51 function gwvp_gitBackendInterface()
53 global $repo_base, $BASE_URL;
58 if(isset($_REQUEST["q"])) {
59 $query = $_REQUEST["q"];
60 $qspl = explode("/", $query);
62 for($i=2; $i < count($qspl); $i++) {
63 $newloc .= "/".$qspl[$i];
67 $actual_repo_name = preg_replace("/\.git$/", "", $repo);
69 $user = gwvp_checkBasicAuthLogin();
72 error_log("User is set to false, so its anonymouse");
74 error_log("user is $user");
77 // must remember that $user of false is anonymous when we code gwvp_repoPerm'sCheck()
78 if(!gwvp_repoPermissionCheck($actual_repo_name, $user)) {
79 error_log("perms check fails - start auth");
80 if(isset($_SERVER["PHP_AUTH_USER"])) {
81 error_log("have auth - push 403");
84 error_log("push auth");
85 gwvp_AskForBasicAuth();
90 // we need to quite a bit of parsing in here. The "repo" will always be /git/repo.git
91 // but if we get here from a browser, we need to forward back to a normal repo viewer
92 // the only way i can think of doing this is to check the useragent for the word "git"
96 * 1) figure out the repo its acessing
97 * 2) figure out the perms on the repo
98 * 3) determine if its a pull or a push
99 * - if its a pull, we just serve straight from the fs
100 * - if its a push, we go thru git-http-backend
101 * 4) if it requiers auth, we push to auth
104 $agent = "git-unknown";
107 // tested the user agent bit with jgit from eclipse and normal git... seems to work
108 if(isset($_SERVER["HTTP_USER_AGENT"])) {
109 $agent = $_SERVER["HTTP_USER_AGENT"];
110 error_log("in git backend with user agent $agent");
111 if(stristr($agent, "git")!==false) {
118 /* dont need this code right now
119 if($isgitagent) echo "GIT: i am a git backened interface for a repo $repo, agent $agent";
120 else echo "NOT GIT: i am a git backened interface for a repo $repo, agent $agent";
123 // now we need to rebuild the actual request or do we?
124 //$basegit = "$BASE_URL/git/something.git";
125 //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
126 chdir("$repo_base/$repo");
127 exec("/usr/bin/git update-server-info");
129 if($_SERVER["REQUEST_METHOD"] == "POST") {
130 gwvp_AskForBasicAuth();
131 gwvp_callGitBackend($repo);
135 if(isset($_REQUEST["service"])) {
136 if($_REQUEST["service"] == "git-receive-pack") {
137 // we are a write call - we need auth and we're going to the backend proper
138 gwvp_AskForBasicAuth();
139 gwvp_callGitBackend($repo);
145 if(file_exists("$repo_base/$repo/$newloc")) {
146 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
147 $fh = fopen("$repo_base/$repo/$newloc", "rb");
149 error_log("pushing file");
151 echo fread($fh, 8192);
154 echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
155 header('HTTP/1.0 404 No Such Thing');
161 function gwvp_callGitBackend($repo)
163 // this is where things become a nightmare
164 $fh = fopen('php://input', "r");
166 $ruri = $_SERVER["REQUEST_URI"];
167 $strrem = "git/$repo";
168 $euri = str_replace($strrem, "", $_REQUEST["q"]);
169 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
173 $rmeth = $_SERVER["REQUEST_METHOD"];
176 foreach($_REQUEST as $key => $var) {
178 //error_log("adding, $var from $key");
179 if($qs == "") $qs.="$key=$var";
180 else $qs.="&$key=$var";
188 // this is where the fun, it ends.
195 if(isset($procenv)) unset($procenv);
196 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
197 $procenv["PATH_TRANSLATED"] = "/tmp/$repo/$euri";
198 $procenv["REQUEST_METHOD"] = "$rmeth";
199 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
200 $procenv["QUERY_STRING"] = "$qs";
201 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
202 $procenv["REMOTE_USER"] = "user";
203 $procenv["REMOTE_ADDR"] = "1.2.3.4";
204 $procenv["AUTH_TYPE"] = "Basic";
206 if(isset($_SERVER["CONTENT_TYPE"])) {
207 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
209 //$procenv["CONTENT_TYPE"] = "";
211 if(isset($_SERVER["CONTENT_LENGTH"])) {
212 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
215 error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
222 $proc = proc_open("/usr/lib/git-core/git-http-backend", array(array("pipe","rb"),array("pipe","wb"),array("file","/tmp/err", "a")), $pipes, $pwd, $procenv);
225 while(!$untilblank&&!feof($pipes[1])) {
226 $lines_t = fgets($pipes[1]);
227 $lines = trim($lines_t);
228 error_log("got line: $lines");
229 if($lines_t == "\r\n") {
231 error_log("now blank");
232 } else header($lines);
233 if($lines === false) {
234 error_log("got an unexpexted exit...");
244 if(!stream_set_blocking($fh,0)) {
245 error_log("cant set input non-blocking");
248 if(!stream_set_blocking($pipes[1],0)) {
249 error_log("cant set pipe1 non-blocking");
252 // i was going to use stream_select, but i feel this works better like this
256 $from_client_data = fread($fh,8192);
257 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
259 //fwrite($fl, $from_client_data);
260 $client_len = strlen($from_client_data);
262 error_log("client end");
267 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
268 // but i do know that the fgets for the headers above should have comsued that
269 if(!feof($pipes[1])) {
270 $from_cgi_data_t = fread($pipes[1],8192);
271 $from_cgi_data = $from_cgi_data_t;
273 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
274 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
275 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
277 if(strlen($from_cgi_data_t)>0) {
278 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
279 // or something the git-http-backend thing is doing..
280 // TODO: find out why this happens
281 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
282 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
283 error_log("MOOOKS - we did trunc");
285 error_log("MOOOKS - we did not trunc");
291 if($from_cgi_data !== false) {
295 $cgi_len = strlen($from_cgi_data);
297 error_log("cgi end");
301 if(feof($pipes[1])) $continue = false;
303 if($client_len == 0 && $cgi_len == 0) {
305 error_log("sleep tick");
307 error_log("sizes: $client_len, $cgi_len");
309 error_log("from cgi: \"$from_cgi_data\"");
325 function gwvp_repoExists($name)
329 if(file_exists("$repo_base/$name.git")) return true;
333 function gwvp_createGitRepo($name, $bundle=null)
337 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
338 if($bundle == null) {
339 error_log("would create $repo_base/$name.git");
340 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
341 chdir("$repo_base/$name.git");
342 exec("/usr/bin/git update-server-info");
344 error_log("create via mirror on $repo_base/$name.git");
345 exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
346 chdir("$repo_base/$name.git");
347 exec("/usr/bin/git update-server-info");