GitRepo.vala
[gitlive] / GitRepo.vala
index 1dd9edc..d7333d8 100644 (file)
@@ -14,23 +14,51 @@ public class GitRepo : Object
 
 
     public string gitdir;
+    public string git_working_dir;
     public bool debug = false;
 
     /**
     * index of.. matching gitpath..
     */
-     public static int indexOfAdd( Array<GitRepo> repos, string gitpath) {
+     public static int indexOf( Array<GitRepo> repos, string gitpath) {
+        // make a fake object to compare against..
+        var test_repo = new GitRepo(gitpath);
+        
         for(var i =0; i < repos.length; i++) {
-            if (repos.index(i).gitdir == gitpath) {
+            if (repos.index(i).gitdir == test_repo.gitdir) {
                 return i;
             }
         }
         return -1;
     
     }
-
-
-
+    
+    public static Array<GitRepo> list_cache = null;
+    
+    public static Array<GitRepo> list()
+    {
+        
+        if (Repo._list !== false) {
+            return Repo._list;
+        }
+        Repo._list  = [];
+        var dir = GLib.get_home_dir() + '/gitlive';
+        var ar = File.list(dir );
+        print(JSON.stringify(ar));
+        ar.forEach(function(f) {
+            if (File.exists(dir + '/' + f +'/.git')) {
+                Repo._list.push(new imports.Scm.Git.Repo.Repo(  {
+                    repopath : dir  +'/' + f,
+                    name : f
+                }))
+            }
+        });
+        
+        return Repo._list;
+          
+}
+    
    
     /**
      * constructor:
@@ -42,7 +70,7 @@ public class GitRepo : Object
      
     public GitRepo(string path) {
         // cal parent?
-        
+        this.git_working_dir = path;
         this.gitdir = path + "/.git";
         if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
             this.gitdir = path; // naked...
@@ -57,11 +85,12 @@ public class GitRepo : Object
      *
      * @argument {Array} files the files to add.
      */
-    public string add ( Array<GitMonitorQueue> files )
+    public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
     {
         // should really find out if these are untracked files each..
         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
         // not sure if that is how git works.. but just be certian.
+        var ret = "";
         for (var i = 0; i < files.length;i++) {
             var f = files.index(i).vname;
             try {
@@ -72,6 +101,7 @@ public class GitRepo : Object
             }        
 
         }
+        return ret;
     }
     
       /**
@@ -80,7 +110,7 @@ public class GitRepo : Object
      *
      * @argument {Array} files the files to add.
      */
-    public string remove  ( Array<GitMonitorQueue> files )
+    public string remove  ( Array<GitMonitorQueue> files ) throws Error, SpawnError
     {
         // this may fail if files do not exist..
         // should really find out if these are untracked files each..
@@ -116,7 +146,7 @@ public class GitRepo : Object
      * 
      */
      
-    public string commit ( string message, Array<GitMonitorQueue> files  )
+    public string commit ( string message, Array<GitMonitorQueue> files  ) throws Error, SpawnError
     {
         
 
@@ -138,8 +168,8 @@ public class GitRepo : Object
             
         }
         */
-        string[] args = { "commit", "-m", };
-        arg +=  (message.length > 0  ? message : "Changed" );
+        string[] args = { "commit", "-m" };
+        args +=  (message.length > 0  ? message : "Changed" );
         for (var i = 0; i< files.length ; i++ ) {
             args += files.index(i).vname; // full path?
         }
@@ -155,7 +185,7 @@ public class GitRepo : Object
      * -- maybe later it will have a few options and do more stuff..
      *
      */
-    public string pull ()
+    public string pull () throws Error, SpawnError
     {
         // should probably hand error conditions better... 
         string[] cmd = { "pull" };
@@ -172,7 +202,7 @@ public class GitRepo : Object
      * -- maybe later it will have a few options and do more stuff..
      *
      */
-    public string push ()
+    public string push () throws Error, SpawnError
     {
         // should 
         return this.git({ "push" });
@@ -185,19 +215,16 @@ public class GitRepo : Object
      *
      */
     
-    public string git(string[] args_in, ?string[] env) throws Error, SpawnError
+    public string git(string[] args_in ) throws Error, SpawnError
     {
         // convert arguments.
         
 
         string[]  args = { "git" };
-        args +=  "--git-dir";
-        args +=  this.gitdir;
+        //args +=  "--git-dir";
+        //args +=  this.gitdir;
         args +=  "--no-pager";
-
-        if (env == null) {
-            env = {};
-        }
 
         //if (this.gitdir != this.repopath) {
         //    args +=   "--work-tree";
@@ -208,10 +235,12 @@ public class GitRepo : Object
         }            
 
         //this.lastCmd = args.join(" ");
-        if(this.debug) {
-         
-            print(  string.joinv (", ", args)); 
-        }
+        //if(this.debug) {
+            stdout.printf( "CWD=%s\n",  this.git_working_dir ); 
+            print(  string.joinv (" ", args)); 
+        //}
+
+        string[]   env = {};
         string  home = "HOME=" + Environment.get_home_dir() ;
         env +=  home ;
         // do not need to set gitpath..
@@ -220,13 +249,14 @@ public class GitRepo : Object
         //}
         
 
-        var cfg = new SpawnConfig(this.gitdir, args , env);
+
+        var cfg = new SpawnConfig(this.git_working_dir , args , env);
         
 
        // may throw error...
         var sp = new Spawn(cfg);
-                
-        //print("GOT: " + output)
+
+        stdout.printf( "GOT: %s\n" , sp.output);
         // parse output for some commands ?
         return sp.output;
     }