4 public class GitMonitorQueue : MonitorNamePathDir {
10 public string vdir; // relative path (within git)
11 public string vname; // relative filename (within git)
12 public string message ; // for commit
13 public bool commit_all;
15 public GitMonitorQueue(MonitorNamePathDir f) {
17 base(f.name, f.path, f.dir);
20 this.commit_all = false;
22 var vpath_ar = this.dir.substring(GitMonitor.gitlive.length +1).split("/", 0);
24 if (vpath_ar[0].length < 1) {
32 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
35 for (var i = 1; i< vpath_ar.length; i++) {
39 this.vdir = string.joinv("/", vpath);
41 this.vname = this.vdir + (this.vdir.length > 0 ? "/" : "") + this.name;
44 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
45 "gitpath: %s\nvdir: %s\nvname: %s\n",
46 this.name, this.path, this.dir,
47 this.gitpath, this.vdir, this.vname
51 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
56 public bool shouldIgnore()
60 // vim.. what a seriously brain dead program..
61 if (this.name == "4913") {
65 if (this.name[0] == '.') {
67 if (this.name == ".htaccess") {
73 if (this.name[this.name.length -1] == '~') {
77 //if (f.name.match(/~$/)) {
80 //if (f.name.match(/^nbproject/)) {
83 // ignore anything in top level!!!!
84 if (this.gitpath.length < 1) {
93 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
95 for(var i =0; i < add_files.length; i++) {
96 if (add_files.index(i).vname == add) {
102 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
103 for(var i =0; i < messages.length; i++) {
104 if (messages.index(i).message == message) {
110 public static string messageToString(Array<GitMonitorQueue> messages ) {
112 for(var i =0; i < messages.length; i++) {
113 ret+= messages.index(i).message;
115 return string.joinv("\n",ret);
117 public static string queueArrayToString(Array<GitMonitorQueue> list{
119 for(var i =0; i < list.length; i++) {
121 ret += (ret.length > 0 ? ", " : "") + list.item(i).vname;
130 public class GitMonitor : Monitor
133 public static GitMonitor gitmonitor;
135 * @property {String} the "gitlive" directory, normally ~/gitlive
136 * dset by OWNER... - we should do this as a CTOR.
139 public static string gitlive;
142 public Array<GitMonitorQueue> queue ;
143 public bool queueRunning = false;
145 public DateTime lastAdd;
148 public GitMonitor () {
149 this.queue = new Array<GitMonitorQueue>();
150 GitMonitor.gitmonitor = this;
156 public new void pause() {
158 // what does this do to the old one...
159 this.queue = new Array<GitMonitorQueue> ();
160 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
164 public new void resume () {
166 this.queue = new Array<GitMonitorQueue> ();
167 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
172 * Start the monitoring
173 * and run the queue every 500 milliseconds..
176 public new void start() {
177 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
180 this.lastAdd = new DateTime.now(new TimeZone.local());
182 Timeout.add_full(Priority.LOW, 500, () => {
183 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
184 // call this.monitor on each of 'top'
185 for(int i = 0; i < this.top.length ; i++) {
187 this.monitor(this.top.index(i) );
189 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
196 var notification = new Notify.Notification(
198 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
202 notification.set_timeout(5);
207 return false; // do not keep doing this..
212 Timeout.add_full(Priority.LOW, 1000, () => {
213 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
215 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
216 if (this.queue.length < 1 || this.queueRunning) {
220 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
222 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
223 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
225 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
228 //_this.lastAdd = new Date();
238 public new void stop() {
239 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
244 public new void monitor (string path, int depth = 0)
247 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
250 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
251 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
256 // FIXME - check if repo is flagged as not autocommit..
257 //var repo = imports.Scm.Repo.Repo.get(path);
258 //if (!repo || !repo.autocommit()) {
264 // check if the repo is to be monitored.
265 //print("PATH : " + path);
268 base.monitor(path, depth);
275 * - pulls the items off the queue
276 * (as commands run concurrently and new items may get added while it's running)
277 * - runs the queue items
281 public void runQueue()
287 print("GitMonitor.runQueue\n");
289 this.queueRunning = true;
291 var cmds = new Array<GitMonitorQueue>();
293 for(var i = 0; i < this.queue.length; i++) {
294 cmds.append_val(this.queue.index(i));
297 this.queue = new Array<GitMonitorQueue>();// empty queue!
300 string[] success = {};
301 string[] failure = {};
302 //var repos = new Array<GitRepo>(); //??
303 //var done = new Array<GitMonitorQueue>();
305 // first build a array of repo's to work with
306 var repo_list = new Array<GitRepo>();
310 //print(JSON.stringify(cmds));
311 // make sure nothing get's added to the queue where we are doing this..
315 print("GitMonitor.runQueue - creating repos\n");
317 for(var i = 0; i < cmds.length; i++) {
319 var cmd = cmds.index(i);
321 var gitpath = cmd.gitpath;
322 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
324 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
326 repo_list.append_val(new GitRepo( gitpath ));
327 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
329 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
331 //if (typeof(repo_list[gitpath]) == 'undefined') {
332 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
333 // repo_list[gitpath].cmds = [];
334 // repo_list[gitpath].pull();
336 repo_list.index(ix).cmds.append_val(cmd);
340 // build add, remove and commit message list..
342 print("GitMonitor.runQueue - creating actions\n");
344 for(var i = 0;i < repo_list.length;i++) {
346 var repo = repo_list.index(i);
348 var add_files = new Array<GitMonitorQueue>();
349 var add_files_f = new Array<GitMonitorQueue>();
350 var remove_files = new Array<GitMonitorQueue>();
351 var messages = new Array<GitMonitorQueue>();
352 //print(JSON.stringify(repo.cmds,null,4));
354 for(var ii = 0;ii < repo.cmds.length;ii++) {
355 var cmd = repo.cmds.index(ii);
361 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
365 add_files.append_val(cmd);
369 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
373 // if file exists, do not try and delete it.
374 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
378 remove_files.append_val(cmd);
382 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
385 messages.append_val(cmd);
389 stdout.printf("Opps unmatched action %s\n", cmd.action);
393 print( "ADD : %s\n", queueArrayToString(add_files));
395 // these can fail... at present... as we wildcard stuff.
397 // make sure added files do not get removed.. ??
399 var remove_files_f = new Array<GitMonitorQueue>();
400 for(var ii = 0;ii < remove_files.length;ii++) {
401 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
404 remove_files_f.append_val(remove_files.index(ii));
406 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
409 // if file was added, then removed,
410 var remove_files_f = new Array<GitMonitorQueue>();
411 for(var ii = 0;ii < remove_files.length;ii++) {
412 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
413 // in add and remove - do not remvove
416 remove_files_f.append_val(remove_files.index(ii));
418 for(var ii = 0;ii < add_files.length;ii++) {
419 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
420 // in add and remove - do not remvove
423 add_files_f.append_val(add_files.index(ii));
426 print("ADD : %u files\n" , add_files_f.length);
427 print("REMOVE: %u files\n" , remove_files_f.length);
430 // make sure monitoring is paused so it does not recursively pick up
435 failure += e.message;
441 repo.add(add_files_f);
443 failure += e.message;
446 repo.remove(remove_files_f);
448 failure += e.message;
455 success += repo.commit(
456 GitMonitorQueue.messageToString(messages),
459 success += repo.push();
462 failure += e.message;
467 // finally merge all the commit messages.
470 // catch notification failures.. so we can carry on..
471 if (success.length > 0) {
474 var notification = new Notify.Notification(
476 string.joinv("\n",success),
481 notification.set_timeout(5);
485 if (failure.length > 0) {
487 var notification = new Notify.Notification(
489 string.joinv("\n",failure),
494 notification.set_timeout(5); // show errros for longer
501 this.queueRunning = false;
508 //string[] just_created;
517 public override void onChanged(MonitorNamePathDir src)
519 print("GitMonitor.onChanged\n");
520 return; // always ignore this..?
521 //this.parsePath(src);
527 * results in git add + git commit..
530 public override void onChangesDoneHint(MonitorNamePathDir src)
532 print("GitMonitor.onChangedHint\n");
538 this.lastAdd = new DateTime.now(new TimeZone.local());
539 var cmd = new GitMonitorQueue(src);
540 if (cmd.shouldIgnore()) {
545 //var add_it = false;
547 if (this.is_just_created(cmd.path)) {
549 if (typeof(this.just_created[src.path]) !='undefined') {
550 delete this.just_created[src.path];
553 [ src.gitpath, 'add', src.vpath ],
554 [ src.gitpath, 'commit', { message: src.vpath} ]
562 this.queue.append_val(cmd);
564 cmd = new GitMonitorQueue(src);
565 cmd.action = "commit";
566 cmd.message = cmd.vname;
567 this.queue.append_val(cmd);
571 public override void onDeleted(MonitorNamePathDir src)
573 print("GitMonitor.onDeleted\n");
577 this.lastAdd = new DateTime.now(new TimeZone.local());
578 var cmd = new GitMonitorQueue(src);
579 if (cmd.shouldIgnore()) {
582 // should check if monitor needs removing..
583 // it should also check if it was a directory.. - so we dont have to commit all..
585 this.queue.append_val(cmd);
587 cmd = new GitMonitorQueue(src);
588 cmd.action = "commit";
589 cmd.message = cmd.vname;
590 cmd.commit_all = true;
592 this.queue.append_val(cmd);
595 public override void onCreated(MonitorNamePathDir src) {
596 print("GitMonitor.onCreated\n");
600 this.lastAdd = new DateTime.now(new TimeZone.local());
601 var cmd = new GitMonitorQueue(src);
602 if (cmd.shouldIgnore()) {
606 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
607 // this.just_created[src.path] = true;
608 return; // we do not handle file create flags... - use done hint.
610 // directory has bee created
611 this.monitor(src.path);
612 //this.top.append_val(src.path);
613 //this.monitor(src.path );
616 // -- no point in adding a dir.. as git does not handle them...
618 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
619 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
625 public override void onAttributeChanged(MonitorNamePathDir src) {
626 print("GitMonitor.onAttributeChanged\n");
630 if (src.dir == GitMonitor.gitlive) {
631 return; // attribute on top level..
634 this.lastAdd = new DateTime.now(new TimeZone.local());
635 var cmd = new GitMonitorQueue(src);
636 if (cmd.shouldIgnore()) {
640 this.queue.append_val(cmd);
642 cmd = new GitMonitorQueue(src);
643 cmd.action = "commit";
644 cmd.message = "Attribute changed " + cmd.vname;
645 this.queue.append_val(cmd);
648 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
650 print("GitMonitor.onMoved\n");
654 this.lastAdd = new DateTime.now(new TimeZone.local());
655 var cmd_s = new GitMonitorQueue(src);
657 var cmd_d = new GitMonitorQueue(dest);
660 if (cmd_d.gitpath != cmd_s.gitpath) {
662 this.onCreated(dest);
663 this.onChangesDoneHint(dest);
666 // needs to handle move to/from unsupported types..
668 if (cmd_s.shouldIgnore()) {
669 this.onCreated(dest);
670 this.onChangesDoneHint(dest);
674 if (cmd_d.shouldIgnore()) {
683 this.queue.append_val(cmd_s);
687 cmd_d.action = "add";
688 this.queue.append_val(cmd_d);
691 var cmd = new GitMonitorQueue(dest);
692 cmd.action = "commit";
693 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
694 this.queue.append_val(cmd);