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                                                     init : function() {
312                                                         XObject.prototype.init.call(this);
313                                                         this.el.set_column_types ( 3, [
314                                                            GObject.TYPE_STRING, // title 
315                                                           GObject.TYPE_STRING, // rev 
316                                                       ] );
317                                                     },
318                                                     load : function(tr,iter)
319                                                     {
320                                                         //this.insert(citer,iter,0);
321                                                         if (!iter) {
322                                                             this.el.clear();
323                                                         }
324                                                         
325                                                         for(var i =0 ; i < tr.length; i++) {
326                                                             var ret = {  };
327                                                             if (iter) {
328                                                                 this.el.insert(ret ,iter,-1);
329                                                             } else {
330                                                                 this.el.append(ret);
331                                                             }
332                                                             //print(JSON.stringify(ret,null,4));
333                                                              
334                                                               
335                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].text );
336                                                             this.el.set_value(ret.iter, 1, '' + tr[i].rev  );
337                                                      
338                                                             if (tr[i].children && tr[i].children.length) {
339                                                                 this.load(tr[i].children, ret.iter);
340                                                             }
341                                                         }     
342                                                     },
343                                                     loadTree : function() {
344                                                     
345                                                            this.working = false;
346                                                        
347                                                        var wid = this.get('workingCombo').el.get_active();
348                                                        var rid = this.get('releaseCombo').el.get_active();
349                                                        if (wid < 0 || rid < 0 || rid == wid) {
350                                                         return;
351                                                        }
352                                                        
353                                                        var w = Merger.repo.branches[wid];
354                                                        var r = Merger.repo.branches[rid];
355                                                        
356                                                     
357                                                     
358                                                         var rev = r.name + '..' + w.name;
359                                                         this.working = r.name;
360                                                     
361                                                         var hist = Merger.repo.dayTree('/', false, 'rev', rev);
362                                                         this.load(hist);
363                                                             
364                                                            
365                                                     }
366                                                 },
367                                                 {
368                                                     xtype: Gtk.TreeViewColumn,
369                                                     pack : "append_column",
370                                                     init : function() {
371                                                         XObject.prototype.init.call(this);
372                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
373                                                     
374                                                     },
375                                                     items : [
376                                                         {
377                                                             xtype: Gtk.CellRendererText,
378                                                             pack : "pack_start"
379                                                         }
380                                                     ]
381                                                 }
382                                             ]
383                                         }
384                                     ]
385                                 },
386                                 {
387                                     xtype: Gtk.ScrolledWindow,
388                                     init : function() {
389                                         XObject.prototype.init.call(this);
390                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
391                                     },
392                                     items : [
393                                         {
394                                             xtype: Gtk.TreeView,
395                                             listeners : {
396                                                 cursor_changed : function (self) {
397                                                   // SEE SELECTION.CHANGED
398                                                     if (this.el.get_selection().count_selected_rows() < 1) {
399                                                         //nothing? - clea it?
400                                                         return;
401                                                     }
402                                                         var ret = {};         
403                                                     var model = this.get('/changedFilesStore');
404                                                 
405                                                      var s = this.el.get_selection();
406                                                      var files = [];
407                                                     s.selected_foreach(function(model,p,iter) {
408                                                     
409                                                        files.push( model.get_value(iter, 0).value.get_string());
410                                                      
411                                                     });
412                                                     this.get('/patchview').showDiff(files); 
413                                                     //var value = ''+ ret.model.get_value(ret.iter, 1).value.get_string();
414                                                      //print("OUT?" + value);// id..
415                                                     // load the list in the right grid..
416                                                      
417                                                     return true;
418                                                 
419                                                 }
420                                             },
421                                             id : "changedFilesView",
422                                             pack : "add",
423                                             init : function() {
424                                                 XObject.prototype.init.call(this);
425                                                     var description = new Pango.FontDescription.c_new();
426                                                description.set_size(8000);
427                                                  this.el.modify_font(description);
428                                             
429                                                  this.selection = this.el.get_selection();
430                                                   this.selection.set_mode( Gtk.SelectionMode.MULTIPLE);
431                                                 var _this = this;
432                                               this.selection.signal['changed'].connect(function() {
433                                                  _this.listeners.cursor_changed.apply(
434                                                       _this, [ _this, '']
435                                                    );
436                                                          });
437                                               },
438                                             items : [
439                                                 {
440                                                     xtype: Gtk.ListStore,
441                                                     id : "changedFilesStore",
442                                                     pack : "set_model",
443                                                     init : function() {
444                                                         XObject.prototype.init.call(this);
445                                                         this.el.set_column_types ( 3, [
446                                                            GObject.TYPE_STRING, // file  
447                                                           GObject.TYPE_STRING, // added
448                                                           GObject.TYPE_STRING, // removed
449                                                       ] );
450                                                     },
451                                                     load : function(tr)
452                                                     {
453                                                         //this.insert(citer,iter,0);
454                                                         this.el.clear();
455                                                         for(var i =0 ; i < tr.length; i++) {
456                                                             var ret = {  };
457                                                            
458                                                             this.el.append(ret);
459                                                             
460                                                             //print(JSON.stringify(ret,null,4));
461                                                              
462                                                               
463                                                             this.el.set_value(ret.iter, 0, ''  +  tr[i].filename );
464                                                             this.el.set_value(ret.iter, 1, '' + tr[i].added  );
465                                                             this.el.set_value(ret.iter, 2, '' + tr[i].removed  );
466                                                      
467                                                             
468                                                         }     
469                                                     }
470                                                 },
471                                                 {
472                                                     xtype: Gtk.TreeViewColumn,
473                                                     resizable : true,
474                                                     min_width : 200,
475                                                     pack : "append_column",
476                                                     title : "Filename",
477                                                     init : function() {
478                                                         XObject.prototype.init.call(this);
479                                                         this.el.add_attribute(this.items[0].el , 'markup', 0 );
480                                                     
481                                                     },
482                                                     items : [
483                                                         {
484                                                             xtype: Gtk.CellRendererText,
485                                                             pack : "pack_start"
486                                                         }
487                                                     ]
488                                                 },
489                                                 {
490                                                     xtype: Gtk.TreeViewColumn,
491                                                     min_width : 50,
492                                                     pack : "append_column",
493                                                     title : "Added",
494                                                     init : function() {
495                                                         XObject.prototype.init.call(this);
496                                                         this.el.add_attribute(this.items[0].el , 'markup', 1 );
497                                                     
498                                                     },
499                                                     items : [
500                                                         {
501                                                             xtype: Gtk.CellRendererText,
502                                                             pack : "pack_start"
503                                                         }
504                                                     ]
505                                                 },
506                                                 {
507                                                     xtype: Gtk.TreeViewColumn,
508                                                     min_width : 50,
509                                                     pack : "append_column",
510                                                     title : "Removed",
511                                                     init : function() {
512                                                         XObject.prototype.init.call(this);
513                                                         this.el.add_attribute(this.items[0].el , 'markup', 2 );
514                                                     
515                                                     },
516                                                     items : [
517                                                         {
518                                                             xtype: Gtk.CellRendererText,
519                                                             pack : "pack_start"
520                                                         }
521                                                     ]
522                                                 }
523                                             ]
524                                         },
525                                         {
526                                             xtype: Gtk.TreeView,
527                                             pack : false,
528                                             items : [
529                                                 {
530                                                     xtype: Gtk.ListStore,
531                                                     pack : false
532                                                 }
533                                             ]
534                                         }
535                                     ]
536                                 }
537                             ]
538                         },
539                         {
540                             xtype: Gtk.VBox,
541                             items : [
542                                 {
543                                     xtype: Gtk.HBox,
544                                     pack : "pack_start,false,false,0",
545                                     items : [
546                                         {
547                                             xtype: Gtk.Button,
548                                             label : "Commit diff (no merge)"
549                                         },
550                                         {
551                                             xtype: Gtk.Button,
552                                             label : "Commit diff (merge after)"
553                                         }
554                                     ]
555                                 },
556                                 {
557                                     xtype: Gtk.ScrolledWindow,
558                                     pack : "pack_end,true,true,0",
559                                     init : function() {
560                                         XObject.prototype.init.call(this);
561                                           this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
562                                     },
563                                     items : [
564                                         {
565                                             xtype: WebKit.WebView,
566                                             id : "patchview",
567                                             pack : "add",
568                                             showDiff : function(files) {
569                                                 
570                                              
571                                                 var diff = Merger.repo.diff(files, 'github', 'master');
572                                                 
573                                                // remove..
574                                                 var s  = "document.body.textContent='';var pre  = document.createElement('pre'); document.body.appendChild(pre);";
575                                                 s += "pre.textContent =  " +
576                                                          JSON.stringify(repo.lastCmd + "\n") + '+  ' + 
577                                                        JSON.stringify(diff) + ";";
578                                                 print(s);
579                                                     
580                                                 this.el.execute_script(s);
581                                                     
582                                                     
583                                                 
584                                                 
585                                                 
586                                             }
587                                         }
588                                     ]
589                                 }
590                             ]
591                         }
592                     ]
593                 }
594             ]
595         },
596         {
597             xtype: Gtk.Button,
598             label : "Cancel",
599             pack : "add_action_widget,0"
600         },
601         {
602             xtype: Gtk.Button,
603             id : "ok_button",
604             label : "OK",
605             pack : "add_action_widget,1"
606         }
607     ]
608 });
609 Merger.init();
610 XObject.cache['/Merger'] = Merger;