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