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',  src.vpath, { 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         
124         this.queue.push( 
125             [ src.gitpath,  'add', src.vpath ],
126             [ src.gitpath,  'commit', src.vpath, {  message: src.vpath} ],
127             [ src.gitpath , 'push', { all: true } ]
128             
129         );
130         if (this.nqv) {
131             var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
132             Git.run(src.gitpath , 'push', '--all' );
133             notify(src.name,"CHANGED", sp);
134         }
135
136     },
137     onDeleted : function(src) 
138     { 
139         this.parsePath(src);
140         if (this.shouldIgnore(src)) {
141             return;
142         }
143         // should check if monitor needs removing..
144         // it should also check if it was a directory.. - so we dont have to commit all..
145         
146         
147         this.queue.push( 
148             [ src.gitpath, 'rm' , src.vpath ],
149             [ src.gitpath, 'commit', { all: true, message: src.vpath} ],
150             [ src.gitpath, 'push', { all: true } ]
151         );
152         if (!this.nqv) {
153             return;
154         }
155         
156         var sp = Git.run(src.gitpath,'rm' , src.vpath);
157         Git.run(src.gitpath , 'push', { all: true } );
158         if (sp.status !=0) {
159             notify(src.name,"DELETED", sp);
160             return;
161         }
162         sp = Git.run(src.gitpath,'commit' ,{ all: true, message: src.vpath});
163         Git.run(src.gitpath , 'push',{ all: true });
164         notify(src.name,"DELETED", sp);
165         return;
166         
167     },
168     onCreated : function(src) 
169     { 
170         this.parsePath(src);
171         if (this.shouldIgnore(src)) {
172             return;
173         }
174         
175         if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
176             this.just_created[src.path] = true;
177             return; // we do not handle file create flags... - use done hint.
178         }
179         // director has bee created
180         this.monitor(src.path);
181         
182         this.queue.push( 
183             [ src.gitpath, 'add' , src.vpath,  { all: true } ],
184             [ src.gitpath, 'commit' , { all: true, message: src.vpath} ],
185             [ src.gitpath, 'push', { all: true } ]
186         );
187         if (!this.nqv) {
188             return;
189         }
190         var sp = Git.run(src.gitpath, 'add', src.vpath);
191         Git.run(src.gitpath , 'push', { all: true } );
192
193         if (sp.status !=0) {
194             notify(src.path,"CREATED", sp);
195             return;
196         }
197         //uh.call(fm,f,of, event_type);
198         sp = Git.run(src.gitpath,'commit' , { all: true, message: src.vpath});
199         Git.run(src.gitpath , 'push', { all: true } );
200         notify(src.path,"CREATED", sp);
201         return;
202         
203     },
204     onAttributeChanged : function(src) { 
205         this.parsePath(src);
206         if (this.shouldIgnore(src)) {
207             return;
208         }
209         this.queue.push( 
210             [ src.gitpath, 'commit' ,  src.vpath, { message: src.vpath} ],
211             [ src.gitpath, 'push', { all: true } ]
212         );
213         if (!this.nqv) {
214             return;
215         }
216         var sp = Git.run(src.gitpath, 'commit',{ all: true, message: src.vpath});
217         Git.run(src.gitpath , 'push', { all: true } );
218         notify(src.path,"ATTRIBUTE_CHANGED", sp);
219         return;
220     
221     },
222     
223     onMoved : function(src,dest)
224     { 
225         this.parsePath(src);
226         this.parsePath(dest);
227         
228         if (src.gitpath != dest.gitpath) {
229             this.onDeleted(src);
230             this.onCreated(dest);
231             this.onChangedDoneHint(dest);
232             return;
233         }
234         // needs to handle move to/from unsupported types..
235         
236         if (this.shouldIgnore(src)) {
237             return;
238         }
239         if (this.shouldIgnore(dest)) {
240             return;
241         }
242         this.queue.push( 
243             [ src.gitpath, 'mv',  '-k', src.vpath, dest.vpath ],
244             [ src.gitpath, 'commit' ,  src.vpath, dest.vpath ,
245                 { message:   'MOVED ' + src.vpath +' to ' + dest.vpath} ],
246             [ src.gitpath, 'push', { all: true } ]
247         );
248         
249         if (!this.nqv) {
250             return;
251         }
252         
253         var sp = Git.run(src.gitpath,  'mv',  '-k', src.vpath, dest.vpath);
254         if (sp.status !=0) {
255             notify(dest.path,"MOVED", sp);
256             return;
257         }
258         sp = Git.run(src.gitpath,'commit' , { all: true, message:   'MOVED ' + src.vpath +' to ' + dest.vpath} );
259         Git.run(src.gitpath , 'push', { all: true } );
260         notify(src.path,"MOVED", sp);
261         
262     }
263           
264     
265 });
266  
267  
268  
269
270 function notify(fn, act , sp)
271 {
272     var sum = act + " " + fn;
273     
274     var notification = new Notify.Notification({
275         summary: sum,
276                 body : sp
277         });
278
279     notification.set_timeout(500);
280     notification.show();
281 }
282
283
284
285   
286
287 function errorDialog(data) {
288     var msg = new Gtk.MessageDialog({
289             message_type: Gtk.MessageType.ERROR, 
290             buttons : Gtk.ButtonsType.OK, 
291             text: data
292     });
293     msg.run();
294     msg.destroy();
295 }
296
297  
298
299
300
301 //
302 // need a better icon...
303
304 StatusIcon.init(); 
305 Notify.init("gitlive");
306 monitor.add(GLib.get_home_dir() + "/gitlive");
307 monitor.start();
308 Gtk.main();
309 //icon.signal["activate"].connect(on_left_click);
310