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
40 var monitor = new Monitor({
41     
42     queue : [],
43     nqv : true, // temp var while I switch to queued version.
44     
45     start: function() {
46         var _this = this;
47         this.lastAdd = new Date();
48         
49         Glib.idle_add(PRIORITY_LOW, function() {
50             if (!_this.queue.length) {
51                 return 0;
52             }
53             Math.floor(((new Date()) - this.lastAdd) / 100)
54             _this.runQueue();
55         },null,null);
56         
57         Monitor.prototype.start.call(this);
58         var notification = new Notify.Notification({
59             summary: "Git Live",
60             body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
61         });
62
63         notification.set_timeout(500);
64         notification.show();   
65         
66     },
67     
68     shouldIgnore: function(f)
69     {
70         if (f.name[0] == '.') {
71             // except!
72             if (f.name == '.htaccess') {
73                 return false;
74             }
75             
76             return true;
77         }
78         if (f.name.match(/~$/)) {
79             return true;
80         }
81         // ignore anything in top level!!!!
82         if (!f.vpath.length) {
83             return true;
84         }
85         
86         return false;
87         
88     },
89     
90     parsePath: function(f) {
91            
92         var vpath_ar = f.path.substring(gitlive.length +1).split('/');
93         
94         f.gitpath = gitlive + '/' + vpath_ar.shift();
95         f.vpath =  vpath_ar.join('/');
96         
97         
98     },
99     
100     just_created : {},
101       
102     onChanged : function(src) 
103     { 
104         return; // always ignore this..?
105         //this.parsePath(src);
106     },
107     onChangesDoneHint : function(src) 
108     { 
109         this.parsePath(src);
110         if (this.shouldIgnore(src)) {
111             return;
112         }
113         
114         var add_it = false;
115         if (typeof(this.just_created[src.path]) !='undefined') {
116             delete this.just_created[src.path];
117             this.lastAdd = new Date();
118             this.queue.push( 
119                 [ src.gitpath,  'add', src.vpath ],
120                 [ src.gitpath,  'commit',  src.vpath, { message: src.vpath} ],
121                 [ src.gitpath , 'push', { all: true } ]
122                 
123             );
124             if (this.nqv) {
125                 
126                 Git.run(src.gitpath, 'add', src.vpath);
127                 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
128                 Git.run(src.gitpath , 'push', { all: true } );
129                 notify(src.name,"CHANGED", sp);
130             }
131             return;
132         }
133         this.lastAdd = new Date();
134         this.queue.push( 
135             [ src.gitpath,  'add', src.vpath ],
136             [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ],
137             [ src.gitpath , 'push', { all: true } ]
138             
139         );
140         if (this.nqv) {
141             var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
142             Git.run(src.gitpath , 'push', '--all' );
143             notify(src.name,"CHANGED", sp);
144         }
145
146     },
147     onDeleted : function(src) 
148     { 
149         this.parsePath(src);
150         if (this.shouldIgnore(src)) {
151             return;
152         }
153         // should check if monitor needs removing..
154         // it should also check if it was a directory.. - so we dont have to commit all..
155         
156         this.lastAdd = new Date();
157         this.queue.push( 
158             [ src.gitpath, 'rm' , src.vpath ],
159             [ src.gitpath, 'commit', { all: true, message: src.vpath} ],
160             [ src.gitpath, 'push', { all: true } ]
161         );
162         if (!this.nqv) {
163             return;
164         }
165         
166         var sp = Git.run(src.gitpath,'rm' , src.vpath);
167         Git.run(src.gitpath , 'push', { all: true } );
168         if (sp.status !=0) {
169             notify(src.name,"DELETED", sp);
170             return;
171         }
172         sp = Git.run(src.gitpath,'commit' ,{ all: true, message: src.vpath});
173         Git.run(src.gitpath , 'push',{ all: true });
174         notify(src.name,"DELETED", sp);
175         return;
176         
177     },
178     onCreated : function(src) 
179     { 
180         this.parsePath(src);
181         if (this.shouldIgnore(src)) {
182             return;
183         }
184         
185         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
186             this.just_created[src.path] = true;
187             return; // we do not handle file create flags... - use done hint.
188         }
189         // director has bee created
190         this.monitor(src.path);
191         this.lastAdd = new Date();
192         this.queue.push( 
193             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
194             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ],
195             [ src.gitpath, 'push', { all: true } ]
196         );
197         if (!this.nqv) {
198             return;
199         }
200         var sp = Git.run(src.gitpath, 'add', src.vpath);
201         Git.run(src.gitpath , 'push', { all: true } );
202
203         if (sp.status !=0) {
204             notify(src.path,"CREATED", sp);
205             return;
206         }
207         //uh.call(fm,f,of, event_type);
208         sp = Git.run(src.gitpath,'commit' , { all: true, message: src.vpath});
209         Git.run(src.gitpath , 'push', { all: true } );
210         notify(src.path,"CREATED", sp);
211         return;
212         
213     },
214     onAttributeChanged : function(src) { 
215         this.parsePath(src);
216         if (this.shouldIgnore(src)) {
217             return;
218         }
219         this.lastAdd = new Date();
220         this.queue.push( 
221             [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ],
222             [ src.gitpath, 'push', { all: true } ]
223         );
224         if (!this.nqv) {
225             return;
226         }
227         var sp = Git.run(src.gitpath, 'commit',{ all: true, message: src.vpath});
228         Git.run(src.gitpath , 'push', { all: true } );
229         notify(src.path,"ATTRIBUTE_CHANGED", sp);
230         return;
231     
232     },
233     
234     onMoved : function(src,dest)
235     { 
236         this.parsePath(src);
237         this.parsePath(dest);
238         
239         if (src.gitpath != dest.gitpath) {
240             this.onDeleted(src);
241             this.onCreated(dest);
242             this.onChangedDoneHint(dest);
243             return;
244         }
245         // needs to handle move to/from unsupported types..
246         
247         if (this.shouldIgnore(src)) {
248             return;
249         }
250         if (this.shouldIgnore(dest)) {
251             return;
252         }
253         this.lastAdd = new Date();
254         this.queue.push( 
255             [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
256             [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
257                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ],
258             [ src.gitpath, 'push', { all: true } ]
259         );
260         
261         if (!this.nqv) {
262             return;
263         }
264         
265         var sp = Git.run(src.gitpath,  'mv',  '-k', src.vpath, dest.vpath);
266         if (sp.status !=0) {
267             notify(dest.path,"MOVED", sp);
268             return;
269         }
270         sp = Git.run(src.gitpath,'commit' , { all: true, message:   'MOVED ' + src.vpath +' to ' + dest.vpath} );
271         Git.run(src.gitpath , 'push', { all: true } );
272         notify(src.path,"MOVED", sp);
273         
274     }
275           
276     
277 });
278  
279  
280  
281
282 function notify(fn, act , sp)
283 {
284     var sum = act + " " + fn;
285     
286     var notification = new Notify.Notification({
287         summary: sum,
288                 body : sp
289         });
290
291     notification.set_timeout(500);
292     notification.show();
293 }
294
295
296
297   
298
299 function errorDialog(data) {
300     var msg = new Gtk.MessageDialog({
301             message_type: Gtk.MessageType.ERROR, 
302             buttons : Gtk.ButtonsType.OK, 
303             text: data
304     });
305     msg.run();
306     msg.destroy();
307 }
308
309  
310
311
312
313 //
314 // need a better icon...
315
316 StatusIcon.init(); 
317 Notify.init("gitlive");
318 monitor.add(GLib.get_home_dir() + "/gitlive");
319 monitor.start();
320 Gtk.main();
321 //icon.signal["activate"].connect(on_left_click);
322