2 var Gio = imports.gi.Gio;
3 var Gtk = imports.gi.Gtk;
4 var Notify = imports.gi.Notify;
5 var GLib = imports.gi.GLib;
7 var Spawn = imports.Spawn;
8 var StatusIcon = imports.StatusIcon.StatusIcon;
9 var Monitor = imports.Monitor.Monitor;
13 var GitMonitor = new Monitor({
16 * @property {String} the "gitlive" directory, normally ~/gitlive
17 * dset by OWNER... - we should do this as a CTOR.
31 imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
37 imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PLAY );
43 * Start the monitoring
44 * and run the queue every 500 milliseconds..
48 imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_REFRESH );
50 this.lastAdd = new Date();
52 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
53 _this.top.forEach(_this.monitor, _this);
54 imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PLAY );
57 var notification = new Notify.Notification({
59 body : this.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
63 notification.set_timeout(5);
71 GLib.timeout_add(GLib.PRIORITY_LOW, 1000, function() {
72 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
73 if (!_this.queue.length || _this.queueRunning) {
76 var last = Math.floor(((new Date()) - _this.lastAdd) / 100);
78 //print("LAST RUN?" + last);
80 if (last < 5) { // wait 1/2 a seconnd before running.
83 //_this.lastAdd = new Date();
95 imports.StatusIcon.StatusIcon.el.set_from_stock( Gtk.STOCK_MEDIA_PAUSE );
96 Monitor.prototype.stop.call(this);
100 monitor : function(path, fn, depth)
102 depth = typeof(depth) == 'number' ? depth *1 : 0;
105 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
106 if (depth > 1 && GLib.file_test(path + '/.git' , GLib.FileTest.IS_DIR)) {
111 var repo = imports.Scm.Repo.Repo.get(path);
112 if (!repo || !repo.autocommit()) {
118 // check if the repo is to be monitored.
119 //print("PATH : " + path);
122 Monitor.prototype.monitor.call(this, path,fn, depth);
127 * - pulls the items off the queue
128 * (as commands run concurrently and new items may get added while it's running)
129 * - runs the queue items
139 this.queueRunning = true;
141 this.queue.forEach(function (q) {
144 this.queue = []; // empty queue!
151 // first build a array of repo's to work with
156 //print(JSON.stringify(cmds));
158 cmds.forEach(function(cmd) {
159 var gitpath = cmd.shift();
160 if (typeof(repo_list[gitpath]) == 'undefined') {
161 repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
162 repo_list[gitpath].cmds = [];
163 repo_list[gitpath].pull();
165 repo_list[gitpath].cmds.push(cmd);
168 // build add, remove and commit message list..
172 for (var gitpath in repo_list) {
173 var repo = repo_list[gitpath];
175 var remove_files = [];
177 //print(JSON.stringify(repo.cmds,null,4));
179 repo.cmds.forEach(function(cmd) {
181 var name = cmd.shift();
182 var arg = cmd.shift();
187 if (add_files.indexOf(arg) > -1) {
191 // if file does not exist.. s,ip
193 //if (!GLib.file_test(arg, GLib.FileTest.EXISTS)) {
204 if (add_files.indexOf(arg) > -1) {
208 // if file exists, do not try and delete it.
209 if (GLib.file_test(arg, GLib.FileTest.EXISTS)) {
213 remove_files.push(arg);
218 if (messages.indexOf(arg.message) < 0) {
219 messages.push(arg.message);
226 // these can fail... at present... as we wildcard stuff.
227 print("ADD : " + JSON.stringify(add_files));
229 // make sure added files do not get removed..
230 remove_files = remove_files.filter(function(v) {
231 return add_files.indexOf(v) < 0;
233 print("REMOVE : " + JSON.stringify(remove_files));
236 // make sure monitoring is paused so it does not recursively pick up
243 repo.remove(remove_files);
248 success.push(repo.commit({
249 reason : messages.join("\n"),
252 success.push(repo.push());
255 failure.push(e.message);
260 // finally merge all the commit messages.
263 // catch notification failures.. so we can carry on..
264 if (success.length) {
265 print(success.join("\n"));
267 var notification = new Notify.Notification({
268 summary: "Git Live Commited",
269 body : success.join("\n"),
274 notification.set_timeout(5);
278 if (failure.length) {
280 var notification = new Notify.Notification({
281 summary: "Git Live ERROR!!",
282 body : failure.join("\n"),
287 notification.set_timeout(5); // show errros for longer
294 this.queueRunning = false;
297 shouldIgnore: function(f)
305 // vim.. what a seriously brain dead program..
306 if (f.name == '4913') {
310 if (f.name[0] == '.') {
312 if (f.name == '.htaccess') {
318 if (f.name.match(/~$/)) {
321 if (f.name.match(/^nbproject/)) {
324 // ignore anything in top level!!!!
325 if (!f.vpath.length) {
334 * Fill in gitlive, vpath and repo
337 parsePath: function(f)
340 var vpath_ar = f.path.substring(this.gitlive.length +1).split('/');
342 f.gitpath = this.gitlive + '/' + vpath_ar.shift();
343 f.vpath = vpath_ar.join('/');
344 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
351 onChanged : function(src)
353 return; // always ignore this..?
354 //this.parsePath(src);
361 * results in git add + git commit..
364 onChangesDoneHint : function(src)
366 this.lastAdd = new Date();
368 if (this.shouldIgnore(src)) {
374 if (typeof(this.just_created[src.path]) !='undefined') {
375 delete this.just_created[src.path];
378 [ src.gitpath, 'add', src.vpath ],
379 [ src.gitpath, 'commit', { message: src.vpath} ]
387 [ src.gitpath, 'add', src.vpath ],
388 [ src.gitpath, 'commit', { message: src.vpath} ]
393 onDeleted : function(src)
395 this.lastAdd = new Date();
397 if (this.shouldIgnore(src)) {
400 // should check if monitor needs removing..
401 // it should also check if it was a directory.. - so we dont have to commit all..
405 [ src.gitpath, 'rm' , src.vpath ],
406 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
412 onCreated : function(src)
414 this.lastAdd = new Date();
416 if (this.shouldIgnore(src)) {
420 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
421 this.just_created[src.path] = true;
422 return; // we do not handle file create flags... - use done hint.
424 // director has bee created
425 this.monitor(src.path);
428 [ src.gitpath, 'add' , src.vpath, { all: true } ],
429 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
435 onAttributeChanged : function(src)
437 this.lastAdd = new Date();
439 if (this.shouldIgnore(src)) {
445 //[ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
446 [ src.gitpath, 'add' , src.vpath ],
447 [ src.gitpath, 'commit' , { message: "Attribute Changed :" + src.vpath} ]
453 onMoved : function(src,dest)
455 this.lastAdd = new Date();
457 this.parsePath(dest);
459 if (src.gitpath != dest.gitpath) {
461 this.onCreated(dest);
462 this.onChangedDoneHint(dest);
465 // needs to handle move to/from unsupported types..
467 if (this.shouldIgnore(src)) {
470 if (this.shouldIgnore(dest)) {
475 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
476 [ src.gitpath, 'add', dest.vpath ],
477 [ src.gitpath, 'rm', src.vpath ],
479 [ src.gitpath, 'commit' ,
480 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath}