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