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