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