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") {
74 if (this.name[this.name.length -1] == '~') {
77 // netbeans / android studio.. silly temp files..
79 if (Regex.match_simple("___jb_old___$", this.name)) {
82 if (Regex.match_simple("___jb_bak___$", this.name)) {
85 //if (f.name.match(/^nbproject/)) {
88 // ignore anything in top level!!!!
89 if (this.gitpath.length < 1) {
98 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
100 for(var i =0; i < add_files.length; i++) {
101 if (add_files.index(i).vname == add) {
107 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
108 for(var i =0; i < messages.length; i++) {
109 if (messages.index(i).message == message) {
115 public static string messageToString(Array<GitMonitorQueue> messages ) {
117 for(var i =0; i < messages.length; i++) {
118 ret+= messages.index(i).message;
120 return string.joinv("\n",ret);
122 public static string queueArrayToString(Array<GitMonitorQueue> list) {
124 for(var i =0; i < list.length; i++) {
126 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
132 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
133 for(var i =0; i < list.length; i++) {
134 var test = list.index(i);
135 if (list.index(i).gitpath != cmd_s.gitpath) {
138 if (list.index(i).vname != cmd_s.vname) {
141 if (list.index(i).action != action) {
148 public string fullpath()
150 return this.gitpath + "/" + this.vname;
159 public class GitMonitor : Monitor
162 public static GitMonitor gitmonitor;
164 * @property {String} the "gitlive" directory, normally ~/gitlive
165 * dset by OWNER... - we should do this as a CTOR.
168 public static string gitlive;
171 public Array<GitMonitorQueue> queue ;
172 public bool queueRunning = false;
174 public DateTime lastAdd;
177 public GitMonitor () {
178 this.queue = new Array<GitMonitorQueue>();
179 GitMonitor.gitmonitor = this;
182 public new void pauseError() {
184 // what does this do to the old one...
185 this.queue = new Array<GitMonitorQueue> ();
186 StatusIconA.statusicon.pauseError();
191 public new void pause() {
193 // what does this do to the old one...
194 this.queue = new Array<GitMonitorQueue> ();
195 StatusIconA.statusicon.pause();
199 public new void resume () {
201 this.queue = new Array<GitMonitorQueue> ();
202 StatusIconA.statusicon.resume();
207 * Start the monitoring
208 * and run the queue every 500 milliseconds..
211 public new void start() {
212 StatusIconA.statusicon.refreshing();
215 this.lastAdd = new DateTime.now(new TimeZone.local());
217 Timeout.add_full(Priority.LOW, 500, () => {
218 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
219 // call this.monitor on each of 'top'
220 for(int i = 0; i < this.top.length ; i++) {
222 this.monitor(this.top.index(i) );
224 StatusIconA.statusicon.resume();
231 var notification = new Notify.Notification(
233 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
237 notification.set_timeout(5);
240 print("Error %s\n",e.message);
242 return false; // do not keep doing this..
247 Timeout.add_full(Priority.LOW, 1000, () => {
248 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
250 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
251 if (this.queue.length < 1 || this.queueRunning) {
255 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
257 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
258 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
260 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
263 //_this.lastAdd = new Date();
273 public new void stop() {
274 StatusIconA.statusicon.pause();
279 public new void monitor (string path, int depth = 0)
282 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
285 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
286 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
291 // FIXME - check if repo is flagged as not autocommit..
292 //var repo = imports.Scm.Repo.Repo.get(path);
293 //if (!repo || !repo.autocommit()) {
299 // check if the repo is to be monitored.
300 //print("PATH : " + path);
303 base.monitor(path, depth);
310 * - pulls the items off the queue
311 * (as commands run concurrently and new items may get added while it's running)
312 * - runs the queue items
316 public void runQueue()
322 print("GitMonitor.runQueue\n");
324 this.queueRunning = true;
326 var cmds = new Array<GitMonitorQueue>();
328 for(var i = 0; i < this.queue.length; i++) {
329 cmds.append_val(this.queue.index(i));
332 this.queue = new Array<GitMonitorQueue>();// empty queue!
335 string[] success = {};
336 string[] failure = {};
337 //var repos = new Array<GitRepo>(); //??
338 //var done = new Array<GitMonitorQueue>();
340 // first build a array of repo's to work with
341 var repo_list = new Array<GitRepo>();
345 //print(JSON.stringify(cmds));
346 // make sure nothing get's added to the queue where we are doing this..
350 print("GitMonitor.runQueue - creating repos\n");
352 for(var i = 0; i < cmds.length; i++) {
354 var cmd = cmds.index(i);
356 var gitpath = cmd.gitpath;
357 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
359 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
361 repo_list.append_val(new GitRepo( gitpath ));
362 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
364 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
366 //if (typeof(repo_list[gitpath]) == 'undefined') {
367 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
368 // repo_list[gitpath].cmds = [];
369 // repo_list[gitpath].pull();
371 repo_list.index(ix).cmds.append_val(cmd);
375 // build add, remove and commit message list..
377 print("GitMonitor.runQueue - creating actions\n");
379 for(var i = 0;i < repo_list.length;i++) {
381 var repo = repo_list.index(i);
383 var add_files = new Array<GitMonitorQueue>();
384 var add_files_f = new Array<GitMonitorQueue>();
385 var remove_files = new Array<GitMonitorQueue>();
386 var messages = new Array<GitMonitorQueue>();
387 //print(JSON.stringify(repo.cmds,null,4));
389 for(var ii = 0;ii < repo.cmds.length;ii++) {
390 var cmd = repo.cmds.index(ii);
396 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
400 add_files.append_val(cmd);
404 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
408 // if file exists, do not try and delete it.
409 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
413 remove_files.append_val(cmd);
417 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
420 messages.append_val(cmd);
424 stdout.printf("Opps unmatched action %s\n", cmd.action);
428 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
429 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
431 // these can fail... at present... as we wildcard stuff.
433 // make sure added files do not get removed.. ??
435 var remove_files_f = new Array<GitMonitorQueue>();
436 for(var ii = 0;ii < remove_files.length;ii++) {
437 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
440 remove_files_f.append_val(remove_files.index(ii));
442 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
445 // if file was added, then removed,
446 var remove_files_f = new Array<GitMonitorQueue>();
447 for(var ii = 0;ii < remove_files.length;ii++) {
451 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
452 // in add and remove - do not remvove
455 remove_files_f.append_val(remove_files.index(ii));
457 for(var ii = 0;ii < add_files.length;ii++) {
458 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
459 // the add file is in the remove list, and it does not exist - do not add it..
460 print("check exists ? %s\n",add_files.index(ii).fullpath());
462 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
470 add_files_f.append_val(add_files.index(ii));
473 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
474 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
477 // make sure monitoring is paused so it does not recursively pick up
482 this.pauseError(e.message);
483 //failure += e.message;
484 ..print("Pull failed:\n");
491 repo.add(add_files_f);
493 failure += e.message;
494 print("Add failed:\n");
497 repo.remove(remove_files_f);
499 failure += e.message;
500 print("Remove failed:\n");
507 success += repo.commit(
508 GitMonitorQueue.messageToString(messages),
511 success += repo.push();
514 failure += e.message;
515 print("Push failed:\n");
521 // finally merge all the commit messages.
524 // catch notification failures.. so we can carry on..
525 if (success.length > 0) {
528 var notification = new Notify.Notification(
530 string.joinv("\n",success),
535 notification.set_timeout(5);
539 if (failure.length > 0) {
541 var notification = new Notify.Notification(
543 string.joinv("\n",failure),
549 notification.set_timeout(10); // show errros for longer
558 this.queueRunning = false;
565 //string[] just_created;
574 public override void onChanged(MonitorNamePathDir src)
576 print("GitMonitor.onChanged\n");
577 return; // always ignore this..?
578 //this.parsePath(src);
584 * results in git add + git commit..
587 public override void onChangesDoneHint(MonitorNamePathDir src)
589 print("GitMonitor.onChangedHint\n");
595 this.lastAdd = new DateTime.now(new TimeZone.local());
596 var cmd = new GitMonitorQueue(src);
597 if (cmd.shouldIgnore()) {
602 //var add_it = false;
604 if (this.is_just_created(cmd.path)) {
606 if (typeof(this.just_created[src.path]) !='undefined') {
607 delete this.just_created[src.path];
610 [ src.gitpath, 'add', src.vpath ],
611 [ src.gitpath, 'commit', { message: src.vpath} ]
619 this.queue.append_val(cmd);
621 cmd = new GitMonitorQueue(src);
622 cmd.action = "commit";
623 cmd.message = cmd.vname;
624 this.queue.append_val(cmd);
628 public override void onDeleted(MonitorNamePathDir src)
630 print("GitMonitor.onDeleted\n");
634 this.lastAdd = new DateTime.now(new TimeZone.local());
635 var cmd = new GitMonitorQueue(src);
636 if (cmd.shouldIgnore()) {
639 // should check if monitor needs removing..
640 // it should also check if it was a directory.. - so we dont have to commit all..
642 this.queue.append_val(cmd);
644 cmd = new GitMonitorQueue(src);
645 cmd.action = "commit";
646 cmd.message = cmd.vname;
647 cmd.commit_all = true;
649 this.queue.append_val(cmd);
652 public override void onCreated(MonitorNamePathDir src) {
653 print("GitMonitor.onCreated\n");
657 this.lastAdd = new DateTime.now(new TimeZone.local());
658 var cmd = new GitMonitorQueue(src);
659 if (cmd.shouldIgnore()) {
663 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
664 // this.just_created[src.path] = true;
665 return; // we do not handle file create flags... - use done hint.
667 // directory has bee created
668 this.monitor(src.path);
669 //this.top.append_val(src.path);
670 //this.monitor(src.path );
673 // -- no point in adding a dir.. as git does not handle them...
675 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
676 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
682 public override void onAttributeChanged(MonitorNamePathDir src) {
683 print("GitMonitor.onAttributeChanged\n");
687 if (src.dir == GitMonitor.gitlive) {
688 return; // attribute on top level..
691 this.lastAdd = new DateTime.now(new TimeZone.local());
692 var cmd = new GitMonitorQueue(src);
693 if (cmd.shouldIgnore()) {
697 this.queue.append_val(cmd);
699 cmd = new GitMonitorQueue(src);
700 cmd.action = "commit";
701 cmd.message = "Attribute changed " + cmd.vname;
702 this.queue.append_val(cmd);
705 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
707 print("GitMonitor.onMoved\n");
711 this.lastAdd = new DateTime.now(new TimeZone.local());
712 var cmd_s = new GitMonitorQueue(src);
714 var cmd_d = new GitMonitorQueue(dest);
717 if (cmd_d.gitpath != cmd_s.gitpath) {
719 this.onCreated(dest);
720 this.onChangesDoneHint(dest);
723 // needs to handle move to/from unsupported types..
725 if (cmd_s.shouldIgnore()) {
726 this.onCreated(dest);
727 this.onChangesDoneHint(dest);
731 if (cmd_d.shouldIgnore()) {
740 print("RM: %s\n", cmd_s.vname);
742 this.queue.append_val(cmd_s);
746 print("ADD: %s\n", cmd_d.vname);
747 cmd_d.action = "add";
748 this.queue.append_val(cmd_d);
751 var cmd = new GitMonitorQueue(dest);
752 cmd.action = "commit";
753 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
754 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
755 cmd.message = cmd_d.vname;
758 this.queue.append_val(cmd);