Merger.js
[gitlive] / 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                                     var ret = {  };
129                                     this.model.el.append(ret);
130                                     //print(JSON.stringify(ret,null,4));
131                                     if (tr[i].name == 'master') {
132                                         master = i;
133                                     }
134                                     if (tr[i].name == 'working') {
135                                         working = i;
136                                     }          
137                                     this.model.el.set_value(ret.iter, 0, '' + tr[i].name );
138                                     this.model.el.set_value(ret.iter, 1, '' + tr[i].rev  );
139                              
140                                     
141                                 }     
142                                 if (master !== false) {
143                                     this.el.set_active(master);
144                                 }
145                                 if (working !== false) {
146                                     this.el.set_active(working);
147                                 }
148                                 
149                             },
150                             model : {
151                                 xtype: Gtk.ListStore,
152                                 init : function() {
153                                     XObject.prototype.init.call(this);
154                                     this.el.set_column_types ( 3, [
155                                            GObject.TYPE_STRING, // file  
156                                           GObject.TYPE_STRING, // added
157                                           GObject.TYPE_STRING, // removed
158                                       ] );
159                                 }
160                             }
161                         },
162                         {
163                             xtype: Gtk.Label,
164                             label : "Release Branch"
165                         },
166                         {
167                             xtype: Gtk.ComboBox,
168                             listeners : {
169                                 changed : function (self) {
170                                    this.get('/historyTreeStore').loadTree();
171                                 }
172                             },
173                             id : "releaseCombo",
174                             init : function() {
175                                 this.el = new Gtk.ComboBox.with_entry();
176                                 
177                                 
178                                 this.model  = new XObject(this.model);
179                                 this.model.init();
180                                 this.el.set_model(this.model.el);
181                                 this.el.set_entry_text_column (0);
182                                  XObject.prototype.init.call(this);
183                                 
184                             },
185                             load : function(tr)
186                             {
187                                 //this.insert(citer,iter,0);
188                                 this.model.el.clear();
189                                 
190                                 var master = false;
191                                 var release = false;
192                                 
193                                 for(var i =0 ; i < tr.length; i++) {
194                                     var ret = {  };
195                                     this.model.el.append(ret);
196                                     //print(JSON.stringify(ret,null,4));
197                                     if (tr[i].name == 'master') {
198                                         master = i;
199                                     }
200                                     if (tr[i].name == 'release') {
201                                         release = i;
202                                     }          
203                                     this.model.el.set_value(ret.iter, 0, '' + tr[i].name );
204                                     this.model.el.set_value(ret.iter, 1, '' + tr[i].rev  );
205                              
206                                     
207                                 }     
208                                 if (master !== false) {
209                                     this.el.set_active(master);
210                                 }
211                                 if (release !== false) {
212                                     this.el.set_active(release);
213                                 }
214                                 
215                             },
216                             model : {
217                                 xtype: Gtk.ListStore,
218                                 init : function() {
219                                     XObject.prototype.init.call(this);
220                                     this.el.set_column_types ( 3, [
221                                            GObject.TYPE_STRING, // file  
222                                           GObject.TYPE_STRING, // added
223                                           GObject.TYPE_STRING, // removed
224                                       ] );
225                                 }
226                             }
227                         }
228                     ]
229                 },
230                 {
231                     xtype: Gtk.VPaned,
232                     pack : "pack_end,true,true,0",
233                     position : 400,
234                     items : [
235                         {
236                             xtype: Gtk.HPaned,
237                             pack : "add",
238                             position : 200,
239                             items : [
240                                 {
241                                     xtype: Gtk.ScrolledWindow,
242                                     init : function() {
243                                         XObject.prototype.init.call(this);
244                                          this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
245                                     },
246                                     items : [
247                                         {
248                                             xtype: Gtk.TreeView,
249                                             listeners : {
250                                                 cursor_changed : function (self) {
251                                                 
252                                                     if (this.el.get_selection().count_selected_rows() < 1) {
253                                                         //nothing?
254                                                         return;
255                                                     }
256                                                     var model = this.get('/historyTreeStore');
257                                                     var ret = {};        
258                                                      var s = this.el.get_selection();
259                                                     s.get_selected(ret);
260                                                     
261                                                     var value = ''+ ret.model.get_value(ret.iter, 1).value.get_string();
262                                                      print("OUT?" + value);// id..
263                                                     // load the list in the right grid..
264                                                     var Repo = imports.Scm.Git.Repo.Repo;
265                                                     
266                                                     
267                                                    
268                                                     if (model.release === false) {
269                                                         return;
270                                                     }
271                                                     model.rev = value;
272                                                    
273                                                     var files = Merger.repo.changedFiles('/',   'rev', model.release + '..' + value);
274                                                     this.get('/changedFilesStore').load(files);
275                                                     return true;
276                                                 
277                                                 }
278                                             },
279                                             pack : "add",
280                                             init : function() {
281                                                 XObject.prototype.init.call(this);
282                                                     var description = new Pango.FontDescription.c_new();
283                                                description.set_size(8000);
284                                                  this.el.modify_font(description);
285                                             
286                                                  this.selection = this.el.get_selection();
287                                                   this.selection.set_mode( Gtk.SelectionMode.SINGLE);
288                                                 var _this = this;
289                                                 /*
290                                              780                                                                     
291                                              781                                                                         // is this really needed??
292                                              782                                                                         this.selection.signal['changed'].connect(function() {
293                                              783                                                                                 _this.get('/LeftTree.view').listeners.cursor_changed.apply(
294                                              784                                                                                     _this.get('/LeftTree.view'), [ _this.get('/LeftTree.view'), '']
295                                              785                                                                                 );
296                                              786                                                                         });
297                                              787                                                                         
298                                              */
299                                              },
300                                             items : [
301                                                 {
302                                                     xtype: Gtk.TreeStore,
303                                                     id : "historyTreeStore",
304                                                     pack : "set_model",
305                                                     release : false,
306                                                     init : function() {
307                                                         XObject.prototype.init.call(this);
308                                                         this.el.set_column_types ( 3, [
309                                                            GObject.TYPE_STRING, // title 
310                                                           GObject.TYPE_STRING, // rev 
311                                                       ] );
312                                                     },
313                                                     load : function(tr,iter)
314                                                     {
315                                                         //this.insert(citer,iter,0);
316                                                         if (!iter) {
317                                                             this.el.clear();
318                                                         }
319                                                         
320                                                         for(var i =0 ; i < tr.length; i++) {
321                                                             var ret = {  };
322                                                             if (iter) {
323                                                                 this.el.insert(ret ,iter,-1);
324                                                             } else {
325                                                                 this.el.append(ret);
326                                                             }
327                                                             //print(JSON.stringify(ret,null,4));
328                                                              
329                                                               
330                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].text );
331                                                             this.el.set_value(ret.iter, 1, '' + tr[i].rev  );
332                                                      
333                                                             if (tr[i].children && tr[i].children.length) {
334                                                                 this.load(tr[i].children, ret.iter);
335                                                             }
336                                                         }     
337                                                     },
338                                                     loadTree : function() {
339                                                     
340                                                        this.working = false;
341                                                        if (Merger.loading) {
342                                                             return;
343                                                        }
344                                                        
345                                                        
346                                                        var wid = this.get('/workingCombo').el.get_active();
347                                                        var rid = this.get('/releaseCombo').el.get_active();
348                                                        if (wid < 0 || rid < 0 || rid == wid) {
349                                                             return;
350                                                        }
351                                                        
352                                                        var w = Merger.repo.branches[wid];
353                                                        var r = Merger.repo.branches[rid];
354                                                        
355                                                     
356                                                     
357                                                         var rev = r.name + '..' + w.name;
358                                                         this.release = r.name;
359                                                         
360                                                         // this takes some time, lets. try and dialog it..
361                                                     
362                                                         
363                                                         
364                                                         
365                                                         var msg = new Gtk.MessageDialog( {
366                                                             buttons : Gtk.ButtonsType.NONE,
367                                                             text: "Loading History"
368                                                             
369                                                         });
370                                                         
371                                                         msg.set_transient_for(Merger.el);
372                                                         msg.show_all();
373                                                     
374                                                         var hist = Merger.repo.dayTree('/', false, 'rev', rev);
375                                                         msg.hide();
376                                                         
377                                                         this.load(hist);
378                                                             
379                                                            
380                                                     }
381                                                 },
382                                                 {
383                                                     xtype: Gtk.TreeViewColumn,
384                                                     pack : "append_column",
385                                                     title : "Changes",
386                                                     init : function() {
387                                                         XObject.prototype.init.call(this);
388                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
389                                                     
390                                                     },
391                                                     items : [
392                                                         {
393                                                             xtype: Gtk.CellRendererText,
394                                                             pack : "pack_start"
395                                                         }
396                                                     ]
397                                                 }
398                                             ]
399                                         }
400                                     ]
401                                 },
402                                 {
403                                     xtype: Gtk.ScrolledWindow,
404                                     init : function() {
405                                         XObject.prototype.init.call(this);
406                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
407                                     },
408                                     items : [
409                                         {
410                                             xtype: Gtk.TreeView,
411                                             listeners : {
412                                                 cursor_changed : function (self) {
413                                                   // SEE SELECTION.CHANGED
414                                                     if (this.el.get_selection().count_selected_rows() < 1) {
415                                                         //nothing? - clea it?
416                                                         return;
417                                                     }
418                                                         var ret = {};         
419                                                     var model = this.get('/changedFilesStore');
420                                                 
421                                                      var s = this.el.get_selection();
422                                                      var files = [];
423                                                     s.selected_foreach(function(model,p,iter) {
424                                                     
425                                                        files.push( model.get_value(iter, 0).value.get_string());
426                                                      
427                                                     });
428                                                     this.get('/patchview').showDiff(files); 
429                                                     //var value = ''+ ret.model.get_value(ret.iter, 1).value.get_string();
430                                                      //print("OUT?" + value);// id..
431                                                     // load the list in the right grid..
432                                                      
433                                                     return true;
434                                                 
435                                                 }
436                                             },
437                                             id : "changedFilesView",
438                                             pack : "add",
439                                             files : function() {
440                                                 
441                                             },
442                                             init : function() {
443                                                 XObject.prototype.init.call(this);
444                                                     var description = new Pango.FontDescription.c_new();
445                                                description.set_size(8000);
446                                                  this.el.modify_font(description);
447                                             
448                                                  this.selection = this.el.get_selection();
449                                                   this.selection.set_mode( Gtk.SelectionMode.MULTIPLE);
450                                                 var _this = this;
451                                               this.selection.signal['changed'].connect(function() {
452                                                  _this.listeners.cursor_changed.apply(
453                                                       _this, [ _this, '']
454                                                    );
455                                                          });
456                                               },
457                                             items : [
458                                                 {
459                                                     xtype: Gtk.ListStore,
460                                                     id : "changedFilesStore",
461                                                     pack : "set_model",
462                                                     init : function() {
463                                                         XObject.prototype.init.call(this);
464                                                         this.el.set_column_types ( 3, [
465                                                            GObject.TYPE_STRING, // file  
466                                                           GObject.TYPE_STRING, // added
467                                                           GObject.TYPE_STRING, // removed
468                                                       ] );
469                                                     },
470                                                     load : function(tr)
471                                                     {
472                                                         //this.insert(citer,iter,0);
473                                                         this.el.clear();
474                                                         for(var i =0 ; i < tr.length; i++) {
475                                                             var ret = {  };
476                                                            
477                                                             this.el.append(ret);
478                                                             
479                                                             //print(JSON.stringify(ret,null,4));
480                                                              
481                                                               
482                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].filename );
483                                                             this.el.set_value(ret.iter, 1, '' + tr[i].added  );
484                                                             this.el.set_value(ret.iter, 2, '' + tr[i].removed  );
485                                                      
486                                                             
487                                                         }     
488                                                     }
489                                                 },
490                                                 {
491                                                     xtype: Gtk.TreeViewColumn,
492                                                     resizable : true,
493                                                     min_width : 200,
494                                                     pack : "append_column",
495                                                     title : "Filename",
496                                                     init : function() {
497                                                         XObject.prototype.init.call(this);
498                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
499                                                     
500                                                     },
501                                                     items : [
502                                                         {
503                                                             xtype: Gtk.CellRendererText,
504                                                             pack : "pack_start"
505                                                         }
506                                                     ]
507                                                 },
508                                                 {
509                                                     xtype: Gtk.TreeViewColumn,
510                                                     min_width : 50,
511                                                     pack : "append_column",
512                                                     title : "Added",
513                                                     init : function() {
514                                                         XObject.prototype.init.call(this);
515                                                         this.el.add_attribute(this.items[0].el , 'markup', 1 );
516                                                     
517                                                     },
518                                                     items : [
519                                                         {
520                                                             xtype: Gtk.CellRendererText,
521                                                             pack : "pack_start"
522                                                         }
523                                                     ]
524                                                 },
525                                                 {
526                                                     xtype: Gtk.TreeViewColumn,
527                                                     min_width : 50,
528                                                     pack : "append_column",
529                                                     title : "Removed",
530                                                     init : function() {
531                                                         XObject.prototype.init.call(this);
532                                                         this.el.add_attribute(this.items[0].el , 'markup', 2 );
533                                                     
534                                                     },
535                                                     items : [
536                                                         {
537                                                             xtype: Gtk.CellRendererText,
538                                                             pack : "pack_start"
539                                                         }
540                                                     ]
541                                                 }
542                                             ]
543                                         },
544                                         {
545                                             xtype: Gtk.TreeView,
546                                             pack : false,
547                                             items : [
548                                                 {
549                                                     xtype: Gtk.ListStore,
550                                                     pack : false
551                                                 }
552                                             ]
553                                         }
554                                     ]
555                                 }
556                             ]
557                         },
558                         {
559                             xtype: Gtk.VBox,
560                             items : [
561                                 {
562                                     xtype: Gtk.HBox,
563                                     pack : "pack_start,false,false,0",
564                                     items : [
565                                         {
566                                             xtype: Gtk.Button,
567                                             listeners : {
568                                                 clicked : function (self) {
569                                                     Commit = imports.Commit.Commit;
570                                                     Commit.repo = Merger.repo;
571                                                     Merger.el.set_transient_for(Commit.el);
572                                                 
573                                                     Commit.show({
574                                                         files : []
575                                                     
576                                                     });
577                                                 
578                                                 
579                                                 }
580                                             },
581                                             label : "Commit diff (no merge)"
582                                         },
583                                         {
584                                             xtype: Gtk.Button,
585                                             label : "Commit diff (merge after)"
586                                         }
587                                     ]
588                                 },
589                                 {
590                                     xtype: Gtk.ScrolledWindow,
591                                     pack : "pack_end,true,true,0",
592                                     init : function() {
593                                         XObject.prototype.init.call(this);
594                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
595                                     },
596                                     items : [
597                                         {
598                                             xtype: WebKit.WebView,
599                                             id : "patchview",
600                                             pack : "add",
601                                             clear : function() {
602                                                 
603                                                 
604                                                 
605                                                  
606                                                // remove..
607                                                 var s  = "document.body.textContent='';"
608                                                     
609                                                 this.el.execute_script(s);
610                                                     
611                                                     
612                                                 
613                                                 
614                                                 
615                                             },
616                                             showDiff : function(files) {
617                                                 this.clear();
618                                                 
619                                                 
620                                                  var model = this.get('/historyTreeStore');
621                                                 
622                                                 if (model.release === false) {
623                                                     return;
624                                                 }
625                                                 
626                                                 var diff = Merger.repo.diff(files, model.release, model.rev);
627                                                 
628                                                // remove..
629                                                 var s  = "var pre  = document.createElement('pre'); document.body.appendChild(pre);";
630                                                 s += "pre.textContent =  " +
631                                                          JSON.stringify(Merger.repo.lastCmd + "\n") + '+  ' + 
632                                                        JSON.stringify(diff) + ";";
633                                                 print(s);
634                                                     
635                                                 this.el.execute_script(s);
636                                                     
637                                                     
638                                                 
639                                                 
640                                                 
641                                             }
642                                         }
643                                     ]
644                                 }
645                             ]
646                         }
647                     ]
648                 }
649             ]
650         },
651         {
652             xtype: Gtk.Button,
653             id : "ok_button",
654             label : "Close",
655             pack : "add_action_widget,1"
656         }
657     ]
658 });
659 Merger.init();
660 XObject.cache['/Merger'] = Merger;