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