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") {
72 if (this.name == ".gitignore") {
79 if (this.name[this.name.length -1] == '~') {
82 // netbeans / android studio.. silly temp files..
84 if (Regex.match_simple("___jb_old___$", this.name)) {
87 if (Regex.match_simple("___jb_bak___$", this.name)) {
90 //if (f.name.match(/^nbproject/)) {
93 // ignore anything in top level!!!!
94 if (this.gitpath.length < 1) {
103 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
105 for(var i =0; i < add_files.length; i++) {
106 if (add_files.index(i).vname == add) {
112 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
113 for(var i =0; i < messages.length; i++) {
114 if (messages.index(i).message == message) {
120 public static string messageToString(Array<GitMonitorQueue> messages ) {
122 for(var i =0; i < messages.length; i++) {
123 ret+= messages.index(i).message;
125 return string.joinv("\n",ret);
127 public static string queueArrayToString(Array<GitMonitorQueue> list) {
129 for(var i =0; i < list.length; i++) {
131 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
137 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
138 for(var i =0; i < list.length; i++) {
139 var test = list.index(i);
140 if (list.index(i).gitpath != cmd_s.gitpath) {
143 if (list.index(i).vname != cmd_s.vname) {
146 if (list.index(i).action != action) {
153 public string fullpath()
155 return this.gitpath + "/" + this.vname;
164 public class GitMonitor : Monitor
167 public static GitMonitor gitmonitor;
169 * @property {String} the "gitlive" directory, normally ~/gitlive
170 * dset by OWNER... - we should do this as a CTOR.
173 public static string gitlive;
176 public Array<GitMonitorQueue> queue ;
177 public bool queueRunning = false;
179 public DateTime lastAdd;
181 public GitMonitor () {
182 this.queue = new Array<GitMonitorQueue>();
183 GitMonitor.gitmonitor = this;
185 Timeout.add_full(Priority.LOW, 1000, () => {
187 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
189 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
190 if (this.queue.length < 1 || this.queueRunning) {
194 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
196 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
197 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
199 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
202 //_this.lastAdd = new Date();
212 public new void pauseError(string failure)
215 var notification = new Notify.Notification(
222 notification.set_timeout(60); // show errros for longer
230 this.queueRunning = false;
231 // what does this do to the old one...
232 this.queue = new Array<GitMonitorQueue> ();
234 StatusIconA.statusicon.pauseError();
236 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
237 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
238 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
241 m.set_keep_above(true);
243 m.set_position(Gtk.WindowPosition.CENTER);
244 m.response.connect( (id_pressed) => {
250 public new void pause() {
252 // what does this do to the old one...
253 this.queue = new Array<GitMonitorQueue> ();
254 StatusIconA.statusicon.pause();
259 public new void resume () {
261 this.queue = new Array<GitMonitorQueue> ();
262 StatusIconA.statusicon.resume();
268 * Start the monitoring
269 * and run the queue every 500 milliseconds..
272 public new void start()
274 StatusIconA.statusicon.refreshing();
277 this.lastAdd = new DateTime.now(new TimeZone.local());
279 Timeout.add_full(Priority.LOW, 500, () => {
280 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
281 // call this.monitor on each of 'top'
282 for(int i = 0; i < this.top.length ; i++) {
284 this.monitor(this.top.index(i) );
286 StatusIconA.statusicon.resume();
293 var notification = new Notify.Notification(
295 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
299 notification.set_timeout(5);
302 GLib.debug("Error sending notification to screen: %s\n",e.message);
304 return false; // do not keep doing this..
314 public new void stop() {
315 StatusIconA.statusicon.pause();
320 public new void monitor (string path, int depth = 0)
323 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
326 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
327 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
332 // FIXME - check if repo is flagged as not autocommit..
333 //var repo = imports.Scm.Repo.Repo.get(path);
334 //if (!repo || !repo.autocommit()) {
340 // check if the repo is to be monitored.
341 //print("PATH : " + path);
344 base.monitor(path, depth);
351 * - pulls the items off the queue
352 * (as commands run concurrently and new items may get added while it's running)
353 * - runs the queue items
357 public void runQueue()
363 GLib.debug("GitMonitor.runQueue\n");
365 this.queueRunning = true;
367 var cmds = new Array<GitMonitorQueue>();
369 for(var i = 0; i < this.queue.length; i++) {
370 cmds.append_val(this.queue.index(i));
373 this.queue = new Array<GitMonitorQueue>();// empty queue!
376 string[] success = {};
377 string[] failure = {};
378 //var repos = new Array<GitRepo>(); //??
379 //var done = new Array<GitMonitorQueue>();
381 // first build a array of repo's to work with
382 var repo_list = new Array<GitRepo>();
386 //print(JSON.stringify(cmds));
387 // make sure nothing get's added to the queue where we are doing this..
391 GLib.debug("GitMonitor.runQueue - creating repos\n");
393 for(var i = 0; i < cmds.length; i++) {
395 var cmd = cmds.index(i);
397 var gitpath = cmd.gitpath;
398 GLib.debug("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
400 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
402 repo_list.append_val(new GitRepo( gitpath ));
403 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
405 GLib.debug("GitMonitor.runQueue - adding to repolist %d\n", ix);
407 //if (typeof(repo_list[gitpath]) == 'undefined') {
408 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
409 // repo_list[gitpath].cmds = [];
410 // repo_list[gitpath].pull();
412 repo_list.index(ix).cmds.append_val(cmd);
416 // build add, remove and commit message list..
418 GLib.debug("GitMonitor.runQueue - creating actions\n");
420 for(var i = 0;i < repo_list.length;i++) {
422 var repo = repo_list.index(i);
424 var add_files = new Array<GitMonitorQueue>();
425 var add_files_f = new Array<GitMonitorQueue>();
426 var remove_files = new Array<GitMonitorQueue>();
427 var messages = new Array<GitMonitorQueue>();
428 //print(JSON.stringify(repo.cmds,null,4));
430 for(var ii = 0;ii < repo.cmds.length;ii++) {
431 var cmd = repo.cmds.index(ii);
433 if (repo.is_ignore(cmd.vname)) {
441 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
445 add_files.append_val(cmd);
449 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
453 // if file exists, do not try and delete it.
454 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
458 remove_files.append_val(cmd);
462 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
465 messages.append_val(cmd);
469 stdout.printf("Opps unmatched action %s\n", cmd.action);
473 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
474 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
477 // these can fail... at present... as we wildcard stuff.
479 // make sure added files do not get removed.. ??
481 var remove_files_f = new Array<GitMonitorQueue>();
482 for(var ii = 0;ii < remove_files.length;ii++) {
483 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
486 remove_files_f.append_val(remove_files.index(ii));
488 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
491 // if file was added, then removed,
492 var remove_files_f = new Array<GitMonitorQueue>();
493 for(var ii = 0;ii < remove_files.length;ii++) {
497 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
498 // in add and remove - do not remvove
501 remove_files_f.append_val(remove_files.index(ii));
503 for(var ii = 0;ii < add_files.length;ii++) {
504 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
505 // the add file is in the remove list, and it does not exist - do not add it..
506 print("check exists ? %s\n",add_files.index(ii).fullpath());
508 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
516 add_files_f.append_val(add_files.index(ii));
519 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
520 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
522 if (add_files_f.length < 1 && remove_files_f.length < 1) {
526 // make sure monitoring is paused so it does not recursively pick up
535 this.pauseError(e.message);
536 //failure += e.message;
537 //print("Pull failed:\n");
543 repo.add(add_files_f);
545 this.pauseError(e.message);
547 failure += e.message;
548 GLib.debug("Add failed:\n");
551 repo.remove(remove_files_f);
553 this.pauseError(e.message);
555 failure += e.message;
556 GLib.debug("Remove failed:\n");
561 success += repo.commit(
562 GitMonitorQueue.messageToString(messages),
565 success += repo.push();
569 // if the error is 'nothing to commit, working tree clean'
570 // then it's not an error, - just continue;
571 if (/nothing to commit, working tree clean/.match(e.message)) {
572 GLib.debug("%s\n",e.message);
573 success += e.message;
581 this.pauseError(e.message);
584 //failure += e.message;
585 //print("Push failed:\n");
592 // finally merge all the commit messages.
595 // catch notification failures.. so we can carry on..
596 if (success.length > 0) {
599 var notification = new Notify.Notification(
601 string.joinv("\n",success),
606 notification.set_timeout(5);
610 //if (failure.length > 0) {
612 // should never get this far...
613 // this.pauseError();
616 GLib.debug(e.message);
619 this.queueRunning = false;
626 //string[] just_created;
635 public override void onChanged(MonitorNamePathDir src)
637 //print("GitMonitor.onChanged\n");
638 return; // always ignore this..?
639 //this.parsePath(src);
645 * results in git add + git commit..
648 public override void onChangesDoneHint(MonitorNamePathDir src)
654 GLib.debug("GitMonitor.onChangedHint\n");
656 this.lastAdd = new DateTime.now(new TimeZone.local());
657 var cmd = new GitMonitorQueue(src);
658 if (cmd.shouldIgnore()) {
663 //var add_it = false;
665 if (this.is_just_created(cmd.path)) {
667 if (typeof(this.just_created[src.path]) !='undefined') {
668 delete this.just_created[src.path];
671 [ src.gitpath, 'add', src.vpath ],
672 [ src.gitpath, 'commit', { message: src.vpath} ]
680 this.queue.append_val(cmd);
682 cmd = new GitMonitorQueue(src);
683 cmd.action = "commit";
684 cmd.message = cmd.vname;
685 this.queue.append_val(cmd);
689 public override void onDeleted(MonitorNamePathDir src)
695 GLib.debug("GitMonitor.onDeleted\n");
696 this.lastAdd = new DateTime.now(new TimeZone.local());
697 var cmd = new GitMonitorQueue(src);
698 if (cmd.shouldIgnore()) {
701 // should check if monitor needs removing..
702 // it should also check if it was a directory.. - so we dont have to commit all..
704 this.queue.append_val(cmd);
706 cmd = new GitMonitorQueue(src);
707 cmd.action = "commit";
708 cmd.message = cmd.vname;
709 cmd.commit_all = true;
711 this.queue.append_val(cmd);
714 public override void onCreated(MonitorNamePathDir src) {
719 GLib.debug("GitMonitor.onCreated\n");
720 this.lastAdd = new DateTime.now(new TimeZone.local());
721 var cmd = new GitMonitorQueue(src);
722 if (cmd.shouldIgnore()) {
726 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
727 // this.just_created[src.path] = true;
728 return; // we do not handle file create flags... - use done hint.
730 // directory has bee created
731 this.monitor(src.path);
732 //this.top.append_val(src.path);
733 //this.monitor(src.path );
736 // -- no point in adding a dir.. as git does not handle them...
738 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
739 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
745 public override void onAttributeChanged(MonitorNamePathDir src) {
750 GLib.debug("GitMonitor.onAttributeChanged\n");
751 if (src.dir == GitMonitor.gitlive) {
752 return; // attribute on top level..
755 this.lastAdd = new DateTime.now(new TimeZone.local());
756 var cmd = new GitMonitorQueue(src);
757 if (cmd.shouldIgnore()) {
761 this.queue.append_val(cmd);
763 cmd = new GitMonitorQueue(src);
764 cmd.action = "commit";
765 cmd.message = "Attribute changed " + cmd.vname;
766 this.queue.append_val(cmd);
769 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
775 GLib.debug("GitMonitor.onMoved\n");
776 this.lastAdd = new DateTime.now(new TimeZone.local());
777 var cmd_s = new GitMonitorQueue(src);
779 var cmd_d = new GitMonitorQueue(dest);
782 if (cmd_d.gitpath != cmd_s.gitpath) {
784 this.onCreated(dest);
785 this.onChangesDoneHint(dest);
788 // needs to handle move to/from unsupported types..
790 if (cmd_s.shouldIgnore()) {
791 this.onCreated(dest);
792 this.onChangesDoneHint(dest);
796 if (cmd_d.shouldIgnore()) {
805 GLib.debug("RM: %s\n", cmd_s.vname);
807 this.queue.append_val(cmd_s);
811 GLib.debug("ADD: %s\n", cmd_d.vname);
812 cmd_d.action = "add";
813 this.queue.append_val(cmd_d);
816 var cmd = new GitMonitorQueue(dest);
817 cmd.action = "commit";
818 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
819 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
820 cmd.message = cmd_d.vname;
823 this.queue.append_val(cmd);