Git.vala
[gitlive] / Git.vala
1
2 // valac -o /tmp/ggit Git.vala --pkg libgit2-glib-1.0 --pkg libsoup-2.4 --pkg gee-0.8 -g
3  
4
5 void main()
6 {
7          GLib.Log.set_handler(null, 
8             GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, 
9             (dom, lvl, msg) => {
10                                         
11                                         
12                // should we debug..
13                                         
14                         
15                print("%s\n", msg);
16             }
17          ); 
18
19         
20         Ggit.init();
21         var a = new GitLive.Repo("/home/alan/gitlive/web.Texon");
22         a.fetchAll();
23         return;
24         /*
25         GLib.Timeout.add (1, () => {
26       GLib.debug("Meanwhile");
27       return true;
28     }, GLib.Priority.HIGH);
29         
30         var loop = new MainLoop();
31          
32         var a = new GitLive.Repo("/home/alan/gitlive/web.coba");
33       GLib.debug("Starting");   
34         a.loadRemoteHeads.begin(true, (obj,res) => {
35                 a.loadRemoteHeads.end(res);
36                  GLib.debug("got results");
37                 a.loadLocalBranches();
38                 loop.quit();
39         });
40         loop.run();
41         //a.mergeMasterIntoHead();
42         //a.walkDiff();
43         //return;
44         //a.is_managed();
45         //a.is_autocommit();
46         
47
48         //a.fetchAll();
49         //
50         /*
51         var a = new GitLive.Repo("/home/alan/git/test1-clone");
52         //a.fetchAll();
53         var str = (new GLib.DateTime.now_utc()).format("%Y-%m-%d %H:%M:%S");
54         GLib.FileUtils.set_contents("/home/alan/git/test1-clone/test1",  str); 
55         
56         var ix = a.repo.get_index();
57         ix.add_path("test1");
58         ix.write();
59         var treeoid = ix.write_tree();
60
61         var head = a.repo.get_head();
62         var parent = head.lookup() as Ggit.Commit;
63         Ggit.Commit[] parents = (parent == null) ? 
64                 new Ggit.Commit[] {} :
65                 new Ggit.Commit[] { parent };
66
67         var tree = a.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;// odd format.. 
68         
69         var sig = new Ggit.Signature.now("Alan Knowles", "alan@roojs.com");
70         a.repo.create_commit("HEAD", sig, sig, null, "test commit " + str, tree, parents);
71         */
72         // mmh.. no git add/commit in library...
73 //      string[] spawn_args = {"git", "commit", "-m", "test", "-a"};
74         //string[] spawn_env = Environ.get ();
75 //      Process.spawn_sync ("/home/alan/git/test1-clone", spawn_args, spawn_env, SpawnFlags.SEARCH_PATH, null);
76  
77         //a.pushAll();
78 }
79  
80
81 namespace  GitLive {
82
83         public class Repo : Object {
84
85                 string name = "";
86                 public Ggit.Repository  repo;
87                 Callbacks callbacks;
88
89                 public Repo(string path)
90                 {
91                         this.name = GLib.Path.get_basename(path);
92                         this.repo = Ggit.Repository.open(GLib.File.new_for_path(path));
93                         this.callbacks = new Callbacks(this);
94                 
95                 }
96                 Ggit.Branch                                             head = null;
97             Gee.ArrayList<Ggit.Branch>          branches = null;
98             Ggit.RemoteHead[]                           remote_heads = null;
99                 public void loadLocalBranches(bool force = false)
100                 {
101                         if (!force && this.branches != null) {
102                                 return;
103                         }
104                         
105                         this.branches =  new Gee.ArrayList<Ggit.Branch>();
106                         var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
107                         while (r.next()) {
108                                 var br = r.get() as Ggit.Branch;
109                                 if (br == null) {
110                                         continue;
111                                 }
112                                 if (br.is_tag()) {
113                                         continue;
114                                 }
115                                 
116                                 //var head = this.repo.revparse("refs/heads/" + br.get_name() ).get_id();
117                                 //var rhead = this.repo.revparse(br.get_upstream().get_name() ).get_id();
118                                 try {
119                                         GLib.debug("got branch: N=%s", br.get_name() );
120                                         GLib.debug("is_head %s", br.is_head() ? "Y" : "n");
121
122                                         GLib.debug("upstream = %s", br.get_upstream() == null ? "??" : br.get_upstream().get_name().substring(20));
123                                         GLib.debug("oid = %s",br.get_target().to_string());
124                                         this.branches.add(br);
125                                         if (br.is_head()) {
126                                                 GLib.debug("HEAD= %s", br.get_name());
127                                                 this.head = br;
128                                         }
129                                 } catch (Error e) {
130                                         GLib.debug("Skip branch");
131                                 }
132                         }
133                         
134                         
135                 }
136                  
137                 public bool is_managed()
138                 {
139                         GLib.debug("is_managed: %d", this.repo.get_config().get_int32("gitlive.managed"));
140                         return this.repo.get_config().get_int32("gitlive.managed") == 1;
141                 }
142                 
143                 
144                 public bool is_autocommit ()
145                 {       
146                         GLib.debug("is_autocommit: %d", this.repo.get_config().get_int32("gitlive.autocommit"));
147                         return this.repo.get_config().get_int32("gitlive.autocommit") == 1;
148                 }
149                 public void set_autocommit(bool val)
150                 {
151                         this.repo.get_config().set_int32("gitlive.autocommit", val ? 1 : 0);
152                 
153                 }
154                  
155                 public void walkDiff()
156                 {
157                         this.loadLocalBranches();
158                         
159                         
160                         
161                         var oid =  this.repo.revparse(this.head.get_name() ).get_id()  ;
162                         var moid =  this.repo.revparse("refs/heads/master" ).get_id()  ;
163                         
164                         var a = new Ggit.RevisionWalker(this.repo);
165                         a.set_sort_mode(Ggit.SortMode.TOPOLOGICAL);
166                         a.push(oid);
167                         a.hide(moid);
168                         var last = oid;
169                         for (var noid = a.next(); noid != null; noid= a.next()) {
170                                 //var commit = this.repo.lookup(noid, typeof(Ggit.Commit)) as Ggit.Commit;
171                                 GLib.debug("rev: %s",
172                                         noid.to_string()
173                                 );
174                                 last = noid;
175                         }
176                         var commit = this.repo.lookup(last, typeof(Ggit.Commit)) as Ggit.Commit;
177                         var parent = commit.get_parents();
178                         GLib.debug("parent = %s", parent.get_id(0).to_string());
179                         var master_rev =  parent.get_id(0);
180                         var master_commit  = this.repo.lookup_commit(master_rev);;
181                         
182                         var head_commit = this.repo.lookup_commit(oid);
183                         
184                         var master_tree = master_commit.get_tree();
185                         var head_tree = head_commit.get_tree();
186                         
187                         var diff = new Ggit.Diff.tree_to_tree(this.repo, master_tree, head_tree, new Ggit.DiffOptions());
188                         
189                         diff.print(Ggit.DiffFormatType.PATCH, ( delta,  hunk,  line) => {
190                                 GLib.debug("%d: %s, %s", line.get_new_lineno(), line.get_origin().to_string(), line.get_text());
191                                 return 0;
192                         });
193                         // let's try a merge..
194                         var mo = new Ggit.MergeOptions();
195                         mo.set_file_favor(Ggit.MergeFileFavor.THEIRS);
196                         var ix = this.repo.merge_trees(master_tree, master_tree, head_tree, mo);
197                         
198                         if (ix.has_conflicts()) {
199                                 GLib.debug("merge has conflicts");
200                                 return;
201                         }
202                          var treeoid = ix.write_tree();
203                          
204                          
205                           
206                         var parents =   new Ggit.Commit[] { master_commit };
207
208
209                         var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;
210                         
211                         var sig = new Ggit.Signature.now(
212                                         this.repo.get_config().get_string("user.name"),
213                                         this.repo.get_config().get_string("user.email")
214                         );
215                         
216                         
217                         
218                         this.repo.create_commit("HEAD", sig, sig, null, "Test Merge", new_tree, parents);
219          
220                         
221                         
222                 }
223                 
224                 public void diffhead()
225                 {
226                         var r = this.repo.enumerate_branches(Ggit.BranchType.LOCAL);
227                         Ggit.Branch head;
228                         while (r.next()) {
229                                 var gbr = r.get() as Ggit.Branch;
230                                 if (gbr.is_head()) {
231                                         head = gbr;
232                                 }
233                         }
234                         GLib.debug("checking head=%s", head.get_name());
235                         var br = this.repo.lookup_branch(head.get_name(),Ggit.BranchType.LOCAL);
236                         var tree =  this.repo.lookup_tree(br.get_target());
237                         
238                         
239                 }
240                 
241                 
242                 public void mergeMasterIntoHead()
243                 {
244                         // assumes head is not master...
245                         this.loadLocalBranches();
246                         GLib.debug("head rev = %s", this.head.get_name());
247                         var head_oid =  this.repo.revparse(this.head.get_name() ).get_id()  ;
248                         //var master_oid =  this.repo.revparse("refs/heads/master" ).get_id()  ;
249                         var master_oid =  this.repo.revparse("HEAD" ).get_id()  ;
250
251                         var master_commit  = this.repo.lookup_commit(master_oid);;
252                         var head_commit = this.repo.lookup_commit(head_oid);
253                          
254                 
255                         var anc_oid = this.repo.merge_base(master_commit.get_id(), head_commit.get_id());
256                         
257                         var anc_commit = this.repo.lookup_commit(anc_oid);
258                         var anc_tree = anc_commit.get_tree();
259                         
260                         var mo = new Ggit.MergeOptions();
261                         var co = new Ggit.CheckoutOptions();
262                         var the_ref = this.repo.lookup_reference_dwim("refs/heads/master");
263
264                     var ac = new Ggit.AnnotatedCommit.from_ref(this.repo, the_ref);
265                         
266                         var commits =   new Ggit.AnnotatedCommit[] { ac };
267                         
268                         this.repo.merge(commits, mo, co);
269                         
270                         var cfg = this.repo.get_config().snapshot();
271                         
272                     var sig = new Ggit.Signature.now(
273                                         cfg.get_string("user.name"),
274                                         cfg.get_string("user.email")
275                         );
276                         var new_head = this.repo.get_head();
277                         var oid = new_head.get_target();
278
279                         var ix = this.repo.get_index();
280  
281                         ix.write();
282                         var treeoid = ix.write_tree();
283
284                         var new_tree = this.repo.lookup(treeoid,typeof (Ggit.Tree))  as   Ggit.Tree;
285                         
286                         var parent = new_head.lookup() as Ggit.Commit;
287                         Ggit.Commit[] parents =  new Ggit.Commit[] { parent };
288                         
289                         this.repo.create_commit("refs/heads/" + this.head.get_name(), sig, sig, null, "Test Merge", new_tree, parents);
290          
291                 }
292                 
293                 
294
295                 
296                 
297                 /*
298                 public bool doMergeClose(string commit_message)
299                 {
300                         this.loadLocalBranches(true);
301                         var oldbranch = this.head.get_name();
302             // going to asume this is merge trees..
303                            string [] cmd = { "merge",   "--squash",  oldbranch };
304                            this.git( cmd );
305                            cmd = { "commit",   "-a" , "-m",  commit_message };
306                            this.git( cmd );
307                            this.push();
308                            this.loadBranches(); // updates lastrev..
309                
310                        var notification = new Notify.Notification(
311                                "Merged branch %s to %s".printf(oldbranch, master),
312                                "",
313                                 "dialog-information"
314                                
315                        );
316
317                        notification.set_timeout(5);
318                        notification.show();   
319                
320                 
321                 }
322                 */
323                 
324                 
325                 public bool is_auto_branch ()
326                 {
327                         if (this.name == "gitlog") {
328                                 return false;
329                         }
330                         // check remote...
331                         if (this.is_managed()) {
332                                 return true;
333                         }
334                         return false;
335                         
336          
337                 }
338                 
339                 public async void loadRemoteHeads(bool force = false)
340                 {
341                     SourceFunc callback = loadRemoteHeads.callback;
342                         
343                         ThreadFunc<bool> run = () => {
344                                 
345                                 if (!force && this.remote_heads != null) {
346                                         return true;;
347                                 }
348                                 var r = this.repo.lookup_remote("origin");
349                                 r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
350                                 yield;
351                                 this.remote_heads = r.list();
352                                 
353                                 foreach(var br in this.remote_heads) {
354                                         if (!br.get_name().has_prefix("refs/heads/")) {
355                                                 continue;
356                                         }
357                                         
358                                         GLib.debug("Remote: name=%s  oid=%s local_oid=%s is_local=%s",
359                                                 br.get_name(),
360                                                 br.get_oid().to_string(),
361                                                 br.get_local_oid().to_string(),
362                                                 br.is_local() ? "Y" : "n"
363                                                 );
364                                 }
365                                 Idle.add((owned) callback);
366                                 return true;;
367                         };
368                         new Thread<bool>("loadRemoteHeads-" , run);
369                         yield;
370                         
371
372                 }
373                  
374                 
375                 Ggit.Branch? getBranch(string remote_name, string remote_branch_name)
376                 {
377                          //GLib.debug("bn=%s",remote_branch_name);
378                          if (remote_branch_name.has_prefix("refs/remotes/")) {
379                                  return null;
380                          }
381
382                          var target = remote_branch_name.replace("refs/heads/", remote_name+"/") .replace("refs/remotes/", "");
383                         if (target == "HEAD") {
384                                 target = remote_name +"/master";
385                         }
386                          
387                         foreach(var br in this.branches) {
388                         //      GLib.debug("test:%s=%s", br.get_upstream().get_shorthand() , target);
389                                 if ( br.get_upstream().get_shorthand() == target) {
390                                         return br;
391                                 }
392                                 
393                         }
394                         //GLib.debug("missing %s", remote_branch_name);                 
395                         return null;
396                 
397                 }
398                  
399                 
400                 public void fetchAll()
401                 {
402                         this.loadLocalBranches();
403                         this.loadRemoteHeads();
404                         
405                         // remotes probably will not work with http auth..
406                         //var ar = this.repo.list_remotes();
407                         //foreach(var n in ar) {
408                         var n = "origin"; 
409                         
410                                 GLib.debug("got remote '%s'", n);
411                                 var r = this.repo.lookup_remote(n);
412                                 GLib.debug("connecting '%s'", r.get_url());
413                                  
414                                 try {
415                                         string[] h = { "a = 1" };
416                                         r.connect(Ggit.Direction.FETCH, this.callbacks, null, null);
417                                         
418                                 } catch (Error e) {
419                                         GLib.debug("Got Error Message: %s", e.message);
420                                         return;
421                                 }
422                                 string[] far = {};
423
424                                 foreach(var rh in this.remote_heads) {
425                                         if (rh.get_name().has_prefix("refs/remotes/")) {
426                                                    continue;
427                                          }
428                                 
429                                         var br = this.getBranch(n, rh.get_name());
430                                  
431                                         /*GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
432                                                 rh.get_name(), 
433                                                 rh.get_oid().to_string(),
434                                                 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
435                                         );
436                                         */
437                                         //var loc_oid = this.repo.revparse(br.get_name() ).get_id();
438                                         
439                                         var loc_oid = br.get_target();
440                                         
441                                         size_t ahead, behind;
442                                         this.repo.get_ahead_behind(
443                                                 loc_oid,
444                                                 rh.get_oid(),
445                                                 out ahead,
446                                                 out behind
447                                         );
448                                         
449                                         if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
450                                                 continue;
451                                         }
452                                         if (ahead > 0) {
453                                                 continue;
454                                         }
455                                         far += ("+refs/heads/" + br.get_name()) + ":refs/remotes/" + n + "/" + rh.get_name().replace("refs/heads/","");
456                                 } 
457                                 
458                                 if (far.length < 1) {
459                                         GLib.debug("no fetch required.. it's uptodate");
460                                         r.disconnect();
461                                         return;
462                                 }
463                                 
464                                 GLib.debug("getting remote specs '%s', %d", n, far.length);
465                                 
466 /*
467                                 var far = r.get_fetch_specs();
468                                 */
469                                 foreach(var rs in far) {
470                                         GLib.debug("got remote spec: %s", rs);
471                                         
472                                 }
473
474                                 var options = new Ggit.FetchOptions();
475                                 options.set_remote_callbacks(this.callbacks);
476                                 //yield Async.thread(() => {
477         
478                                         r.download(far, options);
479                                 //});
480                                 r.disconnect();
481                                 
482                                 //r.download(
483                          
484                 
485                 }
486                 
487                 
488                 
489                 public void pushAll()
490                 {
491                         
492                         this.loadLocalBranches();
493                         this.loadRemoteHeads();
494                         // remotes probably will not work with http auth..
495                         //var ar = this.repo.list_remotes();
496                         var n = "origin";
497                         
498                                 GLib.debug("got remote '%s'", n);
499                                 var r = this.repo.lookup_remote(n);
500                                 GLib.debug("connecting '%s'", r.get_url());
501                                  
502                                 r.connect(Ggit.Direction.PUSH, this.callbacks, null, null);
503                                 
504                                 //GLib.debug("getting specs '%s'", n);
505                                 /* 
506                                 
507                                 this.repo.add_remote_push(
508                                         "origin",
509                                         "+%s:%s".printf(head.get_shorthand(),head.get_name())
510                                 );
511  */
512  
513                                 string[] far = {};
514                                 var heads = r.list();
515                                 foreach(var rh in heads) {
516                                         if (rh.get_name().has_prefix("refs/remotes/")) {
517                                                    continue;
518                                          }
519                                 
520                                         var br = this.getBranch(n, rh.get_name());
521                                         /*
522                                         GLib.debug("got heads: name=%s   rev=%s  localrev=%s",
523                                                 rh.get_name(), 
524                                                 rh.get_oid().to_string(),
525                                                 br == null ? "?": this.repo.revparse(br.get_name() ).get_id().to_string()
526                                         );*/
527                                         var loc_oid = this.repo.revparse(br.get_name() ).get_id();
528                                         size_t ahead, behind;
529                                         this.repo.get_ahead_behind(
530                                                 loc_oid,
531                                                 rh.get_oid(),
532                                                 out ahead,
533                                                 out behind
534                                         );
535                                         
536                                         if (rh.get_oid().to_string() == this.repo.revparse(br.get_name() ).get_id().to_string()) {
537                                                 continue;
538                                         }
539                                         if (behind > 0) {
540                                                 continue;
541                                         }
542                                         far += ("+refs/heads/" + br.get_name()) + ":"+ rh.get_name();
543                                 }  
544                                 
545                                 if (far.length < 1) {
546                                         GLib.debug("no push required.. it's uptodate");
547                                         r.disconnect();
548                                         return;
549                                 }
550  
551                                 /*var head = this.repo.get_head();
552                                 string[] far = {};
553                                 far += "+%s:%s".printf(head.get_name(),head.get_name());
554                                 */
555                                 foreach(var rs in far) {
556                                         GLib.debug("got remote spec: %s", rs);
557                                         
558                                 }
559                                         
560                                 var popts = new Ggit.PushOptions();
561                                  popts.callbacks = this.callbacks;
562                                 GLib.debug("Push?");
563                                 r.upload(far,popts);
564                                 GLib.debug("Push done?");
565                                 r.disconnect();
566                                 
567                                 
568                                 //r.download(
569                          
570                 
571                 }
572                 
573                  
574
575         }
576         
577         private class Callbacks : Ggit.RemoteCallbacks
578         {
579                 //private Remote d_remote;
580                 private Ggit.RemoteCallbacks? d_proxy = null;
581
582                 public delegate void TransferProgress(Ggit.TransferProgress stats);
583                 //private TransferProgress? d_transfer_progress;
584
585                 public Callbacks( Repo repo)  //, Ggit.RemoteCallbacks? proxy) //,Remote remote, owned TransferProgress? transfer_progress)
586                 {
587                         //d_remote = remote;
588                         //d_proxy = proxy;
589                         //d_transfer_progress = (owned)transfer_progress;
590                 }
591
592                 protected override void progress(string message)
593                 {
594                         GLib.debug("progress: %s", message);
595                         if (d_proxy != null)
596                         {
597                                 d_proxy.progress(message);
598                         }
599                 }
600
601                 protected override void transfer_progress(Ggit.TransferProgress stats)
602                 {
603                         GLib.debug("transfer_progress");
604                         /*
605                         if (d_transfer_progress != null)
606                         {
607                                 d_transfer_progress(stats);
608                         }
609
610                         if (d_proxy != null)
611                         {
612                                 d_proxy.transfer_progress(stats);
613                         }
614                         */
615                 }
616
617                 protected override void update_tips(string refname, Ggit.OId a, Ggit.OId b)
618                 {
619                         GLib.debug("update_tips");
620                         //d_remote.tip_updated(refname, a, b);
621
622                         if (d_proxy != null)
623                         {
624                                 d_proxy.update_tips(refname, a, b);
625                         }
626                 }
627
628                 protected override void completion(Ggit.RemoteCompletionType type)
629                 {
630                         GLib.debug("completion");
631                         if (d_proxy != null)
632                         {
633                                 d_proxy.completion(type);
634                         }
635                 }
636
637                 protected override Ggit.Cred? credentials(string url, string? username_from_url, Ggit.Credtype allowed_types) throws Error
638                 {
639  
640                         GLib.debug("get credentials %s  UN=%s", url, username_from_url);
641                         var uri = new Soup.URI(url);
642
643                         if (uri != null) {
644                                 var ret = this.netrc(uri.get_host());
645                                 if (ret != null) {
646                                         return ret;
647                                 }
648                                 
649                                 //return new Ggit.CredPlaintext(username_from_url, "test");
650                         }
651                         return null;
652                         /*var provider = d_remote.credentials_provider;
653
654                         if (provider != null)
655                         {
656                                 ret = provider.credentials(url, username_from_url, allowed_types);
657                         }
658
659                         if (ret == null && d_proxy != null)
660                         {
661                                 ret = d_proxy.credentials(url, username_from_url, allowed_types);
662                         }
663                         
664                         return ret;
665                         */
666                 }
667                 public  Ggit.Cred netrc(string domain) 
668                 {
669                         string str;
670                         GLib.FileUtils.get_contents(GLib.Environment.get_home_dir() + "/.netrc", out str);
671                         var lines = str.split("\n");
672                         for(var i=0; i< lines.length; i++) {
673                                 // assumes one line per entry.. if not we are buggered...
674                                 //GLib.debug("got %s" , lines[i]);
675                         
676                                 var bits =  Regex.split_simple ("[ \t]+", lines[i].strip());
677                                 if (bits.length < 6 || bits[0] != "machine" || bits[1] != domain) {
678                                         continue;
679                                 }
680                                 GLib.debug("found password?");
681                                 // we are gussing.... 
682                                 return new Ggit.CredPlaintext(bits[3], bits[5]);
683
684                         }
685                         return null;
686
687
688                 
689                 
690                 }
691                 
692         }
693         
694         
695
696 }