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