GitRepo.vala
[gitlive] / GitRepo.vala
1
2 /**
3  * @class Scm.Git.Repo
4  *
5  * @extends Scm.Repo
6  * 
7  *
8  *
9  */
10 static GitRepo  _GitRepo; 
11  
12 public class GitRepo : Object
13 {
14      
15     public Gee.ArrayList<GitMonitorQueue> cmds;
16
17     public string name;
18     public string gitdir;
19     public string git_working_dir;
20     public bool debug = false;
21     public bool has_local_changes = false;
22     public string host = "";
23     public string git_status;    
24     public string git_diff;        
25     public string ahead_or_behind = "";
26     
27     public Gee.HashMap<string,bool> ignore_files;
28     public GitBranch currentBranch;
29     public Gee.HashMap<string,GitBranch> branches; // accessed in GitBranch..
30         public RooTicket? activeTicket;
31     public  Gee.HashMap<string,GitRepo> cache;
32     
33     
34     
35         public static GitRepo singleton()
36     {
37         if (_GitRepo == null) {
38             _GitRepo = new GitRepo.single();
39             _GitRepo.cache = new Gee.HashMap<string,GitRepo>();
40         }
41         return _GitRepo;
42     }
43  
44     /**
45     * index of.. matching gitpath..
46     */
47     public static int indexOf( Array<GitRepo> repos, string gitpath) {
48         // make a fake object to compare against..
49         var test_repo = GitRepo.get(gitpath);
50         
51         for(var i =0; i < repos.length; i++) {
52             if (repos.index(i).gitdir == test_repo.gitdir) {
53                 return i;
54             }
55         }
56         return -1;
57     
58     }
59     
60
61     
62     
63     
64     public static   Array<GitRepo> list()
65     {
66
67         //if (GitRepo.list_cache !=  null) {
68         //    unowned  Array<GitRepo>    ret = GitRepo.list_cache;
69          //   return ret;
70         //}
71         var cache = GitRepo.singleton().cache;
72         var list_cache = new Array<GitRepo>();
73         
74         var dir = Environment.get_home_dir() + "/gitlive";
75         
76         var f = File.new_for_path(dir);
77         FileEnumerator file_enum;
78         try {
79             file_enum = f.enumerate_children(
80                 FileAttribute.STANDARD_DISPLAY_NAME + ","+ 
81                 FileAttribute.STANDARD_TYPE,
82                 FileQueryInfoFlags.NONE,
83                 null);
84         } catch (Error e) {
85             
86             return list_cache;
87             
88         }
89         
90         FileInfo next_file; 
91         
92         while (true) {
93             
94             try {
95                 next_file = file_enum.next_file(null);
96                 if (next_file == null) {
97                     break;
98                 }
99                 
100             } catch (Error e) {
101                 GLib.debug("Error: %s",e.message);
102                 break;
103             }
104          
105             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
106             
107             if (next_file.get_file_type() !=  FileType.DIRECTORY) {
108                 next_file = null;
109                 continue;
110             }
111             
112             if (next_file.get_file_type() ==  FileType.SYMBOLIC_LINK) {
113                 next_file = null;
114                 continue;
115             }
116             
117             if (next_file.get_display_name()[0] == '.') {
118                 next_file = null;
119                 continue;
120             }
121             var sp = dir+"/"+next_file.get_display_name();
122            
123             var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
124             
125             if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
126                 continue;
127             }
128             
129                 var rep =  GitRepo.get(  sp );
130                 list_cache.append_val(rep);             
131             
132         }
133     
134         return list_cache;
135          
136         }
137         
138         public static GitRepo get(string path) 
139         {
140                 var cache = GitRepo.singleton().cache;
141                 if (cache.has_key(path)) {
142                         return cache.get(path);
143                 }
144                 return new GitRepo(path);
145         }
146         
147     private GitRepo.single() {
148                 // used to create the signleton
149         }
150     /**
151      * constructor:
152      * 
153      * @param {Object} cfg - Configuration
154      *     (basically repopath is currently only critical one.)
155      *
156      */
157      
158     private GitRepo(string path) {
159         // cal parent?
160         this.name =   File.new_for_path(path).get_basename();
161         this.ignore_files = new Gee.HashMap<string,bool>();
162         
163         this.git_working_dir = path;
164         this.gitdir = path + "/.git";
165         if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
166             this.gitdir = path; // naked...
167         }
168         this.cmds = new  Gee.ArrayList<GitMonitorQueue> ();
169         
170                 var cache = GitRepo.singleton().cache;
171         //Repo.superclass.constructor.call(this,cfg);
172                 if ( !cache.has_key(path) ) {
173                         cache.set( path, this);
174         }
175         
176         var r = this.git({ "remote" , "get-url" , "--push" , "origin"});
177         var uri = new Soup.URI(r);      
178         this.host = uri.get_host();
179
180         
181         this.loadBranches();
182         this.loadActiveTicket();
183         this.loadStatus();
184     } 
185     
186     public bool is_master_branch()
187     {
188         // special branches that do not allow autopushing now...
189         return this.currentBranch.name == "master" || this.currentBranch.name == "roojs";
190                 
191     }
192     public void init_config()
193     {
194         // managed = 
195         if (this.get_config("managed") == "") {
196                 this.set_config("managed", this.host != "git.roojs.com" ? "1" : "0");
197                 }
198                 
199     
200     }
201     
202     
203     
204     public string get_config(string key) {
205         return this.git({ "config" , "gitlive." + key);
206         }
207     public string set_config(string key, string value) {
208         return this.git({ "config" , "gitlive." + key, value);
209         }
210     
211     public bool is_managed()
212     {
213         // is it a roojs origin?
214          
215         if (this.host != "git.roojs.com") { // we can only push to this url. -- unless we have forced it to be managed.
216                 var r = this.git({ "config" , "gitlive.managed");
217                 
218                 return FileUtils.test(this.gitdir + "/.gitlive-managed" , FileTest.EXISTS);
219         }
220         // otherwise see if unmanaged is set to disable it..
221         return !FileUtils.test(this.gitdir + "/.gitlive-unmanaged" , FileTest.EXISTS);  
222
223     }
224     
225     
226     public bool is_autocommit ()
227     {           
228         return !FileUtils.test(this.gitdir + "/.gitlive-disable-autocommit" , FileTest.EXISTS);
229     }
230     public void set_autocommit(bool val)
231     {
232
233                 var cur = this.is_autocommit();
234                 GLib.debug("SET auto commit : %s <= %s", val ? "ON" : "OFF",  cur  ? "ON" : "OFF");
235                 if (cur == val) {
236                         return; // no change..
237                 }
238                 if (!val) {
239                         FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autocommit" , "x");
240                 } else {
241                         // it exists...
242                         FileUtils.remove(this.gitdir + "/.gitlive-disable-autocommit" ); 
243                 }
244     
245     }
246     
247     public bool is_auto_branch ()
248     {
249         if (this.name == "gitlog") {
250                 return false;
251                 }
252                 // check remote...
253         if (this.is_managed()) {
254                 return true;
255                 }
256         return false;
257         
258  
259     }
260     
261     public void set_auto_branch(bool val)
262     {
263
264                 var cur = this.is_auto_branch();
265                 GLib.debug("SET auto branch : %s <= %s", val ? "ON" : "OFF",  cur  ? "ON" : "OFF");
266                 
267                 if (cur == val) {
268                         return; // no change..
269                 }
270         if (this.host != "git.roojs.com") { // we can only push to this url. -- unless we have forced it to be managed.
271                 // remote is not our server..
272                 if (val) {
273                         FileUtils.set_contents(this.gitdir + "/.gitlive-managed" , "x");
274                 } else {
275                         FileUtils.remove(this.gitdir + "/.gitlive-managed" ); 
276                         }
277                 return;
278
279         }
280         
281         if (val) {
282                         FileUtils.remove(this.gitdir + "/.gitlive-unmanaged" ); 
283                 } else {
284                 FileUtils.set_contents(this.gitdir + "/.gitlive-unmanaged" , "x");              
285
286                 }
287           
288     
289     }
290     public bool is_autopush ()
291     {
292         return !FileUtils.test(this.gitdir + "/.gitlive-disable-autopush" , FileTest.EXISTS);
293     }
294     public void set_autopush(bool val)
295     {
296
297                 var cur = this.is_autopush();
298                 GLib.debug("SET auto push : %s <= %s", val ? "ON" : "OFF",  cur  ? "ON" : "OFF");
299                 if (cur == val) {
300                         return; // no change..
301                 }
302                 if (!val) {
303                         FileUtils.set_contents(this.gitdir + "/.gitlive-disable-autopush" , "");
304                 } else {
305                         // it exists...
306                         FileUtils.remove(this.gitdir + "/.gitlive-disable-autopush" ); 
307                 }
308     
309     }
310     
311     
312         public void loadStatus()
313         {
314                 var r = this.git({ "status" , "--porcelain" });
315                 this.git_status = r;
316                 this.has_local_changes = r.length > 0;
317                 
318                 var rs = this.git({ "status" , "-sb" });
319
320                 this.ahead_or_behind = rs.contains("[ahead") ? "A" : (rs.contains("[behind") ? "B" : "");
321                 
322                 
323                 this.git_diff  = this.git({ "diff" , "HEAD", "--no-color" });
324         }    
325
326     
327     public void loadBranches()
328     {
329
330         GitBranch.loadBranches(this);
331     }
332      
333     
334     
335     
336     public string branchesToString()
337     {
338         var ret = "";
339                 foreach( var br in this.branches.values) {
340                         if (br.name == "") {
341                                 continue; 
342                         }
343                         ret += ret.length > 0 ? "\n"  : "";
344                         ret += br.name;
345                 
346                 }
347                 return ret;
348         
349     }
350      public static void doMerges(string action, string ticket_id, string commit_message)
351     {
352        GitMonitor.gitmonitor.stop();
353        
354        var commitrevs = "";
355        var sucess = true;
356        foreach(var  repo in GitRepo.singleton().cache.values) {
357                if (repo.activeTicket != null && repo.activeTicket.id == ticket_id) {
358                        var res = repo.doMerge(action,ticket_id, commit_message);
359                        if (!res) {
360                                sucess = false;
361                                continue;
362                        }
363                        commitrevs += commitrevs.length > 0 ? " " : "";
364                        commitrevs += repo.currentBranch.lastrev;
365                }
366        }
367        if (sucess && action == "CLOSE") {
368                RooTicket.singleton().getById(ticket_id).close(commitrevs);
369        }
370        GitMonitor.gitmonitor.start();
371     }
372      
373
374     public bool doMerge(string action, string ticket_id, string commit_message)
375     {
376        // in theory we should check to see if other repo's have got the same branch and merge all them at the same time.
377        // also need to decide which branch we will merge into?
378                    var ret = "";
379                    if (action == "CLOSE" || action == "LEAVE") {
380                                    
381                try {
382                    var oldbranch = this.currentBranch.name;
383                    this.setActiveTicket(null, "master");
384                            string [] cmd = { "merge",   "--squash",  oldbranch };
385                            this.git( cmd );
386                            cmd = { "commit",   "-a" , "-m",  commit_message };
387                            this.git( cmd );
388                            this.push();
389                            this.loadBranches(); // updates lastrev..
390                
391                        var notification = new Notify.Notification(
392                                "Merged branch %s to master".printf(oldbranch),
393                                "",
394                                 "dialog-information"
395                                
396                        );
397
398                        notification.set_timeout(5);
399                        notification.show();   
400                
401                // close ticket..
402                return true; 
403                
404            } catch (Error e) {
405
406                GitMonitor.gitmonitor.pauseError(e.message);
407                return false;
408            }
409            // error~?? -- show the error dialog...
410                    return false;
411        }
412        if (action == "MASTER") {
413                // merge master into ours..
414                        try {
415                        string[] cmd = { "merge",  "master" };
416                        this.git( cmd );
417                        var notification = new Notify.Notification(
418                                        "Merged code from master to %s".printf(this.currentBranch.name),
419                                        "",
420                                         "dialog-information"
421                                        
422                                );
423                                notification.set_timeout(5);
424                                notification.show();   
425                       
426                        return true;
427                        } catch (Error e) {
428                        GitMonitor.gitmonitor.pauseError(e.message);
429                        return false;
430                    }
431            }
432        if (action == "EXIT") {
433                        try {
434                        var oldbranch  = this.currentBranch.name;
435                          this.setActiveTicket(null, "master");
436                        this.loadBranches();
437                        var notification = new Notify.Notification(
438                                        "Left branch %s".printf(oldbranch),
439                                        "",
440                                         "dialog-information"
441                                        
442                                );
443                                notification.set_timeout(5);
444                                notification.show();   
445                        
446                        return true;
447                    } catch (Error e) {
448                        GitMonitor.gitmonitor.pauseError(e.message);
449
450                        return false;                   
451                    }
452                    // error~?? -- show the error dialog...
453
454        }
455        return false;
456     }
457         
458     public void loadActiveTicket()
459     {
460         this.activeTicket = null;
461         if (!FileUtils.test(this.gitdir + "/.gitlive-active-ticket" , FileTest.EXISTS)) {
462                 return;
463         }
464         string ticket_id;
465         FileUtils.get_contents(this.gitdir + "/.gitlive-active-ticket" , out ticket_id);  
466         if (ticket_id.length < 1) {
467                 return;
468                 }
469                 this.activeTicket = RooTicket.singleton().getById(ticket_id.strip());
470         
471         
472     }
473     
474     
475     
476     public bool setActiveTicket(RooTicket? ticket, string branchname)
477     {
478         if (!this.createBranchNamed(branchname)) {
479                 return false;
480                 }
481                 if (ticket != null) {
482                 FileUtils.set_contents(this.gitdir + "/.gitlive-active-ticket" , ticket.id);
483         } else {
484                 FileUtils.remove(this.gitdir + "/.gitlive-active-ticket" );
485         }
486         this.activeTicket = ticket;
487         return true;
488     }
489     
490     public bool createBranchNamed(string branchname)
491     {   
492                 
493
494                      if (this.branches.has_key(branchname)) {
495                         this.switchToExistingBranchNamed(branchname);
496                     
497                     } else {
498                                  this.createNewBranchNamed(branchname); 
499                             
500                     }
501                        var notification = new Notify.Notification(
502                        "Changed to branch %s".printf(branchname),
503                        "",
504                         "dialog-information"
505                        
506                );
507
508                notification.set_timeout(5);
509                notification.show();   
510        
511          
512          this.loadBranches(); // update branch list...
513          //GitMonitor.gitmonitor.runQueue(); // no point - we have hidden the queue..
514          return true;
515     }
516      bool switchToExistingBranchNamed(string branchname)
517      {
518                 var stash = false;
519                                          // this is where it get's tricky...
520                 string files = "";
521                 try {                   
522                                 string[] cmd = { "ls-files" ,  "-m" };                   // list the modified files..
523                                 files = this.git(cmd);
524                                 stash = files.length> 1 ;
525                                 
526                                 
527                                 cmd = { "stash" };                      
528                                 if (stash) { this.git(cmd); }
529                                 
530                                 this.pull();
531                                 
532                                 cmd = { "checkout", branchname  };
533                                 this.git(cmd);
534                   } catch(Error e) {
535                                 GitMonitor.gitmonitor.pauseError(e.message);
536                                 return false;           
537                   }
538                 try {
539                    if (branchname != "master") {
540                        string[] cmd = { "merge", "master"  };
541                             this.git(cmd);
542                             this.push();
543                        
544                     }
545                     
546                 } catch(Error e) {
547                     string[] cmd = { "checkout", "master"  };
548                     this.git(cmd);
549                         GitMonitor.gitmonitor.pauseError(
550                                 "Use\n\na) git checkout %s\nb) git mergetool\nc) git commit\nd) git push\n d) stash pop \ne) start gitlive again\n".printf(
551                                         branchname)
552                                  + e.message
553                         );
554                         return false;           
555                  
556                 }
557                 try {                                   
558                     string[]  cmd = { "stash", "pop"  };
559                     if (stash) { 
560                         this.git(cmd); 
561                         var fl = files.split("\n");
562                         cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
563                         foreach(var f in fl) {
564                                 if (f.length < 1) continue;
565                                 cmd += f;
566                         }
567                         this.git(cmd);                              
568                 }
569              
570
571                    
572                 } catch(Error ee) {
573                         GitMonitor.gitmonitor.pauseError(ee.message);
574                         return false;           
575                 }
576        this.push();
577        return true;                             
578                  
579      }
580     
581     
582     
583      bool createNewBranchNamed(string branchname)
584      {
585                 var stash = false;
586                  try {                                  
587                                 string[] cmd = { "ls-files" ,  "-m" };                   // list the modified files..
588                                 var files = this.git(cmd);
589                                 stash = files.length> 1 ;
590                         
591                          cmd = { "checkout", "-b" , branchname  };
592                         this.git(cmd);
593
594                cmd = { "push", "-u" , "origin" ,"HEAD"  };
595                         this.git(cmd);
596                                 if (stash) { 
597
598                                 var fl = files.split("\n");
599                                 cmd = { "commit", "-m" , "Changed " + string.joinv("",fl) };
600                                 foreach(var f in fl) {
601                                         if (f.length < 1) continue;
602                                         cmd += f;
603                                 }
604                                 this.git(cmd);  
605                                 this.push();                        
606                         }
607
608              
609                 } catch(Error ee) {
610                                 GitMonitor.gitmonitor.pauseError(ee.message);
611                                 return false;           
612                         }
613                         return true;
614      
615      }
616     
617     
618     
619     /**
620      * add:
621      * add files to track.
622      *
623      * @argument {Array} files the files to add.
624      */
625     public string add ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
626     {
627         // should really find out if these are untracked files each..
628         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
629         // not sure if that is how git works.. but just be certian.
630         var ret = "";
631         for (var i = 0; i < files.size;i++) {
632             var f = files.get(i).vname;
633             try {
634                 string[] cmd = { "add",    f  };
635                 this.git( cmd );
636             } catch (Error e) {
637                 ret += e.message  + "\n";
638             }        
639
640         }
641         return ret;
642     }
643         
644     public bool is_ignore(string fname) throws Error, SpawnError
645     {
646                 if (fname == ".gitignore") {
647                         this.ignore_files.clear();
648                 }
649                 
650                 if (this.ignore_files.has_key(fname)) {
651                         return this.ignore_files.get(fname);
652                 }
653                 
654                 try {
655                         var ret = this.git( { "check-ignore" , fname } );
656                         this.ignore_files.set(fname, ret.length >  0);
657                         return ret.length > 0;
658                 } catch (SpawnError e) {
659                         this.ignore_files.set(fname, false);
660                         return false;
661                 }
662                  
663     } 
664     
665     
666       /**
667      * remove:
668      * remove files to track.
669      *
670      * @argument {Array} files the files to add.
671      */
672     public string remove  ( Gee.ArrayList<GitMonitorQueue> files ) throws Error, SpawnError
673     {
674         // this may fail if files do not exist..
675         // should really find out if these are untracked files each..
676         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
677         // not sure if that is how git works.. but just be certian.
678         var ret = "";
679
680         for (var i = 0; i < files.size;i++) {
681             var f = files.get(i).vname;
682             try {
683                 string[] cmd = { "rm",  "-f" ,  f  };
684                 this.git( cmd );
685             } catch (Error e) {
686                 ret += e.message  + "\n";
687             }        
688         }
689
690         return ret;
691
692     }
693     
694     
695     /**
696      * commit:
697      * perform a commit.
698      *
699      * @argument {Object} cfg commit configuration
700      * 
701      * @property {String} name (optional)
702      * @property {String} email (optional)
703      * @property {String} changed (date) (optional)
704      * @property {String} reason (optional)
705      * @property {Array} files - the files that have changed. 
706      * 
707      */
708      
709     public string commit ( string message, Gee.ArrayList<GitMonitorQueue> files  ) throws Error, SpawnError
710     {
711         
712
713         /*
714         var env = [];
715
716         if (typeof(cfg.name) != 'undefined') {
717             args.push( {
718                 'author' : cfg.name + ' <' + cfg.email + '>'
719             });
720             env.push(
721                 "GIT_COMMITTER_NAME" + cfg.name,
722                 "GIT_COMMITTER_EMAIL" + cfg.email
723             );
724         }
725
726         if (typeof(cfg.changed) != 'undefined') {
727             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
728             
729         }
730         */
731         string[] args = { "commit", "-m" };
732         args +=  (message.length > 0  ? message : "Changed" );
733         for (var i = 0; i< files.size ; i++ ) {
734             args += files.get(i).vname; // full path?
735         }
736          
737         return this.git(args);
738     }
739     
740     /**
741      * pull:
742      * Fetch and merge remote repo changes into current branch..
743      *
744      * At present we just need this to update the current working branch..
745      * -- maybe later it will have a few options and do more stuff..
746      *
747      */
748     public string pull () throws Error, SpawnError
749     {
750         // should probably hand error conditions better... 
751         string[] cmd = { "pull" , "--no-edit" };
752         return this.git( cmd );
753
754         
755     }
756     
757     public delegate void GitAsyncCallback (GitRepo repo, int err, string str);
758     public void pull_async(GitAsyncCallback cb) 
759     {
760     
761          string[] cmd = { "pull" , "--no-edit" };
762          this.git_async( cmd , cb);
763          
764     
765     }
766     
767     /**
768      * push:
769      * Send local changes to remote repo(s)
770      *
771      * At present we just need this to push the current branch.
772      * -- maybe later it will have a few options and do more stuff..
773      *
774      */
775     public string push () throws Error, SpawnError
776     {
777         // should 
778         return this.git({ "push"  });
779         
780     }
781     
782     
783     
784      /**
785      * git:
786      * The meaty part.. run spawn.. with git..
787      *
788      *
789      */
790     
791     public string git(string[] args_in ) throws Error, SpawnError
792     {
793         // convert arguments.
794         
795         string[]  args = { "git" };
796         //args +=  "--git-dir";
797         //args +=  this.gitdir;
798         args +=  "--no-pager";
799  
800  
801         //if (this.gitdir != this.repopath) {
802         //    args +=   "--work-tree";
803          //   args += this.repopath; 
804         //}
805         for (var i = 0; i < args_in.length;i++) {
806             args += args_in[i];
807         }            
808
809         //this.lastCmd = args.join(" ");
810         //if(this.debug) {
811             GLib.debug( "CWD=%s",  this.git_working_dir ); 
812             GLib.debug( "cmd: %s", string.joinv (" ", args)); 
813         //}
814
815         string[]   env = {};
816         string  home = "HOME=" + Environment.get_home_dir() ;
817         env +=  home ;
818         // do not need to set gitpath..
819         //if (File.exists(this.repo + '/.git/config')) {
820             //env.push("GITPATH=" + this.repo );
821         //}
822           
823         var cfg = new SpawnConfig(this.git_working_dir , args , env);
824         //cfg.debug = true;
825
826        // may throw error...
827         var sp = new Spawn(cfg);
828       
829         // diff output is a bit big..
830                 if (args_in[0] != "diff") {
831                 GLib.debug( "GOT: %s" , sp.output);
832         }
833         // parse output for some commands ?
834         return sp.output;
835     }
836         
837    unowned GitAsyncCallback git_async_on_callback;
838         public void  git_async( string[] args_in,   GitAsyncCallback cb ) throws Error, SpawnError
839     {
840         // convert arguments.
841        this.git_async_on_callback = cb;
842         string[]  args = { "git" };
843         //args +=  "--git-dir";
844         //args +=  this.gitdir;
845         args +=  "--no-pager";
846  
847  
848         //if (this.gitdir != this.repopath) {
849         //    args +=   "--work-tree";
850          //   args += this.repopath; 
851         //}
852         for (var i = 0; i < args_in.length;i++) {
853             args += args_in[i];
854         }            
855
856         //this.lastCmd = args.join(" ");
857         //if(this.debug) {
858             GLib.debug( "CWD=%s",  this.git_working_dir ); 
859             //print( "cmd: %s\n", string.joinv (" ", args)); 
860         //}
861
862         string[]   env = {};
863         string  home = "HOME=" + Environment.get_home_dir() ;
864         env +=  home ;
865         // do not need to set gitpath..
866         //if (File.exists(this.repo + '/.git/config')) {
867             //env.push("GITPATH=" + this.repo );
868         //}
869         
870
871         var cfg = new SpawnConfig(this.git_working_dir , args , env);
872         cfg.async = true;
873        
874
875        // may throw error...
876         var sp = new Spawn(cfg);
877                 //sp.ref();
878         //this.ref();
879         sp.run(this.git_async_on_complete); 
880          
881     }
882     
883     void git_async_on_complete(int err, string output)
884     {
885                 GLib.debug("GOT %d : %s", err, output);
886                 this.git_async_on_callback(this, err, output);
887 //              this.unref();   
888         //      sp.unref();             
889     
890     
891     }
892     
893  
894          
895     
896  
897     public void update_async(GitAsyncCallback cb) 
898     {
899          string[] cmd = { "fetch" , "--all" };
900          this.git_async( cmd , cb);
901          
902     }
903     
904     
905     static uint update_all_total = 0;
906     static string update_all_after = "";
907      
908     public static void updateAll(string after)
909     {
910                 update_all_after = after;
911                 var tr =  GitRepo.singleton().cache;
912             
913         
914        update_all_total = tr.size;
915        foreach(var repo  in tr.values) {
916                 if (!repo.is_managed()) {
917                         update_all_total--;                     
918                         continue;
919                 }
920            repo.update_async(updateAllCallback); 
921         } 
922
923     }
924     public static void  updateAllCallback(GitRepo repo, int err, string res)
925     {
926         repo.loadBranches();
927         repo.loadStatus();
928         
929         update_all_total--;
930         if (update_all_total > 0 ) {
931                 return;
932                 }
933                 switch (update_all_after) {
934                         case "show_clones":
935                                 Clones.singleton().show();
936                                 break;
937                         default:
938                                 break;
939                 }
940                 return;
941     }
942     
943     
944     
945 }