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