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