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;
46 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
47 "gitpath: %s\nvdir: %s\nvname: %s\n",
48 this.name, this.path, this.dir,
49 this.gitpath, this.vdir, this.vname
53 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
58 public bool shouldIgnore()
62 // vim.. what a seriously brain dead program..
63 if (this.name == "4913") {
67 if (this.name[0] == '.') {
69 if (this.name == ".htaccess") {
76 if (this.name[this.name.length -1] == '~') {
79 // netbeans / android studio.. silly temp files..
81 if (Regex.match_simple("___jb_old___$", this.name)) {
84 if (Regex.match_simple("___jb_bak___$", this.name)) {
87 //if (f.name.match(/^nbproject/)) {
90 // ignore anything in top level!!!!
91 if (this.gitpath.length < 1) {
100 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
102 for(var i =0; i < add_files.length; i++) {
103 if (add_files.index(i).vname == add) {
109 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
110 for(var i =0; i < messages.length; i++) {
111 if (messages.index(i).message == message) {
117 public static string messageToString(Array<GitMonitorQueue> messages ) {
119 for(var i =0; i < messages.length; i++) {
120 ret+= messages.index(i).message;
122 return string.joinv("\n",ret);
124 public static string queueArrayToString(Array<GitMonitorQueue> list) {
126 for(var i =0; i < list.length; i++) {
128 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
134 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
135 for(var i =0; i < list.length; i++) {
136 var test = list.index(i);
137 if (list.index(i).gitpath != cmd_s.gitpath) {
140 if (list.index(i).vname != cmd_s.vname) {
143 if (list.index(i).action != action) {
150 public string fullpath()
152 return this.gitpath + "/" + this.vname;
161 public class GitMonitor : Monitor
164 public static GitMonitor gitmonitor;
166 * @property {String} the "gitlive" directory, normally ~/gitlive
167 * dset by OWNER... - we should do this as a CTOR.
170 public static string gitlive;
173 public Array<GitMonitorQueue> queue ;
174 public bool queueRunning = false;
176 public DateTime lastAdd;
179 public GitMonitor () {
180 this.queue = new Array<GitMonitorQueue>();
181 GitMonitor.gitmonitor = this;
183 Timeout.add_full(Priority.LOW, 1000, () => {
185 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
187 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
188 if (this.queue.length < 1 || this.queueRunning) {
192 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
194 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
195 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
197 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
200 //_this.lastAdd = new Date();
210 public new void pauseError(string failure)
213 var notification = new Notify.Notification(
220 notification.set_timeout(60); // show errros for longer
228 this.queueRunning = false;
229 // what does this do to the old one...
230 this.queue = new Array<GitMonitorQueue> ();
232 StatusIconA.statusicon.pauseError();
234 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
235 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
236 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
239 m.set_keep_above(true);
241 m.set_position(Gtk.WindowPosition.CENTER);
242 m.response.connect( (id_pressed) => {
248 public new void pause() {
250 // what does this do to the old one...
251 this.queue = new Array<GitMonitorQueue> ();
252 StatusIconA.statusicon.pause();
257 public new void resume () {
259 this.queue = new Array<GitMonitorQueue> ();
260 StatusIconA.statusicon.resume();
266 * Start the monitoring
267 * and run the queue every 500 milliseconds..
270 public new void start()
272 StatusIconA.statusicon.refreshing();
275 this.lastAdd = new DateTime.now(new TimeZone.local());
277 Timeout.add_full(Priority.LOW, 500, () => {
278 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
279 // call this.monitor on each of 'top'
280 for(int i = 0; i < this.top.length ; i++) {
282 this.monitor(this.top.index(i) );
284 StatusIconA.statusicon.resume();
291 var notification = new Notify.Notification(
293 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
297 notification.set_timeout(5);
300 print("Error %s\n",e.message);
302 return false; // do not keep doing this..
312 public new void stop() {
313 StatusIconA.statusicon.pause();
318 public new void monitor (string path, int depth = 0)
321 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
324 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
325 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
330 // FIXME - check if repo is flagged as not autocommit..
331 //var repo = imports.Scm.Repo.Repo.get(path);
332 //if (!repo || !repo.autocommit()) {
338 // check if the repo is to be monitored.
339 //print("PATH : " + path);
342 base.monitor(path, depth);
349 * - pulls the items off the queue
350 * (as commands run concurrently and new items may get added while it's running)
351 * - runs the queue items
355 public void runQueue()
361 print("GitMonitor.runQueue\n");
363 this.queueRunning = true;
365 var cmds = new Array<GitMonitorQueue>();
367 for(var i = 0; i < this.queue.length; i++) {
368 cmds.append_val(this.queue.index(i));
371 this.queue = new Array<GitMonitorQueue>();// empty queue!
374 string[] success = {};
375 string[] failure = {};
376 //var repos = new Array<GitRepo>(); //??
377 //var done = new Array<GitMonitorQueue>();
379 // first build a array of repo's to work with
380 var repo_list = new Array<GitRepo>();
384 //print(JSON.stringify(cmds));
385 // make sure nothing get's added to the queue where we are doing this..
389 print("GitMonitor.runQueue - creating repos\n");
391 for(var i = 0; i < cmds.length; i++) {
393 var cmd = cmds.index(i);
395 var gitpath = cmd.gitpath;
396 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
398 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
400 repo_list.append_val(new GitRepo( gitpath ));
401 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
403 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
405 //if (typeof(repo_list[gitpath]) == 'undefined') {
406 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
407 // repo_list[gitpath].cmds = [];
408 // repo_list[gitpath].pull();
410 repo_list.index(ix).cmds.append_val(cmd);
414 // build add, remove and commit message list..
416 print("GitMonitor.runQueue - creating actions\n");
418 for(var i = 0;i < repo_list.length;i++) {
420 var repo = repo_list.index(i);
422 var add_files = new Array<GitMonitorQueue>();
423 var add_files_f = new Array<GitMonitorQueue>();
424 var remove_files = new Array<GitMonitorQueue>();
425 var messages = new Array<GitMonitorQueue>();
426 //print(JSON.stringify(repo.cmds,null,4));
428 for(var ii = 0;ii < repo.cmds.length;ii++) {
429 var cmd = repo.cmds.index(ii);
431 if (repo.is_ignore(cmd.vname)) {
439 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
443 add_files.append_val(cmd);
447 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
451 // if file exists, do not try and delete it.
452 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
456 remove_files.append_val(cmd);
460 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
463 messages.append_val(cmd);
467 stdout.printf("Opps unmatched action %s\n", cmd.action);
471 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
472 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
475 // these can fail... at present... as we wildcard stuff.
477 // make sure added files do not get removed.. ??
479 var remove_files_f = new Array<GitMonitorQueue>();
480 for(var ii = 0;ii < remove_files.length;ii++) {
481 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
484 remove_files_f.append_val(remove_files.index(ii));
486 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
489 // if file was added, then removed,
490 var remove_files_f = new Array<GitMonitorQueue>();
491 for(var ii = 0;ii < remove_files.length;ii++) {
495 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
496 // in add and remove - do not remvove
499 remove_files_f.append_val(remove_files.index(ii));
501 for(var ii = 0;ii < add_files.length;ii++) {
502 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
503 // the add file is in the remove list, and it does not exist - do not add it..
504 print("check exists ? %s\n",add_files.index(ii).fullpath());
506 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
514 add_files_f.append_val(add_files.index(ii));
517 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
518 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
520 if (add_files_f.length < 1 && remove_files_f.length < 1) {
524 // make sure monitoring is paused so it does not recursively pick up
532 this.pauseError(e.message);
533 //failure += e.message;
534 //print("Pull failed:\n");
541 repo.add(add_files_f);
543 this.pauseError(e.message);
545 failure += e.message;
546 print("Add failed:\n");
549 repo.remove(remove_files_f);
551 this.pauseError(e.message);
553 failure += e.message;
554 print("Remove failed:\n");
559 success += repo.commit(
560 GitMonitorQueue.messageToString(messages),
563 success += repo.push();
567 // if the error is 'nothing to commit, working tree clean'
568 // then it's not an error, - just continue;
570 if (/nothing to commit, working tree clean/.match(e.message)) {
571 success += e.message;
578 this.pauseError(e.message);
581 //failure += e.message;
582 //print("Push failed:\n");
589 // finally merge all the commit messages.
592 // catch notification failures.. so we can carry on..
593 if (success.length > 0) {
596 var notification = new Notify.Notification(
598 string.joinv("\n",success),
603 notification.set_timeout(5);
607 //if (failure.length > 0) {
609 // should never get this far...
610 // this.pauseError();
616 this.queueRunning = false;
623 //string[] just_created;
632 public override void onChanged(MonitorNamePathDir src)
634 //print("GitMonitor.onChanged\n");
635 return; // always ignore this..?
636 //this.parsePath(src);
642 * results in git add + git commit..
645 public override void onChangesDoneHint(MonitorNamePathDir src)
651 print("GitMonitor.onChangedHint\n");
653 this.lastAdd = new DateTime.now(new TimeZone.local());
654 var cmd = new GitMonitorQueue(src);
655 if (cmd.shouldIgnore()) {
660 //var add_it = false;
662 if (this.is_just_created(cmd.path)) {
664 if (typeof(this.just_created[src.path]) !='undefined') {
665 delete this.just_created[src.path];
668 [ src.gitpath, 'add', src.vpath ],
669 [ src.gitpath, 'commit', { message: src.vpath} ]
677 this.queue.append_val(cmd);
679 cmd = new GitMonitorQueue(src);
680 cmd.action = "commit";
681 cmd.message = cmd.vname;
682 this.queue.append_val(cmd);
686 public override void onDeleted(MonitorNamePathDir src)
692 print("GitMonitor.onDeleted\n");
693 this.lastAdd = new DateTime.now(new TimeZone.local());
694 var cmd = new GitMonitorQueue(src);
695 if (cmd.shouldIgnore()) {
698 // should check if monitor needs removing..
699 // it should also check if it was a directory.. - so we dont have to commit all..
701 this.queue.append_val(cmd);
703 cmd = new GitMonitorQueue(src);
704 cmd.action = "commit";
705 cmd.message = cmd.vname;
706 cmd.commit_all = true;
708 this.queue.append_val(cmd);
711 public override void onCreated(MonitorNamePathDir src) {
716 print("GitMonitor.onCreated\n");
717 this.lastAdd = new DateTime.now(new TimeZone.local());
718 var cmd = new GitMonitorQueue(src);
719 if (cmd.shouldIgnore()) {
723 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
724 // this.just_created[src.path] = true;
725 return; // we do not handle file create flags... - use done hint.
727 // directory has bee created
728 this.monitor(src.path);
729 //this.top.append_val(src.path);
730 //this.monitor(src.path );
733 // -- no point in adding a dir.. as git does not handle them...
735 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
736 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
742 public override void onAttributeChanged(MonitorNamePathDir src) {
747 print("GitMonitor.onAttributeChanged\n");
748 if (src.dir == GitMonitor.gitlive) {
749 return; // attribute on top level..
752 this.lastAdd = new DateTime.now(new TimeZone.local());
753 var cmd = new GitMonitorQueue(src);
754 if (cmd.shouldIgnore()) {
758 this.queue.append_val(cmd);
760 cmd = new GitMonitorQueue(src);
761 cmd.action = "commit";
762 cmd.message = "Attribute changed " + cmd.vname;
763 this.queue.append_val(cmd);
766 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
772 print("GitMonitor.onMoved\n");
773 this.lastAdd = new DateTime.now(new TimeZone.local());
774 var cmd_s = new GitMonitorQueue(src);
776 var cmd_d = new GitMonitorQueue(dest);
779 if (cmd_d.gitpath != cmd_s.gitpath) {
781 this.onCreated(dest);
782 this.onChangesDoneHint(dest);
785 // needs to handle move to/from unsupported types..
787 if (cmd_s.shouldIgnore()) {
788 this.onCreated(dest);
789 this.onChangesDoneHint(dest);
793 if (cmd_d.shouldIgnore()) {
802 print("RM: %s\n", cmd_s.vname);
804 this.queue.append_val(cmd_s);
808 print("ADD: %s\n", cmd_d.vname);
809 cmd_d.action = "add";
810 this.queue.append_val(cmd_d);
813 var cmd = new GitMonitorQueue(dest);
814 cmd.action = "commit";
815 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
816 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
817 cmd.message = cmd_d.vname;
820 this.queue.append_val(cmd);