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