gitlive.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 GI      = imports.gi.GIRepository;
13 Gio      = imports.gi.Gio;
14 GLib      = imports.gi.GLib;
15 Gtk      = imports.gi.Gtk;
16 Notify = imports.gi.Notify;
17
18 Spawn = imports.Spawn;
19 Git = imports.Git;
20 StatusIcon = imports.StatusIcon.StatusIcon;
21 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         cmds.forEach(function(cmd) {
91             if (repos.indexOf(cmd[0]) < 0) {
92                 repos.push(cmd[0]);
93                 Git.run(cmd[0] , 'pull'); // pull before we push!
94             }
95             var sp = Git.run.apply(Git,cmd);
96              
97             switch (sp.result * 1) {
98                 case 0: // success:
99                     success.push(sp.args.join(' '));
100                     if (sp.output.length) success.push(sp.output + '');
101                   // if (sp.stderr.length) success.push(sp.stderr + '');
102                     break;
103                 default: 
104                     failure.push(sp.args.join(' '));
105                     if (sp.output.length) failure.push(sp.output);
106                     if (sp.stderr.length) failure.push(sp.stderr);
107                     break;
108             }
109             
110         });
111          
112         // push upstream.
113         repos.forEach(function(r) {
114             var sp = Git.run(r , 'push', { all: true } );
115             if (sp.length) {
116                 success.push(sp);
117             }
118             
119         });
120         
121         if (success.length) {
122             print(success.join("\n"));
123             var notification = new Notify.Notification({
124                 summary: "Git Live Commited",
125                 body : success.join("\n")
126                 
127             });
128
129             notification.set_timeout(2000);
130             notification.show();   
131         }
132         if (failure.length) {
133         
134             var notification = new Notify.Notification({
135                 summary: "Git Live ERROR!!",
136                 body : failure.join("\n")
137                 
138             });
139
140             notification.set_timeout(5000); // show errros for longer
141             notification.show();   
142         }
143         this.queueRunning = false;
144     },
145     
146     shouldIgnore: function(f)
147     {
148         if (f.name[0] == '.') {
149             // except!
150             if (f.name == '.htaccess') {
151                 return false;
152             }
153             
154             return true;
155         }
156         if (f.name.match(/~$/)) {
157             return true;
158         }
159         // ignore anything in top level!!!!
160         if (!f.vpath.length) {
161             return true;
162         }
163         
164         return false;
165         
166     },
167     
168     parsePath: function(f) {
169            
170         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
171         
172         f.gitpath = gitlive + '/' + vpath_ar.shift();
173         f.vpath =  vpath_ar.join('/');
174         
175         
176     },
177     
178     just_created : {},
179       
180     onChanged : function(src) 
181     { 
182         return; // always ignore this..?
183         //this.parsePath(src);
184     },
185     onChangesDoneHint : function(src) 
186     { 
187         this.parsePath(src);
188         if (this.shouldIgnore(src)) {
189             return;
190         }
191         
192         var add_it = false;
193         if (typeof(this.just_created[src.path]) !='undefined') {
194             delete this.just_created[src.path];
195             this.lastAdd = new Date();
196             this.queue.push( 
197                 [ src.gitpath,  'add', src.vpath ],
198                 [ src.gitpath,  'commit',  src.vpath, { message: src.vpath} ] 
199                 
200             );
201          
202             return;
203         }
204         this.lastAdd = new Date();
205         this.queue.push( 
206             [ src.gitpath,  'add', src.vpath ],
207             [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ]
208
209             
210         );
211        
212
213     },
214     onDeleted : function(src) 
215     { 
216         this.parsePath(src);
217         if (this.shouldIgnore(src)) {
218             return;
219         }
220         // should check if monitor needs removing..
221         // it should also check if it was a directory.. - so we dont have to commit all..
222         
223         this.lastAdd = new Date();
224         this.queue.push( 
225             [ src.gitpath, 'rm' , src.vpath ],
226             [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
227             
228         );
229     
230         
231     },
232     onCreated : function(src) 
233     { 
234         this.parsePath(src);
235         if (this.shouldIgnore(src)) {
236             return;
237         }
238         
239         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
240             this.just_created[src.path] = true;
241             return; // we do not handle file create flags... - use done hint.
242         }
243         // director has bee created
244         this.monitor(src.path);
245         this.lastAdd = new Date();
246         this.queue.push( 
247             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
248             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
249             
250         );
251         
252         
253     },
254     onAttributeChanged : function(src) { 
255         this.parsePath(src);
256         if (this.shouldIgnore(src)) {
257             return;
258         }
259         this.lastAdd = new Date();
260         this.queue.push( 
261             [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ]
262         );
263  
264     
265     },
266     
267     onMoved : function(src,dest)
268     { 
269         this.parsePath(src);
270         this.parsePath(dest);
271         
272         if (src.gitpath != dest.gitpath) {
273             this.onDeleted(src);
274             this.onCreated(dest);
275             this.onChangedDoneHint(dest);
276             return;
277         }
278         // needs to handle move to/from unsupported types..
279         
280         if (this.shouldIgnore(src)) {
281             return;
282         }
283         if (this.shouldIgnore(dest)) {
284             return;
285         }
286         this.lastAdd = new Date();
287         this.queue.push( 
288             [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
289             [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
290                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ]
291         );
292          
293     }
294           
295     
296 });
297  
298  
299   
300
301 function errorDialog(data) {
302     var msg = new Gtk.MessageDialog({
303             message_type: Gtk.MessageType.ERROR, 
304             buttons : Gtk.ButtonsType.OK, 
305             text: data
306     });
307     msg.run();
308     msg.destroy();
309 }
310
311  
312
313
314
315 //
316 // need a better icon...
317
318 StatusIcon.init(); 
319 Notify.init("gitlive");
320 monitor.add(GLib.get_home_dir() + "/gitlive");
321 monitor.start();
322 Gtk.main();
323 //icon.signal["activate"].connect(on_left_click);
324