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