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