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