2 ///<script type="text/javascript">
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
12 GI = imports.gi.GIRepository;
14 GLib = imports.gi.GLib;
16 Notify = imports.gi.Notify;
18 Spawn = imports.Spawn;
20 StatusIcon = imports.StatusIcon.StatusIcon;
21 Monitor = imports.Monitor.Monitor;
24 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
25 Gtk.init (null, null);
27 var gitlive = GLib.get_home_dir() + "/gitlive";
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."});
39 var monitor = new Monitor({
43 nqv : false, // temp var while I switch to queued version.
47 this.lastAdd = new Date();
49 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
50 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
51 if (!_this.queue.length || _this.queueRunning) {
54 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
55 if (last < 4) { // wait 1/2 a seconnd before running.
62 Monitor.prototype.start.call(this);
63 var notification = new Notify.Notification({
65 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
68 notification.set_timeout(2000);
73 * - pulls the items off the queue
74 * (as commands run concurrently and new items may get added while it's running)
75 * - runs the queue items
81 this.queueRunning = true;
83 this.queue.forEach(function (q) {
86 this.queue = []; // empty queue!
91 cmds.forEach(function(cmd) {
92 if (repos.indexOf(cmd[0]) < 0) {
95 var sp = Git.run.apply(Git,cmd);
97 switch (sp.result * 1) {
99 success.push(sp.args.join(' '));
100 if (sp.output.length) success.push(sp.output + '');
101 // if (sp.stderr.length) success.push(sp.stderr + '');
104 failure.push(sp.args.join(' '));
105 if (sp.output.length) failure.push(sp.output);
106 if (sp.stderr.length) failure.push(sp.stderr);
113 repos.forEach(function(r) {
114 var sp = Git.run(r , 'push', { all: true } );
121 if (success.length) {
122 print(success.join("\n"));
123 var notification = new Notify.Notification({
124 summary: "Git Live Commited",
125 body : success.join("\n")
129 notification.set_timeout(2000);
132 if (failure.length) {
134 var notification = new Notify.Notification({
135 summary: "Git Live ERROR!!",
136 body : failure.join("\n")
140 notification.set_timeout(2000);
143 this.queueRunning = false;
146 shouldIgnore: function(f)
148 if (f.name[0] == '.') {
150 if (f.name == '.htaccess') {
156 if (f.name.match(/~$/)) {
159 // ignore anything in top level!!!!
160 if (!f.vpath.length) {
168 parsePath: function(f) {
170 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
172 f.gitpath = gitlive + '/' + vpath_ar.shift();
173 f.vpath = vpath_ar.join('/');
180 onChanged : function(src)
182 return; // always ignore this..?
183 //this.parsePath(src);
185 onChangesDoneHint : function(src)
188 if (this.shouldIgnore(src)) {
193 if (typeof(this.just_created[src.path]) !='undefined') {
194 delete this.just_created[src.path];
195 this.lastAdd = new Date();
197 [ src.gitpath, 'add', src.vpath ],
198 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
203 Git.run(src.gitpath, 'add', src.vpath);
204 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
205 Git.run(src.gitpath , 'push', { all: true } );
206 notify(src.name,"CHANGED", sp);
210 this.lastAdd = new Date();
212 [ src.gitpath, 'add', src.vpath ],
213 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
218 var sp = Git.run(src.gitpath, 'commit', { all: true, message: src.vpath});
219 Git.run(src.gitpath , 'push', '--all' );
220 notify(src.name,"CHANGED", sp);
224 onDeleted : function(src)
227 if (this.shouldIgnore(src)) {
230 // should check if monitor needs removing..
231 // it should also check if it was a directory.. - so we dont have to commit all..
233 this.lastAdd = new Date();
235 [ src.gitpath, 'rm' , src.vpath ],
236 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
243 var sp = Git.run(src.gitpath,'rm' , src.vpath);
244 Git.run(src.gitpath , 'push', { all: true } );
246 notify(src.name,"DELETED", sp);
249 sp = Git.run(src.gitpath,'commit' ,{ all: true, message: src.vpath});
250 Git.run(src.gitpath , 'push',{ all: true });
251 notify(src.name,"DELETED", sp);
255 onCreated : function(src)
258 if (this.shouldIgnore(src)) {
262 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
263 this.just_created[src.path] = true;
264 return; // we do not handle file create flags... - use done hint.
266 // director has bee created
267 this.monitor(src.path);
268 this.lastAdd = new Date();
270 [ src.gitpath, 'add' , src.vpath, { all: true } ],
271 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
277 var sp = Git.run(src.gitpath, 'add', src.vpath);
278 Git.run(src.gitpath , 'push', { all: true } );
281 notify(src.path,"CREATED", sp);
284 //uh.call(fm,f,of, event_type);
285 sp = Git.run(src.gitpath,'commit' , { all: true, message: src.vpath});
286 Git.run(src.gitpath , 'push', { all: true } );
287 notify(src.path,"CREATED", sp);
291 onAttributeChanged : function(src) {
293 if (this.shouldIgnore(src)) {
296 this.lastAdd = new Date();
298 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
303 var sp = Git.run(src.gitpath, 'commit',{ all: true, message: src.vpath});
304 Git.run(src.gitpath , 'push', { all: true } );
305 notify(src.path,"ATTRIBUTE_CHANGED", sp);
310 onMoved : function(src,dest)
313 this.parsePath(dest);
315 if (src.gitpath != dest.gitpath) {
317 this.onCreated(dest);
318 this.onChangedDoneHint(dest);
321 // needs to handle move to/from unsupported types..
323 if (this.shouldIgnore(src)) {
326 if (this.shouldIgnore(dest)) {
329 this.lastAdd = new Date();
331 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
332 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
333 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
340 var sp = Git.run(src.gitpath, 'mv', '-k', src.vpath, dest.vpath);
342 notify(dest.path,"MOVED", sp);
345 sp = Git.run(src.gitpath,'commit' , { all: true, message: 'MOVED ' + src.vpath +' to ' + dest.vpath} );
346 Git.run(src.gitpath , 'push', { all: true } );
347 notify(src.path,"MOVED", sp);
355 function notify(fn, act , sp)
357 var sum = act + " " + fn;
359 var notification = new Notify.Notification({
364 notification.set_timeout(2000);
372 function errorDialog(data) {
373 var msg = new Gtk.MessageDialog({
374 message_type: Gtk.MessageType.ERROR,
375 buttons : Gtk.ButtonsType.OK,
387 // need a better icon...
390 Notify.init("gitlive");
391 monitor.add(GLib.get_home_dir() + "/gitlive");
394 //icon.signal["activate"].connect(on_left_click);