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