made my star image a bit nicer
[gwvp.git] / gwvplib / gwvpgitcontrol.php
index 55f9bd5..68935a2 100644 (file)
 <?php
 
-function gwvp_createGitRepo($name)
+$CALL_ME_FUNCTIONS["gitcontrol"] = "gwvp_gitControlCallMe";
+
+//$MENU_ITEMS["20repos"]["text"] = "Repo Admin";
+//$MENU_ITEMS["20repos"]["link"] = "$BASE_URL/admin/repos";
+
+function gwvp_gitControlCallMe()
+{
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               if(isset($qspl[0])) {
+                       if($qspl[0] == "git") {
+                               return "gwvp_gitBackendInterface";
+                       }
+               } 
+               else return false;
+       }
+       
+       return false;
+       
+}
+
+function gwvp_repoPermissionCheck($repo, $user)
+{
+       return true;
+}
+
+function gwvp_gitBackendInterface()
+{
+       global $repo_base, $BASE_URL;
+       
+       $repo = "";
+       $newloc = "/";
+       if(isset($_REQUEST["q"])) {
+               $query = $_REQUEST["q"];
+               $qspl = explode("/", $query);
+               $repo = $qspl[1];
+               for($i=2; $i < count($qspl); $i++) {
+                       $newloc .= "/".$qspl[$i];
+               }
+       }
+       
+       $actual_repo_name = preg_replace("/\.git$/", "", $repo); 
+       
+       $user = gwvp_checkBasicAuthLogin();
+
+       if(!$user) {
+               error_log("User is set to false, so its anonymouse");
+       } else {
+               error_log("user is $user");
+       }
+       
+       // must remember that $user of false is anonymous when we code gwvp_repoPerm'sCheck()
+       if(!gwvp_repoPermissionCheck($actual_repo_name, $user)) {
+               error_log("perms check fails - start auth");
+               if(isset($_SERVER["PHP_AUTH_USER"])) {
+                       error_log("have auth - push 403");
+                       gwvp_fourZeroThree();
+               } else {
+                       error_log("push auth");
+                       gwvp_AskForBasicAuth();
+                       return;
+               }
+       }
+       
+       // we need to quite a bit of parsing in here. The "repo" will always be /git/repo.git
+       // but if we get here from a browser, we need to forward back to a normal repo viewer
+       // the only way i can think of doing this is to check the useragent for the word "git"
+       
+       /*
+        * here we need to
+        * 1) figure out the repo its acessing
+        * 2) figure out the perms on the repo
+        * 3) determine if its a pull or a push
+        * - if its a pull, we just serve straight from the fs
+        * - if its a push, we go thru git-http-backend
+        * 4) if it requiers auth, we push to auth
+        * 
+        */
+       $agent = "git-unknown";
+       $isgitagent = false;
+       
+       // tested the user agent bit with jgit from eclipse and normal git... seems to work
+       if(isset($_SERVER["HTTP_USER_AGENT"])) {
+               $agent = $_SERVER["HTTP_USER_AGENT"];
+               error_log("in git backend with user agent $agent");
+               if(stristr($agent, "git")!==false) {
+                       $isgitagent = true;
+               }
+       }
+       
+       
+               
+       /* dont need this code right now
+       if($isgitagent) echo "GIT: i am a git backened interface for a repo $repo, agent $agent";
+       else echo "NOT GIT: i am a git backened interface for a repo $repo, agent $agent";
+       */
+       
+       // now we need to rebuild the actual request or do we?
+       //$basegit = "$BASE_URL/git/something.git";
+       //$newloc = preg_replace("/^$basegit/", "", $_SERVER["REQUEST_URI"]);
+       
+       if(file_exists("$repo_base/$repo/$newloc")) {
+               error_log("would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc");
+               $fh = fopen("$repo_base/$repo/$newloc", "rb");
+               
+               error_log("pushing file");
+               while(!feof($fh)) {
+                       echo fread($fh, 8192);
+               }
+       } else {
+               echo "would ask $repo,$actual_repo_name for $repo/$newloc from $repo_base/$repo/$newloc, NE";
+               header('HTTP/1.0 404 No Such Thing');
+               return;
+       }
+}
+
+
+
+function gwvp_repoExists($name)
+{
+       global $repo_base;
+       
+       if(file_exists("$repo_base/$name.git")) return true;
+       else return false;
+}
+
+function gwvp_createGitRepo($name, $bundle=null)
 {
        global $repo_base;
        
-       error_log("would create $repo_base/$name.git");
+       // phew, this works, but i tell you this - bundles arent quite as nice as they should be
+       if($bundle == null) {
+               error_log("would create $repo_base/$name.git");
+               exec("/usr/bin/git init $repo_base/$name.git --bare > /tmp/gitlog 2>&1");
+               chdir("$repo_base/$name.git");
+               exec("/usr/bin/git update-server-info");
+       } else {
+               error_log("create via mirror on $repo_base/$name.git");
+               exec("/usr/bin/git clone --mirror $bundle $repo_base/$name.git > /tmp/gitlog 2>&1");
+               chdir("$repo_base/$name.git");
+               exec("/usr/bin/git update-server-info");
+       }
        
        return true;
 }