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!?
13 GI = imports.gi.GIRepository
14 GLib = imports.gi.GLib;
16 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
17 GI.IRepository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
21 Notify = imports.gi.Notify;
23 Spawn = imports.Spawn;
25 StatusIcon = imports.StatusIcon.StatusIcon;
26 Monitor = imports.Monitor.Monitor;
29 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
30 Gtk.init (null, null);
32 var gitlive = GLib.get_home_dir() + "/gitlive";
34 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
35 var msg = new Gtk.MessageDialog({message_type:
36 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
44 var monitor = new Monitor({
48 * action: 'add' | rm | update
65 this.lastAdd = new Date();
67 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
68 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
69 if (!_this.queue.length || _this.queueRunning) {
72 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
73 if (last < 4) { // wait 1/2 a seconnd before running.
80 Monitor.prototype.start.call(this);
81 var notification = new Notify.Notification({
83 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
86 notification.set_timeout(2000);
91 * - pulls the items off the queue
92 * (as commands run concurrently and new items may get added while it's running)
93 * - runs the queue items
99 this.queueRunning = true;
101 //this.queue.forEach(function (q) {
105 this.action_queue.forEach(function (q) {
108 //this.queue = []; // empty queue!
109 this.action_queue = [];
114 cmds.forEach(function(cmd) {
115 // prevent duplicate calls..
116 if (done.indexOf(JSON.stringify(cmd)) > -1) {
119 done.push(JSON.stringify(cmd));
120 // --- what does this do?????
122 //if (repos.indexOf(cmd[0]) < 0) {
123 // repos.push(cmd[0]);
124 // Git.run(cmd[0] , 'pull'); // pull before we push!
127 switch( cmd.action ) {
129 Git.run(gitlive + '/' + cmd.repo, 'add', cmd.file );
143 var sp = Git.run.apply(Git,cmd);
145 switch (sp.result * 1) {
147 success.push(sp.args.join(' '));
148 if (sp.output.length) success.push(sp.output + '');
149 // if (sp.stderr.length) success.push(sp.stderr + '');
152 failure.push(sp.args.join(' '));
153 if (sp.output.length) failure.push(sp.output);
154 if (sp.stderr.length) failure.push(sp.stderr);
161 repos.forEach(function(r) {
162 var sp = Git.run(r , 'push', { all: true } );
169 if (success.length) {
170 print(success.join("\n"));
171 var notification = new Notify.Notification({
172 summary: "Git Live Commited",
173 body : success.join("\n")
177 notification.set_timeout(2000);
180 if (failure.length) {
182 var notification = new Notify.Notification({
183 summary: "Git Live ERROR!!",
184 body : failure.join("\n")
188 notification.set_timeout(5000); // show errros for longer
191 this.queueRunning = false;
194 shouldIgnore: function(f)
196 if (f.name[0] == '.') {
198 if (f.name == '.htaccess') {
204 if (f.name.match(/~$/)) {
207 // ignore anything in top level!!!!
208 if (!f.vpath.length) {
217 * set gitpath and vpath
222 parsePath: function(f)
225 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
226 f.repo = vpath_ar.shift();
227 f.gitpath = gitlive + '/' + f.repo;
228 f.vpath = vpath_ar.join('/');
235 onChanged : function(src)
237 return; // always ignore this..?
238 //this.parsePath(src);
242 * results in git add + git commit..
248 onChangesDoneHint : function(src)
251 if (this.shouldIgnore(src)) {
256 if (typeof(this.just_created[src.path]) !='undefined') {
257 delete this.just_created[src.path];
258 this.lastAdd = new Date();
260 // [ src.gitpath, 'add', src.vpath ],
261 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
264 this.action_queue.push({
274 this.lastAdd = new Date();
276 // [ src.gitpath, 'add', src.vpath ],
277 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
281 this.action_queue.push({
289 onDeleted : function(src)
292 if (this.shouldIgnore(src)) {
295 // should check if monitor needs removing..
296 // it should also check if it was a directory.. - so we dont have to commit all..
298 this.lastAdd = new Date();
300 // [ src.gitpath, 'rm' , src.vpath ],
301 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
304 this.action_queue.push({
311 onCreated : function(src)
314 if (this.shouldIgnore(src)) {
318 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
319 this.just_created[src.path] = true;
320 return; // we do not handle file create flags... - use done hint.
322 // director has bee created
323 this.monitor(src.path);
326 since git does not really handle directory adds...
328 this.lastAdd = new Date();
329 this.action_queue.push({
336 [ src.gitpath, 'add' , src.vpath, { all: true } ],
337 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
344 onAttributeChanged : function(src) {
346 if (this.shouldIgnore(src)) {
349 this.lastAdd = new Date();
353 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
355 this.action_queue.push({
364 onMoved : function(src,dest)
367 this.parsePath(dest);
369 if (src.gitpath != dest.gitpath) {
371 this.onCreated(dest);
372 this.onChangedDoneHint(dest);
375 // needs to handle move to/from unsupported types..
377 if (this.shouldIgnore(src)) {
380 if (this.shouldIgnore(dest)) {
383 this.lastAdd = new Date();
385 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
386 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
387 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
390 this.action_queue.push({
406 function errorDialog(data) {
407 var msg = new Gtk.MessageDialog({
408 message_type: Gtk.MessageType.ERROR,
409 buttons : Gtk.ButtonsType.OK,
421 // need a better icon...
427 Notify.init("gitlive");
429 monitor.add(GLib.get_home_dir() + "/gitlive");
432 //icon.signal["activate"].connect(on_left_click);