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.index(i).vname;
127 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
128 for(var i =0; i < list.length; i++) {
129 var test = list.index(i);
130 if (list.index(i).gitpath != cmd_s.gitpath) {
133 if (list.index(i).vname != cmd_s.vname) {
136 if (list.index(i).action != action) {
148 public class GitMonitor : Monitor
151 public static GitMonitor gitmonitor;
153 * @property {String} the "gitlive" directory, normally ~/gitlive
154 * dset by OWNER... - we should do this as a CTOR.
157 public static string gitlive;
160 public Array<GitMonitorQueue> queue ;
161 public bool queueRunning = false;
163 public DateTime lastAdd;
166 public GitMonitor () {
167 this.queue = new Array<GitMonitorQueue>();
168 GitMonitor.gitmonitor = this;
174 public new void pause() {
176 // what does this do to the old one...
177 this.queue = new Array<GitMonitorQueue> ();
178 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
182 public new void resume () {
184 this.queue = new Array<GitMonitorQueue> ();
185 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
190 * Start the monitoring
191 * and run the queue every 500 milliseconds..
194 public new void start() {
195 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
198 this.lastAdd = new DateTime.now(new TimeZone.local());
200 Timeout.add_full(Priority.LOW, 500, () => {
201 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
202 // call this.monitor on each of 'top'
203 for(int i = 0; i < this.top.length ; i++) {
205 this.monitor(this.top.index(i) );
207 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
214 var notification = new Notify.Notification(
216 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
220 notification.set_timeout(5);
225 return false; // do not keep doing this..
230 Timeout.add_full(Priority.LOW, 1000, () => {
231 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
233 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
234 if (this.queue.length < 1 || this.queueRunning) {
238 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
240 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
241 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
243 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
246 //_this.lastAdd = new Date();
256 public new void stop() {
257 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
262 public new void monitor (string path, int depth = 0)
265 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
268 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
269 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
274 // FIXME - check if repo is flagged as not autocommit..
275 //var repo = imports.Scm.Repo.Repo.get(path);
276 //if (!repo || !repo.autocommit()) {
282 // check if the repo is to be monitored.
283 //print("PATH : " + path);
286 base.monitor(path, depth);
293 * - pulls the items off the queue
294 * (as commands run concurrently and new items may get added while it's running)
295 * - runs the queue items
299 public void runQueue()
305 print("GitMonitor.runQueue\n");
307 this.queueRunning = true;
309 var cmds = new Array<GitMonitorQueue>();
311 for(var i = 0; i < this.queue.length; i++) {
312 cmds.append_val(this.queue.index(i));
315 this.queue = new Array<GitMonitorQueue>();// empty queue!
318 string[] success = {};
319 string[] failure = {};
320 //var repos = new Array<GitRepo>(); //??
321 //var done = new Array<GitMonitorQueue>();
323 // first build a array of repo's to work with
324 var repo_list = new Array<GitRepo>();
328 //print(JSON.stringify(cmds));
329 // make sure nothing get's added to the queue where we are doing this..
333 print("GitMonitor.runQueue - creating repos\n");
335 for(var i = 0; i < cmds.length; i++) {
337 var cmd = cmds.index(i);
339 var gitpath = cmd.gitpath;
340 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
342 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
344 repo_list.append_val(new GitRepo( gitpath ));
345 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
347 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
349 //if (typeof(repo_list[gitpath]) == 'undefined') {
350 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
351 // repo_list[gitpath].cmds = [];
352 // repo_list[gitpath].pull();
354 repo_list.index(ix).cmds.append_val(cmd);
358 // build add, remove and commit message list..
360 print("GitMonitor.runQueue - creating actions\n");
362 for(var i = 0;i < repo_list.length;i++) {
364 var repo = repo_list.index(i);
366 var add_files = new Array<GitMonitorQueue>();
367 var add_files_f = new Array<GitMonitorQueue>();
368 var remove_files = new Array<GitMonitorQueue>();
369 var messages = new Array<GitMonitorQueue>();
370 //print(JSON.stringify(repo.cmds,null,4));
372 for(var ii = 0;ii < repo.cmds.length;ii++) {
373 var cmd = repo.cmds.index(ii);
379 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
383 add_files.append_val(cmd);
387 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
391 // if file exists, do not try and delete it.
392 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
396 remove_files.append_val(cmd);
400 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
403 messages.append_val(cmd);
407 stdout.printf("Opps unmatched action %s\n", cmd.action);
411 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
412 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
414 // these can fail... at present... as we wildcard stuff.
416 // make sure added files do not get removed.. ??
418 var remove_files_f = new Array<GitMonitorQueue>();
419 for(var ii = 0;ii < remove_files.length;ii++) {
420 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
423 remove_files_f.append_val(remove_files.index(ii));
425 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
428 // if file was added, then removed,
429 var remove_files_f = new Array<GitMonitorQueue>();
430 for(var ii = 0;ii < remove_files.length;ii++) {
431 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
432 // in add and remove - do not remvove
435 remove_files_f.append_val(remove_files.index(ii));
437 for(var ii = 0;ii < add_files.length;ii++) {
438 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
439 // the add file is in the remove list, and it does not exist - do not add it..
440 if (!FileUtils.test(add_files.index(ii).vname, FileTest.EXISTS)) {
448 add_files_f.append_val(add_files.index(ii));
451 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
452 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
455 // make sure monitoring is paused so it does not recursively pick up
460 failure += e.message;
466 repo.add(add_files_f);
468 failure += e.message;
471 repo.remove(remove_files_f);
473 failure += e.message;
480 success += repo.commit(
481 GitMonitorQueue.messageToString(messages),
484 success += repo.push();
487 failure += e.message;
492 // finally merge all the commit messages.
495 // catch notification failures.. so we can carry on..
496 if (success.length > 0) {
499 var notification = new Notify.Notification(
501 string.joinv("\n",success),
506 notification.set_timeout(5);
510 if (failure.length > 0) {
512 var notification = new Notify.Notification(
514 string.joinv("\n",failure),
519 notification.set_timeout(5); // show errros for longer
526 this.queueRunning = false;
533 //string[] just_created;
542 public override void onChanged(MonitorNamePathDir src)
544 print("GitMonitor.onChanged\n");
545 return; // always ignore this..?
546 //this.parsePath(src);
552 * results in git add + git commit..
555 public override void onChangesDoneHint(MonitorNamePathDir src)
557 print("GitMonitor.onChangedHint\n");
563 this.lastAdd = new DateTime.now(new TimeZone.local());
564 var cmd = new GitMonitorQueue(src);
565 if (cmd.shouldIgnore()) {
570 //var add_it = false;
572 if (this.is_just_created(cmd.path)) {
574 if (typeof(this.just_created[src.path]) !='undefined') {
575 delete this.just_created[src.path];
578 [ src.gitpath, 'add', src.vpath ],
579 [ src.gitpath, 'commit', { message: src.vpath} ]
587 this.queue.append_val(cmd);
589 cmd = new GitMonitorQueue(src);
590 cmd.action = "commit";
591 cmd.message = cmd.vname;
592 this.queue.append_val(cmd);
596 public override void onDeleted(MonitorNamePathDir src)
598 print("GitMonitor.onDeleted\n");
602 this.lastAdd = new DateTime.now(new TimeZone.local());
603 var cmd = new GitMonitorQueue(src);
604 if (cmd.shouldIgnore()) {
607 // should check if monitor needs removing..
608 // it should also check if it was a directory.. - so we dont have to commit all..
610 this.queue.append_val(cmd);
612 cmd = new GitMonitorQueue(src);
613 cmd.action = "commit";
614 cmd.message = cmd.vname;
615 cmd.commit_all = true;
617 this.queue.append_val(cmd);
620 public override void onCreated(MonitorNamePathDir src) {
621 print("GitMonitor.onCreated\n");
625 this.lastAdd = new DateTime.now(new TimeZone.local());
626 var cmd = new GitMonitorQueue(src);
627 if (cmd.shouldIgnore()) {
631 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
632 // this.just_created[src.path] = true;
633 return; // we do not handle file create flags... - use done hint.
635 // directory has bee created
636 this.monitor(src.path);
637 //this.top.append_val(src.path);
638 //this.monitor(src.path );
641 // -- no point in adding a dir.. as git does not handle them...
643 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
644 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
650 public override void onAttributeChanged(MonitorNamePathDir src) {
651 print("GitMonitor.onAttributeChanged\n");
655 if (src.dir == GitMonitor.gitlive) {
656 return; // attribute on top level..
659 this.lastAdd = new DateTime.now(new TimeZone.local());
660 var cmd = new GitMonitorQueue(src);
661 if (cmd.shouldIgnore()) {
665 this.queue.append_val(cmd);
667 cmd = new GitMonitorQueue(src);
668 cmd.action = "commit";
669 cmd.message = "Attribute changed " + cmd.vname;
670 this.queue.append_val(cmd);
673 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
675 print("GitMonitor.onMoved\n");
679 this.lastAdd = new DateTime.now(new TimeZone.local());
680 var cmd_s = new GitMonitorQueue(src);
682 var cmd_d = new GitMonitorQueue(dest);
685 if (cmd_d.gitpath != cmd_s.gitpath) {
687 this.onCreated(dest);
688 this.onChangesDoneHint(dest);
691 // needs to handle move to/from unsupported types..
693 if (cmd_s.shouldIgnore()) {
694 this.onCreated(dest);
695 this.onChangesDoneHint(dest);
699 if (cmd_d.shouldIgnore()) {
708 print("RM: %s\n", cmd_s.vname);
710 this.queue.append_val(cmd_s);
714 print("ADD: %s\n", cmd_d.vname);
715 cmd_d.action = "add";
716 this.queue.append_val(cmd_d);
719 var cmd = new GitMonitorQueue(dest);
720 cmd.action = "commit";
721 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
722 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
723 cmd.message = cmd_d.vname;
726 this.queue.append_val(cmd);