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