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;
182 public GitMonitor () {
183 this.queue = new Array<GitMonitorQueue>();
184 GitMonitor.gitmonitor = this;
186 Timeout.add_full(Priority.LOW, 1000, () => {
188 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
190 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
191 if (this.queue.length < 1 || this.queueRunning) {
195 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
197 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
198 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
200 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
203 //_this.lastAdd = new Date();
213 public new void pauseError(string failure)
216 var notification = new Notify.Notification(
223 notification.set_timeout(60); // show errros for longer
231 this.queueRunning = false;
232 // what does this do to the old one...
233 this.queue = new Array<GitMonitorQueue> ();
235 StatusIconA.statusicon.pauseError();
237 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
238 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
239 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
242 m.set_keep_above(true);
244 m.set_position(Gtk.WindowPosition.CENTER);
245 m.response.connect( (id_pressed) => {
251 public new void pause() {
253 // what does this do to the old one...
254 this.queue = new Array<GitMonitorQueue> ();
255 StatusIconA.statusicon.pause();
260 public new void resume () {
262 this.queue = new Array<GitMonitorQueue> ();
263 StatusIconA.statusicon.resume();
269 * Start the monitoring
270 * and run the queue every 500 milliseconds..
273 public new void start()
275 StatusIconA.statusicon.refreshing();
278 this.lastAdd = new DateTime.now(new TimeZone.local());
280 Timeout.add_full(Priority.LOW, 500, () => {
281 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
282 // call this.monitor on each of 'top'
283 for(int i = 0; i < this.top.length ; i++) {
285 this.monitor(this.top.index(i) );
287 StatusIconA.statusicon.resume();
294 var notification = new Notify.Notification(
296 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
300 notification.set_timeout(5);
303 GLib.debug("Error %s\n",e.message);
305 return false; // do not keep doing this..
315 public new void stop() {
316 StatusIconA.statusicon.pause();
321 public new void monitor (string path, int depth = 0)
324 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
327 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
328 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
333 // FIXME - check if repo is flagged as not autocommit..
334 //var repo = imports.Scm.Repo.Repo.get(path);
335 //if (!repo || !repo.autocommit()) {
341 // check if the repo is to be monitored.
342 //print("PATH : " + path);
345 base.monitor(path, depth);
352 * - pulls the items off the queue
353 * (as commands run concurrently and new items may get added while it's running)
354 * - runs the queue items
358 public void runQueue()
364 GLib.debug("GitMonitor.runQueue\n");
366 this.queueRunning = true;
368 var cmds = new Array<GitMonitorQueue>();
370 for(var i = 0; i < this.queue.length; i++) {
371 cmds.append_val(this.queue.index(i));
374 this.queue = new Array<GitMonitorQueue>();// empty queue!
377 string[] success = {};
378 string[] failure = {};
379 //var repos = new Array<GitRepo>(); //??
380 //var done = new Array<GitMonitorQueue>();
382 // first build a array of repo's to work with
383 var repo_list = new Array<GitRepo>();
387 //print(JSON.stringify(cmds));
388 // make sure nothing get's added to the queue where we are doing this..
392 GLib.debug("GitMonitor.runQueue - creating repos\n");
394 for(var i = 0; i < cmds.length; i++) {
396 var cmd = cmds.index(i);
398 var gitpath = cmd.gitpath;
399 GLib.debug("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
401 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
403 repo_list.append_val(new GitRepo( gitpath ));
404 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
406 GLib.debug("GitMonitor.runQueue - adding to repolist %d\n", ix);
408 //if (typeof(repo_list[gitpath]) == 'undefined') {
409 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
410 // repo_list[gitpath].cmds = [];
411 // repo_list[gitpath].pull();
413 repo_list.index(ix).cmds.append_val(cmd);
417 // build add, remove and commit message list..
419 GLib.debug("GitMonitor.runQueue - creating actions\n");
421 for(var i = 0;i < repo_list.length;i++) {
423 var repo = repo_list.index(i);
425 var add_files = new Array<GitMonitorQueue>();
426 var add_files_f = new Array<GitMonitorQueue>();
427 var remove_files = new Array<GitMonitorQueue>();
428 var messages = new Array<GitMonitorQueue>();
429 //print(JSON.stringify(repo.cmds,null,4));
431 for(var ii = 0;ii < repo.cmds.length;ii++) {
432 var cmd = repo.cmds.index(ii);
434 if (repo.is_ignore(cmd.vname)) {
442 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
446 add_files.append_val(cmd);
450 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
454 // if file exists, do not try and delete it.
455 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
459 remove_files.append_val(cmd);
463 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
466 messages.append_val(cmd);
470 stdout.printf("Opps unmatched action %s\n", cmd.action);
474 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
475 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
478 // these can fail... at present... as we wildcard stuff.
480 // make sure added files do not get removed.. ??
482 var remove_files_f = new Array<GitMonitorQueue>();
483 for(var ii = 0;ii < remove_files.length;ii++) {
484 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
487 remove_files_f.append_val(remove_files.index(ii));
489 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
492 // if file was added, then removed,
493 var remove_files_f = new Array<GitMonitorQueue>();
494 for(var ii = 0;ii < remove_files.length;ii++) {
498 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
499 // in add and remove - do not remvove
502 remove_files_f.append_val(remove_files.index(ii));
504 for(var ii = 0;ii < add_files.length;ii++) {
505 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
506 // the add file is in the remove list, and it does not exist - do not add it..
507 print("check exists ? %s\n",add_files.index(ii).fullpath());
509 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
517 add_files_f.append_val(add_files.index(ii));
520 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
521 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
523 if (add_files_f.length < 1 && remove_files_f.length < 1) {
527 // make sure monitoring is paused so it does not recursively pick up
536 this.pauseError(e.message);
537 //failure += e.message;
538 //print("Pull failed:\n");
544 repo.add(add_files_f);
546 this.pauseError(e.message);
548 failure += e.message;
549 GLib.debug("Add failed:\n");
552 repo.remove(remove_files_f);
554 this.pauseError(e.message);
556 failure += e.message;
557 GLib.debug("Remove failed:\n");
562 success += repo.commit(
563 GitMonitorQueue.messageToString(messages),
566 success += repo.push();
570 // if the error is 'nothing to commit, working tree clean'
571 // then it's not an error, - just continue;
572 if (/nothing to commit, working tree clean/.match(e.message)) {
573 GLib.debug("%s\n",e.message);
574 success += e.message;
582 this.pauseError(e.message);
585 //failure += e.message;
586 //print("Push failed:\n");
593 // finally merge all the commit messages.
596 // catch notification failures.. so we can carry on..
597 if (success.length > 0) {
600 var notification = new Notify.Notification(
602 string.joinv("\n",success),
607 notification.set_timeout(5);
611 //if (failure.length > 0) {
613 // should never get this far...
614 // this.pauseError();
617 GLib.debug(e.message);
620 this.queueRunning = false;
627 //string[] just_created;
636 public override void onChanged(MonitorNamePathDir src)
638 //print("GitMonitor.onChanged\n");
639 return; // always ignore this..?
640 //this.parsePath(src);
646 * results in git add + git commit..
649 public override void onChangesDoneHint(MonitorNamePathDir src)
655 GLib.debug("GitMonitor.onChangedHint\n");
657 this.lastAdd = new DateTime.now(new TimeZone.local());
658 var cmd = new GitMonitorQueue(src);
659 if (cmd.shouldIgnore()) {
664 //var add_it = false;
666 if (this.is_just_created(cmd.path)) {
668 if (typeof(this.just_created[src.path]) !='undefined') {
669 delete this.just_created[src.path];
672 [ src.gitpath, 'add', src.vpath ],
673 [ src.gitpath, 'commit', { message: src.vpath} ]
681 this.queue.append_val(cmd);
683 cmd = new GitMonitorQueue(src);
684 cmd.action = "commit";
685 cmd.message = cmd.vname;
686 this.queue.append_val(cmd);
690 public override void onDeleted(MonitorNamePathDir src)
696 GLib.debug("GitMonitor.onDeleted\n");
697 this.lastAdd = new DateTime.now(new TimeZone.local());
698 var cmd = new GitMonitorQueue(src);
699 if (cmd.shouldIgnore()) {
702 // should check if monitor needs removing..
703 // it should also check if it was a directory.. - so we dont have to commit all..
705 this.queue.append_val(cmd);
707 cmd = new GitMonitorQueue(src);
708 cmd.action = "commit";
709 cmd.message = cmd.vname;
710 cmd.commit_all = true;
712 this.queue.append_val(cmd);
715 public override void onCreated(MonitorNamePathDir src) {
720 GLib.debug("GitMonitor.onCreated\n");
721 this.lastAdd = new DateTime.now(new TimeZone.local());
722 var cmd = new GitMonitorQueue(src);
723 if (cmd.shouldIgnore()) {
727 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
728 // this.just_created[src.path] = true;
729 return; // we do not handle file create flags... - use done hint.
731 // directory has bee created
732 this.monitor(src.path);
733 //this.top.append_val(src.path);
734 //this.monitor(src.path );
737 // -- no point in adding a dir.. as git does not handle them...
739 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
740 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
746 public override void onAttributeChanged(MonitorNamePathDir src) {
751 GLib.debug("GitMonitor.onAttributeChanged\n");
752 if (src.dir == GitMonitor.gitlive) {
753 return; // attribute on top level..
756 this.lastAdd = new DateTime.now(new TimeZone.local());
757 var cmd = new GitMonitorQueue(src);
758 if (cmd.shouldIgnore()) {
762 this.queue.append_val(cmd);
764 cmd = new GitMonitorQueue(src);
765 cmd.action = "commit";
766 cmd.message = "Attribute changed " + cmd.vname;
767 this.queue.append_val(cmd);
770 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
776 GLib.debug("GitMonitor.onMoved\n");
777 this.lastAdd = new DateTime.now(new TimeZone.local());
778 var cmd_s = new GitMonitorQueue(src);
780 var cmd_d = new GitMonitorQueue(dest);
783 if (cmd_d.gitpath != cmd_s.gitpath) {
785 this.onCreated(dest);
786 this.onChangesDoneHint(dest);
789 // needs to handle move to/from unsupported types..
791 if (cmd_s.shouldIgnore()) {
792 this.onCreated(dest);
793 this.onChangesDoneHint(dest);
797 if (cmd_d.shouldIgnore()) {
806 GLib.debug("RM: %s\n", cmd_s.vname);
808 this.queue.append_val(cmd_s);
812 GLib.debug("ADD: %s\n", cmd_d.vname);
813 cmd_d.action = "add";
814 this.queue.append_val(cmd_d);
817 var cmd = new GitMonitorQueue(dest);
818 cmd.action = "commit";
819 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
820 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
821 cmd.message = cmd_d.vname;
824 this.queue.append_val(cmd);