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                                             init : function() {
440                                                 XObject.prototype.init.call(this);
441                                                     var description = new Pango.FontDescription.c_new();
442                                                description.set_size(8000);
443                                                  this.el.modify_font(description);
444                                             
445                                                  this.selection = this.el.get_selection();
446                                                   this.selection.set_mode( Gtk.SelectionMode.MULTIPLE);
447                                                 var _this = this;
448                                               this.selection.signal['changed'].connect(function() {
449                                                  _this.listeners.cursor_changed.apply(
450                                                       _this, [ _this, '']
451                                                    );
452                                                          });
453                                               },
454                                             items : [
455                                                 {
456                                                     xtype: Gtk.ListStore,
457                                                     id : "changedFilesStore",
458                                                     pack : "set_model",
459                                                     init : function() {
460                                                         XObject.prototype.init.call(this);
461                                                         this.el.set_column_types ( 3, [
462                                                            GObject.TYPE_STRING, // file  
463                                                           GObject.TYPE_STRING, // added
464                                                           GObject.TYPE_STRING, // removed
465                                                       ] );
466                                                     },
467                                                     load : function(tr)
468                                                     {
469                                                         //this.insert(citer,iter,0);
470                                                         this.el.clear();
471                                                         for(var i =0 ; i < tr.length; i++) {
472                                                             var ret = {  };
473                                                            
474                                                             this.el.append(ret);
475                                                             
476                                                             //print(JSON.stringify(ret,null,4));
477                                                              
478                                                               
479                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].filename );
480                                                             this.el.set_value(ret.iter, 1, '' + tr[i].added  );
481                                                             this.el.set_value(ret.iter, 2, '' + tr[i].removed  );
482                                                      
483                                                             
484                                                         }     
485                                                     }
486                                                 },
487                                                 {
488                                                     xtype: Gtk.TreeViewColumn,
489                                                     resizable : true,
490                                                     min_width : 200,
491                                                     pack : "append_column",
492                                                     title : "Filename",
493                                                     init : function() {
494                                                         XObject.prototype.init.call(this);
495                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
496                                                     
497                                                     },
498                                                     items : [
499                                                         {
500                                                             xtype: Gtk.CellRendererText,
501                                                             pack : "pack_start"
502                                                         }
503                                                     ]
504                                                 },
505                                                 {
506                                                     xtype: Gtk.TreeViewColumn,
507                                                     min_width : 50,
508                                                     pack : "append_column",
509                                                     title : "Added",
510                                                     init : function() {
511                                                         XObject.prototype.init.call(this);
512                                                         this.el.add_attribute(this.items[0].el , 'markup', 1 );
513                                                     
514                                                     },
515                                                     items : [
516                                                         {
517                                                             xtype: Gtk.CellRendererText,
518                                                             pack : "pack_start"
519                                                         }
520                                                     ]
521                                                 },
522                                                 {
523                                                     xtype: Gtk.TreeViewColumn,
524                                                     min_width : 50,
525                                                     pack : "append_column",
526                                                     title : "Removed",
527                                                     init : function() {
528                                                         XObject.prototype.init.call(this);
529                                                         this.el.add_attribute(this.items[0].el , 'markup', 2 );
530                                                     
531                                                     },
532                                                     items : [
533                                                         {
534                                                             xtype: Gtk.CellRendererText,
535                                                             pack : "pack_start"
536                                                         }
537                                                     ]
538                                                 }
539                                             ]
540                                         },
541                                         {
542                                             xtype: Gtk.TreeView,
543                                             pack : false,
544                                             items : [
545                                                 {
546                                                     xtype: Gtk.ListStore,
547                                                     pack : false
548                                                 }
549                                             ]
550                                         }
551                                     ]
552                                 }
553                             ]
554                         },
555                         {
556                             xtype: Gtk.VBox,
557                             items : [
558                                 {
559                                     xtype: Gtk.HBox,
560                                     pack : "pack_start,false,false,0",
561                                     items : [
562                                         {
563                                             xtype: Gtk.Button,
564                                             label : "Commit diff (no merge)"
565                                         },
566                                         {
567                                             xtype: Gtk.Button,
568                                             label : "Commit diff (merge after)"
569                                         }
570                                     ]
571                                 },
572                                 {
573                                     xtype: Gtk.ScrolledWindow,
574                                     pack : "pack_end,true,true,0",
575                                     init : function() {
576                                         XObject.prototype.init.call(this);
577                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
578                                     },
579                                     items : [
580                                         {
581                                             xtype: WebKit.WebView,
582                                             id : "patchview",
583                                             pack : "add",
584                                             clear : function() {
585                                                 
586                                                 
587                                                 
588                                                  
589                                                // remove..
590                                                 var s  = "document.body.textContent='';"
591                                                     
592                                                 this.el.execute_script(s);
593                                                     
594                                                     
595                                                 
596                                                 
597                                                 
598                                             },
599                                             showDiff : function(files) {
600                                                 this.clear();
601                                                 
602                                                 
603                                                  var model = this.get('/historyTreeStore');
604                                                 
605                                                 if (model.release === false) {
606                                                     return;
607                                                 }
608                                                 
609                                                 var diff = Merger.repo.diff(files, model.release, model.rev);
610                                                 
611                                                // remove..
612                                                 var s  = "var pre  = document.createElement('pre'); document.body.appendChild(pre);";
613                                                 s += "pre.textContent =  " +
614                                                          JSON.stringify(Merger.repo.lastCmd + "\n") + '+  ' + 
615                                                        JSON.stringify(diff) + ";";
616                                                 print(s);
617                                                     
618                                                 this.el.execute_script(s);
619                                                     
620                                                     
621                                                 
622                                                 
623                                                 
624                                             }
625                                         }
626                                     ]
627                                 }
628                             ]
629                         }
630                     ]
631                 }
632             ]
633         },
634         {
635             xtype: Gtk.Button,
636             id : "ok_button",
637             label : "Close",
638             pack : "add_action_widget,1"
639         }
640     ]
641 });
642 Merger.init();
643 XObject.cache['/Merger'] = Merger;