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