changes for gtk3
[gitlive] / gitlive.js
1 #!/usr/bin/seed
2 ///<script type="text/javascript">
3 /**
4 * Git Live
5
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
9
10
11 */
12
13
14 GI = imports.gi.GIRepository;
15 GLib        = imports.gi.GLib;
16
17 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
18 //#GI.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
19
20
21 var Gio      = imports.gi.Gio;
22 var Gtk      = imports.gi.Gtk;
23 var Notify = imports.gi.Notify;
24
25 var Spawn = imports.Spawn;
26 var Git = imports.Git;
27 var StatusIcon = imports.StatusIcon.StatusIcon;
28 var Monitor = imports.Monitor.Monitor;
29
30
31 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
32 Gtk.init (null, null);
33
34 var gitlive = GLib.get_home_dir() + "/gitlive";
35
36 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
37     var msg = new Gtk.MessageDialog({message_type:
38         Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
39     msg.run();
40     msg.destroy();
41     
42     Seed.quit();
43 }
44
45  
46 var monitor = new Monitor({
47     
48     queue : [],
49     queueRunning : false,
50      
51     start: function() {
52         var _this = this;
53         this.lastAdd = new Date();
54          
55         GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
56             //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
57             if (!_this.queue.length || _this.queueRunning) {
58                 return 1;
59             }
60             var last = Math.floor(((new Date()) - this.lastAdd) / 100);
61             if (last < 4) { // wait 1/2 a seconnd before running.
62                 return 1;
63             }
64             _this.runQueue();
65             return 1;
66         },null,null);
67         
68         Monitor.prototype.start.call(this);
69         var notification = new Notify.Notification({
70             summary: "Git Live",
71             body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
72         });
73
74         notification.set_timeout(2000);
75         notification.show();   
76     },
77     /**
78      * run the queue.
79      * - pulls the items off the queue 
80      *    (as commands run concurrently and new items may get added while it's running)
81      * - runs the queue items
82      * - pushes upstream.
83      * 
84      */
85     runQueue: function()
86     {
87         this.queueRunning = true;
88         var cmds = [];
89         this.queue.forEach(function (q) {
90             cmds.push(q);
91         });
92         this.queue = []; // empty queue!
93         
94         var success = [];
95         var failure = [];
96         var repos = [];
97         var done = [];
98         cmds.forEach(function(cmd) {
99             // prevent duplicate calls..
100             if (done.indexOf(cmd.join(',')) > -1) {
101                 return;
102             }
103             done.push(cmd.join(','));
104             
105             if (repos.indexOf(cmd[0]) < 0) {
106                 repos.push(cmd[0]);
107                 Git.run(cmd[0] , 'pull'); // pull before we push!
108             }
109             var sp = Git.run.apply(Git,cmd);
110              
111             switch (sp.result * 1) {
112                 case 0: // success:
113                     success.push(sp.args.join(' '));
114                     if (sp.output.length) success.push(sp.output + '');
115                   // if (sp.stderr.length) success.push(sp.stderr + '');
116                     break;
117                 default: 
118                     failure.push(sp.args.join(' '));
119                     if (sp.output.length) failure.push(sp.output);
120                     if (sp.stderr.length) failure.push(sp.stderr);
121                     break;
122             }
123             
124         });
125          
126         // push upstream.
127         repos.forEach(function(r) {
128             var sp = Git.run(r , 'push', { all: true } );
129             if (sp.length) {
130                 success.push(sp);
131             }
132             
133         });
134         
135         if (success.length) {
136             print(success.join("\n"));
137             var notification = new Notify.Notification({
138                 summary: "Git Live Commited",
139                 body : success.join("\n")
140                 
141             });
142
143             notification.set_timeout(2000);
144             notification.show();   
145         }
146         if (failure.length) {
147         
148             var notification = new Notify.Notification({
149                 summary: "Git Live ERROR!!",
150                 body : failure.join("\n")
151                 
152             });
153
154             notification.set_timeout(5000); // show errros for longer
155             notification.show();   
156         }
157         this.queueRunning = false;
158     },
159     
160     shouldIgnore: function(f)
161     {
162         if (f.name[0] == '.') {
163             // except!
164             if (f.name == '.htaccess') {
165                 return false;
166             }
167             
168             return true;
169         }
170         if (f.name.match(/~$/)) {
171             return true;
172         }
173         // ignore anything in top level!!!!
174         if (!f.vpath.length) {
175             return true;
176         }
177         
178         return false;
179         
180     },
181     
182     parsePath: function(f) {
183            
184         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
185         
186         f.gitpath = gitlive + '/' + vpath_ar.shift();
187         f.vpath =  vpath_ar.join('/');
188         
189         
190     },
191     
192     just_created : {},
193       
194     onChanged : function(src) 
195     { 
196         return; // always ignore this..?
197         //this.parsePath(src);
198     },
199     onChangesDoneHint : function(src) 
200     { 
201         this.parsePath(src);
202         if (this.shouldIgnore(src)) {
203             return;
204         }
205         
206         var add_it = false;
207         if (typeof(this.just_created[src.path]) !='undefined') {
208             delete this.just_created[src.path];
209             this.lastAdd = new Date();
210             this.queue.push( 
211                 [ src.gitpath,  'add', src.vpath ],
212                 [ src.gitpath,  'commit',  src.vpath, { message: src.vpath} ] 
213                 
214             );
215          
216             return;
217         }
218         this.lastAdd = new Date();
219         this.queue.push( 
220             [ src.gitpath,  'add', src.vpath ],
221             [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ]
222
223             
224         );
225        
226
227     },
228     onDeleted : function(src) 
229     { 
230         this.parsePath(src);
231         if (this.shouldIgnore(src)) {
232             return;
233         }
234         // should check if monitor needs removing..
235         // it should also check if it was a directory.. - so we dont have to commit all..
236         
237         this.lastAdd = new Date();
238         this.queue.push( 
239             [ src.gitpath, 'rm' , src.vpath ],
240             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
241             
242         );
243     
244         
245     },
246     onCreated : function(src) 
247     { 
248         this.parsePath(src);
249         if (this.shouldIgnore(src)) {
250             return;
251         }
252         
253         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
254             this.just_created[src.path] = true;
255             return; // we do not handle file create flags... - use done hint.
256         }
257         // director has bee created
258         this.monitor(src.path);
259         this.lastAdd = new Date();
260         this.queue.push( 
261             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
262             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
263             
264         );
265         
266         
267     },
268     onAttributeChanged : function(src) { 
269         this.parsePath(src);
270         if (this.shouldIgnore(src)) {
271             return;
272         }
273         this.lastAdd = new Date();
274         this.queue.push( 
275             [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
276         );
277  
278     
279     },
280     
281     onMoved : function(src,dest)
282     { 
283         this.parsePath(src);
284         this.parsePath(dest);
285         
286         if (src.gitpath != dest.gitpath) {
287             this.onDeleted(src);
288             this.onCreated(dest);
289             this.onChangedDoneHint(dest);
290             return;
291         }
292         // needs to handle move to/from unsupported types..
293         
294         if (this.shouldIgnore(src)) {
295             return;
296         }
297         if (this.shouldIgnore(dest)) {
298             return;
299         }
300         this.lastAdd = new Date();
301         this.queue.push( 
302             [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
303             [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
304                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ]
305         );
306          
307     }
308           
309     
310 });
311  
312  
313   
314
315 function errorDialog(data) {
316     var msg = new Gtk.MessageDialog({
317             message_type: Gtk.MessageType.ERROR, 
318             buttons : Gtk.ButtonsType.OK, 
319             text: data
320     });
321     msg.run();
322     msg.destroy();
323 }
324
325  
326
327
328
329 //
330 // need a better icon...
331
332
333 StatusIcon.init();   
334
335
336 Notify.init("gitlive");
337
338 monitor.add(GLib.get_home_dir() + "/gitlive");
339 monitor.start();
340 Gtk.main();
341 //icon.signal["activate"].connect(on_left_click);
342