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