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