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()
32 global $repo_base, $BASE_URL;
37 if(isset($_REQUEST["q"])) {
38 $query = $_REQUEST["q"];
39 $qspl = explode("/", $query);
41 for($i=2; $i < count($qspl); $i++) {
42 $newloc .= "/".$qspl[$i];
46 $actual_repo_name = preg_replace("/\.git$/", "", $repo);
48 $user = gwvp_checkBasicAuthLogin();
51 error_log("User is set to false, so its anonymouse");
53 error_log("user is $user");
56 // must remember that $user of false is anonymous when we code gwvp_repoPerm'sCheck()
57 if(!gwvp_repoPermissionCheck($actual_repo_name, $user)) {
58 error_log("perms check fails - start auth");
59 if(isset($_SERVER["PHP_AUTH_USER"])) {
60 error_log("have auth - push 403");
63 error_log("push auth");
64 gwvp_AskForBasicAuth();
69 // we need to quite a bit of parsing in here. The "repo" will always be /git/repo.git
70 // but if we get here from a browser, we need to forward back to a normal repo viewer
71 // the only way i can think of doing this is to check the useragent for the word "git"
75 * 1) figure out the repo its acessing
76 * 2) figure out the perms on the repo
77 * 3) determine if its a pull or a push
78 * - if its a pull, we just serve straight from the fs
79 * - if its a push, we go thru git-http-backend
80 * 4) if it requiers auth, we push to auth
83 $agent = "git-unknown";
86 // tested the user agent bit with jgit from eclipse and normal git... seems to work
87 if(isset($_SERVER["HTTP_USER_AGENT"])) {
88 $agent = $_SERVER["HTTP_USER_AGENT"];
89 error_log("in git backend with user agent $agent");
90 if(stristr($agent, "git")!==false) {
97 /* dont need this code right now
98 if($isgitagent) echo "GIT: i am a git backened interface for a repo $repo, agent $agent";
99 else echo "NOT GIT: i am a git backened interface for a repo $repo, agent $agent";
102 // now we need to rebuild the actual request or do we?
103 //$basegit = "$BASE_URL/git/something.git";
104 //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
105 chdir("$repo_base/$repo");
106 exec("/usr/bin/git update-server-info");
108 if($_SERVER["REQUEST_METHOD"] == "POST") {
109 gwvp_AskForBasicAuth();
110 gwvp_callGitBackend($repo);
114 if(isset($_REQUEST["service"])) {
115 if($_REQUEST["service"] == "git-receive-pack") {
116 // we are a write call - we need auth and we're going to the backend proper
117 gwvp_AskForBasicAuth();
118 gwvp_callGitBackend($repo);
124 if(file_exists("$repo_base/$repo/$newloc")) {
125 error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
126 $fh = fopen("$repo_base/$repo/$newloc", "rb");
128 error_log("pushing file");
130 echo fread($fh, 8192);
133 echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
134 header('HTTP/1.0 404 No Such Thing');
140 function gwvp_callGitBackend($repo)
142 // this is where things become a nightmare
143 $fh = fopen('php://input', "r");
145 $ruri = $_SERVER["REQUEST_URI"];
146 $strrem = "git/$repo";
147 $euri = str_replace($strrem, "", $_REQUEST["q"]);
148 //$euri = preg_replace("/^git\/$repo\.git/", "", $_REQUEST["q"]);
152 $rmeth = $_SERVER["REQUEST_METHOD"];
155 foreach($_REQUEST as $key => $var) {
157 //error_log("adding, $var from $key");
158 if($qs == "") $qs.="$key=$var";
159 else $qs.="&$key=$var";
167 // this is where the fun, it ends.
174 if(isset($procenv)) unset($procenv);
175 $procenv["GATEWAY_INTERFACE"] = "CGI/1.1";
176 $procenv["PATH_TRANSLATED"] = "/tmp/$repo/$euri";
177 $procenv["REQUEST_METHOD"] = "$rmeth";
178 $procenv["GIT_HTTP_EXPORT_ALL"] = "1";
179 $procenv["QUERY_STRING"] = "$qs";
180 $procenv["HTTP_USER_AGENT"] = "git/1.7.1";
181 $procenv["REMOTE_USER"] = "user";
182 $procenv["REMOTE_ADDR"] = "1.2.3.4";
183 $procenv["AUTH_TYPE"] = "Basic";
185 if(isset($_SERVER["CONTENT_TYPE"])) {
186 $procenv["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
188 //$procenv["CONTENT_TYPE"] = "";
190 if(isset($_SERVER["CONTENT_LENGTH"])) {
191 $procenv["CONTENT_LENGTH"] = $_SERVER["CONTENT_LENGTH"];
194 error_log("path trans'd is /tmp/$repo/$euri from $ruri with ".$_REQUEST["q"]." $strrem");
201 $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);
204 while(!$untilblank&&!feof($pipes[1])) {
205 $lines_t = fgets($pipes[1]);
206 $lines = trim($lines_t);
207 error_log("got line: $lines");
208 if($lines_t == "\r\n") {
210 error_log("now blank");
211 } else header($lines);
212 if($lines === false) {
213 error_log("got an unexpexted exit...");
223 if(!stream_set_blocking($fh,0)) {
224 error_log("cant set input non-blocking");
227 if(!stream_set_blocking($pipes[1],0)) {
228 error_log("cant set pipe1 non-blocking");
231 // i was going to use stream_select, but i feel this works better like this
235 $from_client_data = fread($fh,8192);
236 if($from_client_data !== false) fwrite($pipes[0], $from_client_data);
238 //fwrite($fl, $from_client_data);
239 $client_len = strlen($from_client_data);
241 error_log("client end");
246 // sometimes, we get a \r\n from the cgi, i do not know why she swallowed the fly,
247 // but i do know that the fgets for the headers above should have comsued that
248 if(!feof($pipes[1])) {
249 $from_cgi_data_t = fread($pipes[1],8192);
250 $from_cgi_data = $from_cgi_data_t;
252 // i dont know if this will solve it... it coudl cause some serious issues elsewhere
253 // TODO: this is a hack, i need to know why the fgets above doesn consume the \r\n even tho it reads it
254 // i.e. why the pointer doesnt increment over it, cause the freads above then get them again.
256 if(strlen($from_cgi_data_t)>0) {
257 // i dont get why this happens, and its very frustrating.. im not sure if its a bug in php
258 // or something the git-http-backend thing is doing..
259 // TODO: find out why this happens
260 $from_cgi_data = preg_replace("/^\r\n/", "", $from_cgi_data_t);
261 if(strlen($from_cgi_data)!=strlen($from_cgi_data_t)) {
262 error_log("MOOOKS - we did trunc");
264 error_log("MOOOKS - we did not trunc");
270 if($from_cgi_data !== false) {
274 $cgi_len = strlen($from_cgi_data);
276 error_log("cgi end");
280 if(feof($pipes[1])) $continue = false;
282 if($client_len == 0 && $cgi_len == 0) {
284 error_log("sleep tick");
286 error_log("sizes: $client_len, $cgi_len");
288 error_log("from cgi: \"$from_cgi_data\"");
304 function gwvp_repoExists($name)
308 if(file_exists("$repo_base/$name.git")) return true;
312 function gwvp_createGitRepo($name, $bundle=null)
316 // phew, this works, but i tell you this - bundles arent quite as nice as they should be
317 if($bundle == null) {
318 error_log("would create $repo_base/$name.git");
319 exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
320 chdir("$repo_base/$name.git");
321 exec("/usr/bin/git update-server-info");
323 error_log("create via mirror on $repo_base/$name.git");
324 exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
325 chdir("$repo_base/$name.git");
326 exec("/usr/bin/git update-server-info");