Fix #5662 - Gitlive - create a tracking branch
[gitlive] / old_seed_version / Merger.js
1 Gtk = imports.gi.Gtk;
2 Gdk = imports.gi.Gdk;
3 Pango = imports.gi.Pango;
4 GLib = imports.gi.GLib;
5 Gio = imports.gi.Gio;
6 GObject = imports.gi.GObject;
7 GtkSource = imports.gi.GtkSource;
8 WebKit = imports.gi.WebKit;
9 Vte = imports.gi.Vte;
10 console = imports.console;
11 XObject = imports.XObject.XObject;
12 Merger=new XObject({
13     xtype: Gtk.Dialog,
14     listeners : {
15         destroy_event : function (self, event) {
16              this.el.hide();
17                         return false;
18         },
19         response : function (self, id) {
20           // hide
21              //if (id < 1) {
22                 this.el.hide();
23                 return;
24             //}
25             if (typeof(this.get('bug').getValue()) != 'object') {
26                 print("ERROR");
27                 return;
28             }
29          
30             this.el.hide();
31                 
32             //var val = this.get('bug').getValue();
33              //   Seed.print(val);
34         }
35     },
36     border_width : 3,
37     default_height : 700,
38     default_width : 800,
39     id : "Merger",
40     title : "Merger",
41     deletable : true,
42     show : function(c) {
43         
44         if (!this.el) {
45             this.init();
46         }
47         var _this = this;
48          
49           //this.el.set_title("Merger - ??? ");
50     //   this.el.set_title("Merger - " + this.repo.repopath);
51     
52     
53          Merger.loading = true; // stop change firing on combos.
54          /// load up branches
55          
56          this.get('/historyTreeStore').el.clear();
57          this.get('/changedFilesStore').el.clear();
58          this.get('/patchview').clear();
59         
60          
61          
62          this.get('/workingCombo').load(Merger.repo.branches);
63          
64          this.get('/releaseCombo').load(Merger.repo.branches);
65     
66     
67     
68     
69          Merger.loading = false;
70     
71         this.el.show_all();
72         //this.get('/ok_button').el.set_sensitive(false);
73         
74         // block until we return.
75         var run_ret = this.el.run();
76         if (run_ret < 1 ) {
77             return  "DONE";
78         }
79         print("RUN RETURN : " + run_ret);
80         return "DONE";
81         //print(JSON.stringify(this.get('bug').getValue()));
82        // return this.get('bug').getValue();
83         //this.success = c.success;
84     },
85     items : [
86         {
87             xtype: Gtk.VBox,
88             pack : function(p,e) {
89                     p.el.get_content_area().pack_start(e.el,true,true,0)
90                 },
91             items : [
92                 {
93                     xtype: Gtk.HBox,
94                     pack : "pack_start,false,true",
95                     items : [
96                         {
97                             xtype: Gtk.Label,
98                             label : "Working Branch"
99                         },
100                         {
101                             xtype: Gtk.ComboBox,
102                             listeners : {
103                                 changed : function (self) {
104                                    this.get('/historyTreeStore').loadTree();
105                                 }
106                             },
107                             id : "workingCombo",
108                             init : function() {
109                                 this.el = new Gtk.ComboBox.with_entry();
110                                 
111                                 
112                                 this.model  = new XObject(this.model);
113                                 this.model.init();
114                                 this.el.set_model(this.model.el);
115                                 this.el.set_entry_text_column (0);
116                                  XObject.prototype.init.call(this);
117                                 
118                             },
119                             load : function(tr)
120                             {
121                                 //this.insert(citer,iter,0);
122                                 this.model.el.clear();
123                                 
124                                 var master = false;
125                                 var working = false;
126                                 
127                                 for(var i =0 ; i < tr.length; i++) {
128                                     if (!tr[i].name.length) {
129                                         continue;
130                                     }
131                                     var ret = {  };
132                                     this.model.el.append(ret);
133                                     //print(JSON.stringify(ret,null,4));
134                                     if (tr[i].name == 'master') {
135                                         master = i;
136                                     }
137                                     if (tr[i].name == 'working') {
138                                         working = i;
139                                     }          
140                                     this.model.el.set_value(ret.iter, 0, '' + tr[i].name );
141                                     this.model.el.set_value(ret.iter, 1, '' + tr[i].rev  );
142                              
143                                     
144                                 }     
145                                 if (master !== false) {
146                                     this.el.set_active(master);
147                                 }
148                                 if (working !== false) {
149                                     this.el.set_active(working);
150                                 }
151                                 
152                             },
153                             model : {
154                                 xtype: Gtk.ListStore,
155                                 init : function() {
156                                     XObject.prototype.init.call(this);
157                                     this.el.set_column_types ( 3, [
158                                            GObject.TYPE_STRING, // file  
159                                           GObject.TYPE_STRING, // added
160                                           GObject.TYPE_STRING, // removed
161                                       ] );
162                                 }
163                             }
164                         },
165                         {
166                             xtype: Gtk.Label,
167                             label : "Release Branch"
168                         },
169                         {
170                             xtype: Gtk.ComboBox,
171                             listeners : {
172                                 changed : function (self) {
173                                    this.get('/historyTreeStore').loadTree();
174                                 }
175                             },
176                             id : "releaseCombo",
177                             init : function() {
178                                 this.el = new Gtk.ComboBox.with_entry();
179                                 
180                                 
181                                 this.model  = new XObject(this.model);
182                                 this.model.init();
183                                 this.el.set_model(this.model.el);
184                                 this.el.set_entry_text_column (0);
185                                  XObject.prototype.init.call(this);
186                                 
187                             },
188                             load : function(tr)
189                             {
190                                 //this.insert(citer,iter,0);
191                                 this.model.el.clear();
192                                 
193                                 var master = false;
194                                 var release = false;
195                                 
196                                 for(var i =0 ; i < tr.length; i++) {
197                                     var ret = {  };
198                                     if (!tr[i].name.length) {
199                                         continue;
200                                     }
201                                     
202                                     this.model.el.append(ret);
203                                     //print(JSON.stringify(ret,null,4));
204                                     if (tr[i].name == 'master') {
205                                         master = i;
206                                     }
207                                     if (tr[i].name == 'release') {
208                                         release = i;
209                                     }
210                                      
211                                     this.model.el.set_value(ret.iter, 0, '' + tr[i].name );
212                                     this.model.el.set_value(ret.iter, 1, '' + tr[i].rev  );
213                              
214                                     
215                                 }     
216                                 if (master !== false) {
217                                     this.el.set_active(master);
218                                 }
219                                 if (release !== false) {
220                                     this.el.set_active(release);
221                                 }
222                                 
223                             },
224                             model : {
225                                 xtype: Gtk.ListStore,
226                                 init : function() {
227                                     XObject.prototype.init.call(this);
228                                     this.el.set_column_types ( 3, [
229                                            GObject.TYPE_STRING, // file  
230                                           GObject.TYPE_STRING, // added
231                                           GObject.TYPE_STRING, // removed
232                                       ] );
233                                 }
234                             }
235                         }
236                     ]
237                 },
238                 {
239                     xtype: Gtk.VPaned,
240                     pack : "pack_end,true,true,0",
241                     position : 400,
242                     items : [
243                         {
244                             xtype: Gtk.HPaned,
245                             pack : "add",
246                             position : 200,
247                             items : [
248                                 {
249                                     xtype: Gtk.ScrolledWindow,
250                                     init : function() {
251                                         XObject.prototype.init.call(this);
252                                          this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
253                                     },
254                                     items : [
255                                         {
256                                             xtype: Gtk.TreeView,
257                                             listeners : {
258                                                 cursor_changed : function (self) {
259                                                 
260                                                     if (this.el.get_selection().count_selected_rows() < 1) {
261                                                         //nothing?
262                                                         return;
263                                                     }
264                                                     var model = this.get('/historyTreeStore');
265                                                     var ret = {};        
266                                                      var s = this.el.get_selection();
267                                                     s.get_selected(ret);
268                                                     
269                                                     var value = ''+ ret.model.get_value(ret.iter, 1).value.get_string();
270                                                      print("OUT?" + value);// id..
271                                                     // load the list in the right grid..
272                                                     var Repo = imports.Scm.Git.Repo.Repo;
273                                                     
274                                                     
275                                                    
276                                                     if (model.release === false) {
277                                                         return;
278                                                     }
279                                                     model.rev = value;
280                                                    
281                                                     var files = Merger.repo.changedFiles('/',   'rev', model.release + '..' + value);
282                                                     this.get('/changedFilesStore').load(files);
283                                                     return true;
284                                                 
285                                                 }
286                                             },
287                                             pack : "add",
288                                             init : function() {
289                                                 XObject.prototype.init.call(this);
290                                                     var description = new Pango.FontDescription.c_new();
291                                                description.set_size(8000);
292                                                  this.el.modify_font(description);
293                                             
294                                                  this.selection = this.el.get_selection();
295                                                   this.selection.set_mode( Gtk.SelectionMode.SINGLE);
296                                                 var _this = this;
297                                                 /*
298                                              780                                                                     
299                                              781                                                                         // is this really needed??
300                                              782                                                                         this.selection.signal['changed'].connect(function() {
301                                              783                                                                                 _this.get('/LeftTree.view').listeners.cursor_changed.apply(
302                                              784                                                                                     _this.get('/LeftTree.view'), [ _this.get('/LeftTree.view'), '']
303                                              785                                                                                 );
304                                              786                                                                         });
305                                              787                                                                         
306                                              */
307                                              },
308                                             items : [
309                                                 {
310                                                     xtype: Gtk.TreeStore,
311                                                     id : "historyTreeStore",
312                                                     pack : "set_model",
313                                                     release : false,
314                                                     init : function() {
315                                                         XObject.prototype.init.call(this);
316                                                         this.el.set_column_types ( 3, [
317                                                            GObject.TYPE_STRING, // title 
318                                                           GObject.TYPE_STRING, // rev 
319                                                       ] );
320                                                     },
321                                                     load : function(tr,iter)
322                                                     {
323                                                         //this.insert(citer,iter,0);
324                                                         if (!iter) {
325                                                             this.el.clear();
326                                                         }
327                                                         
328                                                         for(var i =0 ; i < tr.length; i++) {
329                                                             var ret = {  };
330                                                             if (iter) {
331                                                                 this.el.insert(ret ,iter,-1);
332                                                             } else {
333                                                                 this.el.append(ret);
334                                                             }
335                                                             //print(JSON.stringify(ret,null,4));
336                                                              
337                                                               
338                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].text );
339                                                             this.el.set_value(ret.iter, 1, '' + tr[i].rev  );
340                                                      
341                                                             if (tr[i].children && tr[i].children.length) {
342                                                                 this.load(tr[i].children, ret.iter);
343                                                             }
344                                                         }     
345                                                     },
346                                                     loadTree : function() {
347                                                     
348                                                        this.working = false;
349                                                        if (Merger.loading) {
350                                                             return;
351                                                        }
352                                                        
353                                                        
354                                                        var wid = this.get('/workingCombo').el.get_active();
355                                                        var rid = this.get('/releaseCombo').el.get_active();
356                                                        if (wid < 0 || rid < 0 || rid == wid) {
357                                                             return;
358                                                        }
359                                                        
360                                                        var w = Merger.repo.branches[wid];
361                                                        var r = Merger.repo.branches[rid];
362                                                        
363                                                     
364                                                     
365                                                         var rev = r.name + '..' + w.name;
366                                                         this.release = r.name;
367                                                         this.working = w.name;
368                                                         
369                                                         // this takes some time, lets. try and dialog it..
370                                                     
371                                                         
372                                                         
373                                                         
374                                                         var msg = new Gtk.MessageDialog( {
375                                                             buttons : Gtk.ButtonsType.NONE,
376                                                             text: "Loading History"
377                                                             
378                                                         });
379                                                         
380                                                         msg.set_transient_for(Merger.el);
381                                                         msg.set_modal(true);
382                                                         msg.show_all();
383                                                     
384                                                         var hist = Merger.repo.dayTree('/', false, 'rev', rev);
385                                                         msg.hide();
386                                                         
387                                                         this.load(hist);
388                                                             
389                                                            
390                                                     }
391                                                 },
392                                                 {
393                                                     xtype: Gtk.TreeViewColumn,
394                                                     pack : "append_column",
395                                                     title : "Changes",
396                                                     init : function() {
397                                                         XObject.prototype.init.call(this);
398                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
399                                                     
400                                                     },
401                                                     items : [
402                                                         {
403                                                             xtype: Gtk.CellRendererText,
404                                                             pack : "pack_start"
405                                                         }
406                                                     ]
407                                                 }
408                                             ]
409                                         }
410                                     ]
411                                 },
412                                 {
413                                     xtype: Gtk.ScrolledWindow,
414                                     init : function() {
415                                         XObject.prototype.init.call(this);
416                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
417                                     },
418                                     items : [
419                                         {
420                                             xtype: Gtk.TreeView,
421                                             listeners : {
422                                                 cursor_changed : function (self) {
423                                                   // SEE SELECTION.CHANGED
424                                                     var files = this.files();
425                                                     this.get('/patchview').showDiff(files); 
426                                                     //var value = ''+ ret.model.get_value(ret.iter, 1).value.get_string();
427                                                      //print("OUT?" + value);// id..
428                                                     // load the list in the right grid..
429                                                      
430                                                     return true;
431                                                 
432                                                 }
433                                             },
434                                             id : "changedFilesView",
435                                             pack : "add",
436                                             files : function() {
437                                                  if (this.el.get_selection().count_selected_rows() < 1) {
438                                                     //nothing? - clea it?
439                                                     return [];
440                                                 }
441                                             
442                                                 var ret = {};         
443                                              
444                                             
445                                                  var s = this.el.get_selection();
446                                                  var files = [];
447                                                   s.selected_foreach(function(model,p,iter) {
448                                                 
449                                                    files.push( model.get_value(iter, 0).value.get_string());
450                                                  
451                                                 });
452                                                 return files;
453                                             },
454                                             init : function() {
455                                                 XObject.prototype.init.call(this);
456                                                     var description = new Pango.FontDescription.c_new();
457                                                description.set_size(8000);
458                                                  this.el.modify_font(description);
459                                             
460                                                  this.selection = this.el.get_selection();
461                                                   this.selection.set_mode( Gtk.SelectionMode.MULTIPLE);
462                                                 var _this = this;
463                                               this.selection.signal['changed'].connect(function() {
464                                                  _this.listeners.cursor_changed.apply(
465                                                       _this, [ _this, '']
466                                                    );
467                                                          });
468                                               },
469                                             items : [
470                                                 {
471                                                     xtype: Gtk.ListStore,
472                                                     id : "changedFilesStore",
473                                                     pack : "set_model",
474                                                     init : function() {
475                                                         XObject.prototype.init.call(this);
476                                                         this.el.set_column_types ( 3, [
477                                                            GObject.TYPE_STRING, // file  
478                                                           GObject.TYPE_STRING, // added
479                                                           GObject.TYPE_STRING, // removed
480                                                       ] );
481                                                     },
482                                                     load : function(tr)
483                                                     {
484                                                         //this.insert(citer,iter,0);
485                                                         this.el.clear();
486                                                         for(var i =0 ; i < tr.length; i++) {
487                                                             var ret = {  };
488                                                            
489                                                             this.el.append(ret);
490                                                             
491                                                             //print(JSON.stringify(ret,null,4));
492                                                              
493                                                               
494                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].filename );
495                                                             this.el.set_value(ret.iter, 1, '' + tr[i].added  );
496                                                             this.el.set_value(ret.iter, 2, '' + tr[i].removed  );
497                                                      
498                                                             
499                                                         }     
500                                                     }
501                                                 },
502                                                 {
503                                                     xtype: Gtk.TreeViewColumn,
504                                                     resizable : true,
505                                                     min_width : 200,
506                                                     pack : "append_column",
507                                                     title : "Filename",
508                                                     init : function() {
509                                                         XObject.prototype.init.call(this);
510                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
511                                                     
512                                                     },
513                                                     items : [
514                                                         {
515                                                             xtype: Gtk.CellRendererText,
516                                                             pack : "pack_start"
517                                                         }
518                                                     ]
519                                                 },
520                                                 {
521                                                     xtype: Gtk.TreeViewColumn,
522                                                     min_width : 50,
523                                                     pack : "append_column",
524                                                     title : "Added",
525                                                     init : function() {
526                                                         XObject.prototype.init.call(this);
527                                                         this.el.add_attribute(this.items[0].el , 'markup', 1 );
528                                                     
529                                                     },
530                                                     items : [
531                                                         {
532                                                             xtype: Gtk.CellRendererText,
533                                                             pack : "pack_start"
534                                                         }
535                                                     ]
536                                                 },
537                                                 {
538                                                     xtype: Gtk.TreeViewColumn,
539                                                     min_width : 50,
540                                                     pack : "append_column",
541                                                     title : "Removed",
542                                                     init : function() {
543                                                         XObject.prototype.init.call(this);
544                                                         this.el.add_attribute(this.items[0].el , 'markup', 2 );
545                                                     
546                                                     },
547                                                     items : [
548                                                         {
549                                                             xtype: Gtk.CellRendererText,
550                                                             pack : "pack_start"
551                                                         }
552                                                     ]
553                                                 }
554                                             ]
555                                         },
556                                         {
557                                             xtype: Gtk.TreeView,
558                                             pack : false,
559                                             items : [
560                                                 {
561                                                     xtype: Gtk.ListStore,
562                                                     pack : false
563                                                 }
564                                             ]
565                                         }
566                                     ]
567                                 }
568                             ]
569                         },
570                         {
571                             xtype: Gtk.VBox,
572                             items : [
573                                 {
574                                     xtype: Gtk.HBox,
575                                     pack : "pack_start,false,false,0",
576                                     items : [
577                                         {
578                                             xtype: Gtk.Button,
579                                             listeners : {
580                                                 clicked : function (self) {
581                                                     
582                                                      
583                                                      var model = this.get('/historyTreeStore');
584                                                     
585                                                     if (model.release === false) {
586                                                         return;
587                                                     }
588                                                     
589                                                  
590                                                     var files = this.get('/changedFilesView').files();
591                                                     
592                                                      if (!files.length) {
593                                                         return; // error.!
594                                                     }
595                                                     var diff = Merger.repo.diff(files, model.release, model.rev);
596                                                    
597                                                    
598                                                    print("history?");
599                                                    
600                                                     
601                                                     var history =  Merger.repo.history(files, 1, 'rev', model.rev);
602                                                     
603                                                     print("History" + JSON.stringify(history, null,4));
604                                                     
605                                                     Commit = imports.Commit.Commit;
606                                                 
607                                                     Commit.el.set_modal(true);
608                                                     Commit.el.set_transient_for(Merger.el);
609                                                 
610                                                     var ce = Commit.show({
611                                                         repo : Merger.repo,
612                                                         files : files,
613                                                         release : model.release,
614                                                         rev : model.rev,
615                                                         author : history[0].changeby,
616                                                         changed : history[0].changed_raw
617                                                         
618                                                     
619                                                     });
620                                                     if (ce === false ) {
621                                                         return;
622                                                     }
623                                                     
624                                                     
625                                                     var diff = Merger.repo.diff(files, model.release, model.rev);
626                                                     
627                                                     
628                                                     print(JSON.stringify(ce,null,4));
629                                                     //.... commit!!!
630                                                     
631                                                     imports.GitMonitor.GitMonitor.pause();
632                                                     
633                                                     
634                                                     
635                                                     var msg = new Gtk.MessageDialog( {
636                                                         buttons : Gtk.ButtonsType.NONE,
637                                                         text: "Committing Changes"
638                                                     });
639                                                     
640                                                     msg.set_transient_for(Merger.el);
641                                                     msg.set_modal(true);
642                                                     msg.show_all();
643                                                 
644                                                      
645                                                     
646                                                     try { 
647                                                         
648                                                         Merger.repo.checkout(model.release);
649                                                         
650                                                         print("Call apply patch");
651                                                         
652                                                         Merger.repo.applyPatch(diff);
653                                                         
654                                                         var author = Merger.repo.parseAuthor(ce.author);
655                                                         
656                                                         print("Add new files.");
657                                                         // add all the files..
658                                                         Merger.repo.add(files);
659                                                         
660                                                         print("Commit changes.");
661                                                         Merger.repo.commit({
662                                                             name    : author.name,
663                                                             email   : author.email,
664                                                             author  : ce.author,
665                                                             changed : ce.changed,
666                                                             reason  : ce.message,
667                                                             files : files
668                                                             
669                                                         });
670                                                         
671                                                       
672                                                     } catch (e) {
673                                                      //message..
674                                                         msg.hide();
675                                                          
676                                                         var emsg = new Gtk.MessageDialog({
677                                                                 message_type: Gtk.MessageType.ERROR, 
678                                                                 buttons : Gtk.ButtonsType.OK, 
679                                                                 text: e.message
680                                                         });
681                                                         emsg.set_transient_for(Merger.el);
682                                                         emsg.set_modal(true);
683                                                         emsg.run();
684                                                         emsg.destroy();
685                                                         Merger.repo.stash(); // revert change.. - so we can go back...
686                                                     }
687                                                     msg.show_all();
688                                                     msg.set_markup("Changing to previous branch");
689                                                     Merger.repo.checkout(model.working);
690                                                     
691                                                     
692                                                     // if gitlive was previously running warn the user that it is now paused..
693                                                     /*
694                                                     var cmsg = new Gtk.MessageDialog({
695                                                             message_type: Gtk.MessageType.ERROR, 
696                                                             buttons : Gtk.ButtonsType.OK, 
697                                                             text: "Git Live is now paused "
698                                                     });
699                                                     cmsg.run();
700                                                     cmsg.destroy();
701                                                     */
702                                                     var _t = this;
703                                                     GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 5, function() {
704                                                          imports.GitMonitor.GitMonitor.resume();
705                                                          msg.hide();
706                                                          msg.destroy();
707                                                           _t.get('/historyTreeStore').loadTree();
708                                                           _t.get('/changedFilesStore').el.clear();
709                                                           _t.get('/patchview').showDiff(files); 
710                                                          return false; //only once.
711                                                     });
712                                                      
713                                                     
714                                                 
715                                                 
716                                                 }
717                                             },
718                                             label : "Commit diff (no merge)"
719                                         }
720                                     ]
721                                 },
722                                 {
723                                     xtype: Gtk.ScrolledWindow,
724                                     pack : "pack_end,true,true,0",
725                                     init : function() {
726                                         XObject.prototype.init.call(this);
727                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
728                                     },
729                                     items : [
730                                         {
731                                             xtype: WebKit.WebView,
732                                             id : "patchview",
733                                             pack : "add",
734                                             clear : function() {
735                                                 
736                                                 
737                                                 
738                                                  
739                                                // remove..
740                                                 var s  = "document.body.textContent='';"
741                                                     
742                                                 this.el.execute_script(s);
743                                                     
744                                                     
745                                                 
746                                                 
747                                                 
748                                             },
749                                             showDiff : function(files) {
750                                                 this.clear();
751                                                 
752                                                 
753                                                  var model = this.get('/historyTreeStore');
754                                                 
755                                                 if (model.release === false) {
756                                                     return;
757                                                 }
758                                                 
759                                                 var diff = Merger.repo.diff(files, model.release, model.rev);
760                                                 
761                                                // remove..
762                                                 var s  = "var pre  = document.createElement('pre'); document.body.appendChild(pre);";
763                                                 s += "pre.textContent =  " +
764                                                          JSON.stringify(Merger.repo.lastCmd + "\n") + '+  ' + 
765                                                        JSON.stringify(diff) + ";";
766                                                 print(s);
767                                                     
768                                                 this.el.execute_script(s);
769                                                     
770                                                     
771                                                 
772                                                 
773                                                 
774                                             }
775                                         }
776                                     ]
777                                 }
778                             ]
779                         }
780                     ]
781                 }
782             ]
783         },
784         {
785             xtype: Gtk.Button,
786             id : "ok_button",
787             label : "Close",
788             pack : "add_action_widget,1"
789         }
790     ]
791 });
792 Merger.init();
793 XObject.cache['/Merger'] = Merger;