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(/___jb_old___$/)) {
80 if (f.name.match(/___jb_bak___$/)) {
83 //if (f.name.match(/^nbproject/)) {
86 // ignore anything in top level!!!!
87 if (this.gitpath.length < 1) {
96 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
98 for(var i =0; i < add_files.length; i++) {
99 if (add_files.index(i).vname == add) {
105 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
106 for(var i =0; i < messages.length; i++) {
107 if (messages.index(i).message == message) {
113 public static string messageToString(Array<GitMonitorQueue> messages ) {
115 for(var i =0; i < messages.length; i++) {
116 ret+= messages.index(i).message;
118 return string.joinv("\n",ret);
120 public static string queueArrayToString(Array<GitMonitorQueue> list) {
122 for(var i =0; i < list.length; i++) {
124 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
130 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
131 for(var i =0; i < list.length; i++) {
132 var test = list.index(i);
133 if (list.index(i).gitpath != cmd_s.gitpath) {
136 if (list.index(i).vname != cmd_s.vname) {
139 if (list.index(i).action != action) {
146 public string fullpath()
148 return this.gitpath + "/" + this.vname;
157 public class GitMonitor : Monitor
160 public static GitMonitor gitmonitor;
162 * @property {String} the "gitlive" directory, normally ~/gitlive
163 * dset by OWNER... - we should do this as a CTOR.
166 public static string gitlive;
169 public Array<GitMonitorQueue> queue ;
170 public bool queueRunning = false;
172 public DateTime lastAdd;
175 public GitMonitor () {
176 this.queue = new Array<GitMonitorQueue>();
177 GitMonitor.gitmonitor = this;
183 public new void pause() {
185 // what does this do to the old one...
186 this.queue = new Array<GitMonitorQueue> ();
187 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
191 public new void resume () {
193 this.queue = new Array<GitMonitorQueue> ();
194 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
199 * Start the monitoring
200 * and run the queue every 500 milliseconds..
203 public new void start() {
204 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
207 this.lastAdd = new DateTime.now(new TimeZone.local());
209 Timeout.add_full(Priority.LOW, 500, () => {
210 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
211 // call this.monitor on each of 'top'
212 for(int i = 0; i < this.top.length ; i++) {
214 this.monitor(this.top.index(i) );
216 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
223 var notification = new Notify.Notification(
225 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
229 notification.set_timeout(5);
234 return false; // do not keep doing this..
239 Timeout.add_full(Priority.LOW, 1000, () => {
240 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
242 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
243 if (this.queue.length < 1 || this.queueRunning) {
247 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
249 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
250 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
252 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
255 //_this.lastAdd = new Date();
265 public new void stop() {
266 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
271 public new void monitor (string path, int depth = 0)
274 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
277 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
278 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
283 // FIXME - check if repo is flagged as not autocommit..
284 //var repo = imports.Scm.Repo.Repo.get(path);
285 //if (!repo || !repo.autocommit()) {
291 // check if the repo is to be monitored.
292 //print("PATH : " + path);
295 base.monitor(path, depth);
302 * - pulls the items off the queue
303 * (as commands run concurrently and new items may get added while it's running)
304 * - runs the queue items
308 public void runQueue()
314 print("GitMonitor.runQueue\n");
316 this.queueRunning = true;
318 var cmds = new Array<GitMonitorQueue>();
320 for(var i = 0; i < this.queue.length; i++) {
321 cmds.append_val(this.queue.index(i));
324 this.queue = new Array<GitMonitorQueue>();// empty queue!
327 string[] success = {};
328 string[] failure = {};
329 //var repos = new Array<GitRepo>(); //??
330 //var done = new Array<GitMonitorQueue>();
332 // first build a array of repo's to work with
333 var repo_list = new Array<GitRepo>();
337 //print(JSON.stringify(cmds));
338 // make sure nothing get's added to the queue where we are doing this..
342 print("GitMonitor.runQueue - creating repos\n");
344 for(var i = 0; i < cmds.length; i++) {
346 var cmd = cmds.index(i);
348 var gitpath = cmd.gitpath;
349 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
351 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
353 repo_list.append_val(new GitRepo( gitpath ));
354 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
356 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
358 //if (typeof(repo_list[gitpath]) == 'undefined') {
359 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
360 // repo_list[gitpath].cmds = [];
361 // repo_list[gitpath].pull();
363 repo_list.index(ix).cmds.append_val(cmd);
367 // build add, remove and commit message list..
369 print("GitMonitor.runQueue - creating actions\n");
371 for(var i = 0;i < repo_list.length;i++) {
373 var repo = repo_list.index(i);
375 var add_files = new Array<GitMonitorQueue>();
376 var add_files_f = new Array<GitMonitorQueue>();
377 var remove_files = new Array<GitMonitorQueue>();
378 var messages = new Array<GitMonitorQueue>();
379 //print(JSON.stringify(repo.cmds,null,4));
381 for(var ii = 0;ii < repo.cmds.length;ii++) {
382 var cmd = repo.cmds.index(ii);
388 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
392 add_files.append_val(cmd);
396 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
400 // if file exists, do not try and delete it.
401 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
405 remove_files.append_val(cmd);
409 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
412 messages.append_val(cmd);
416 stdout.printf("Opps unmatched action %s\n", cmd.action);
420 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
421 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
423 // these can fail... at present... as we wildcard stuff.
425 // make sure added files do not get removed.. ??
427 var remove_files_f = new Array<GitMonitorQueue>();
428 for(var ii = 0;ii < remove_files.length;ii++) {
429 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
432 remove_files_f.append_val(remove_files.index(ii));
434 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
437 // if file was added, then removed,
438 var remove_files_f = new Array<GitMonitorQueue>();
439 for(var ii = 0;ii < remove_files.length;ii++) {
443 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
444 // in add and remove - do not remvove
447 remove_files_f.append_val(remove_files.index(ii));
449 for(var ii = 0;ii < add_files.length;ii++) {
450 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
451 // the add file is in the remove list, and it does not exist - do not add it..
452 print("check exists ? %s\n",add_files.index(ii).fullpath());
454 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
462 add_files_f.append_val(add_files.index(ii));
465 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
466 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
469 // make sure monitoring is paused so it does not recursively pick up
474 failure += e.message;
480 repo.add(add_files_f);
482 failure += e.message;
485 repo.remove(remove_files_f);
487 failure += e.message;
494 success += repo.commit(
495 GitMonitorQueue.messageToString(messages),
498 success += repo.push();
501 failure += e.message;
506 // finally merge all the commit messages.
509 // catch notification failures.. so we can carry on..
510 if (success.length > 0) {
513 var notification = new Notify.Notification(
515 string.joinv("\n",success),
520 notification.set_timeout(5);
524 if (failure.length > 0) {
526 var notification = new Notify.Notification(
528 string.joinv("\n",failure),
533 notification.set_timeout(5); // show errros for longer
540 this.queueRunning = false;
547 //string[] just_created;
556 public override void onChanged(MonitorNamePathDir src)
558 print("GitMonitor.onChanged\n");
559 return; // always ignore this..?
560 //this.parsePath(src);
566 * results in git add + git commit..
569 public override void onChangesDoneHint(MonitorNamePathDir src)
571 print("GitMonitor.onChangedHint\n");
577 this.lastAdd = new DateTime.now(new TimeZone.local());
578 var cmd = new GitMonitorQueue(src);
579 if (cmd.shouldIgnore()) {
584 //var add_it = false;
586 if (this.is_just_created(cmd.path)) {
588 if (typeof(this.just_created[src.path]) !='undefined') {
589 delete this.just_created[src.path];
592 [ src.gitpath, 'add', src.vpath ],
593 [ src.gitpath, 'commit', { message: src.vpath} ]
601 this.queue.append_val(cmd);
603 cmd = new GitMonitorQueue(src);
604 cmd.action = "commit";
605 cmd.message = cmd.vname;
606 this.queue.append_val(cmd);
610 public override void onDeleted(MonitorNamePathDir src)
612 print("GitMonitor.onDeleted\n");
616 this.lastAdd = new DateTime.now(new TimeZone.local());
617 var cmd = new GitMonitorQueue(src);
618 if (cmd.shouldIgnore()) {
621 // should check if monitor needs removing..
622 // it should also check if it was a directory.. - so we dont have to commit all..
624 this.queue.append_val(cmd);
626 cmd = new GitMonitorQueue(src);
627 cmd.action = "commit";
628 cmd.message = cmd.vname;
629 cmd.commit_all = true;
631 this.queue.append_val(cmd);
634 public override void onCreated(MonitorNamePathDir src) {
635 print("GitMonitor.onCreated\n");
639 this.lastAdd = new DateTime.now(new TimeZone.local());
640 var cmd = new GitMonitorQueue(src);
641 if (cmd.shouldIgnore()) {
645 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
646 // this.just_created[src.path] = true;
647 return; // we do not handle file create flags... - use done hint.
649 // directory has bee created
650 this.monitor(src.path);
651 //this.top.append_val(src.path);
652 //this.monitor(src.path );
655 // -- no point in adding a dir.. as git does not handle them...
657 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
658 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
664 public override void onAttributeChanged(MonitorNamePathDir src) {
665 print("GitMonitor.onAttributeChanged\n");
669 if (src.dir == GitMonitor.gitlive) {
670 return; // attribute on top level..
673 this.lastAdd = new DateTime.now(new TimeZone.local());
674 var cmd = new GitMonitorQueue(src);
675 if (cmd.shouldIgnore()) {
679 this.queue.append_val(cmd);
681 cmd = new GitMonitorQueue(src);
682 cmd.action = "commit";
683 cmd.message = "Attribute changed " + cmd.vname;
684 this.queue.append_val(cmd);
687 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
689 print("GitMonitor.onMoved\n");
693 this.lastAdd = new DateTime.now(new TimeZone.local());
694 var cmd_s = new GitMonitorQueue(src);
696 var cmd_d = new GitMonitorQueue(dest);
699 if (cmd_d.gitpath != cmd_s.gitpath) {
701 this.onCreated(dest);
702 this.onChangesDoneHint(dest);
705 // needs to handle move to/from unsupported types..
707 if (cmd_s.shouldIgnore()) {
708 this.onCreated(dest);
709 this.onChangesDoneHint(dest);
713 if (cmd_d.shouldIgnore()) {
722 print("RM: %s\n", cmd_s.vname);
724 this.queue.append_val(cmd_s);
728 print("ADD: %s\n", cmd_d.vname);
729 cmd_d.action = "add";
730 this.queue.append_val(cmd_d);
733 var cmd = new GitMonitorQueue(dest);
734 cmd.action = "commit";
735 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
736 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
737 cmd.message = cmd_d.vname;
740 this.queue.append_val(cmd);