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