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;
48 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
49 "gitpath: %s\nvdir: %s\nvname: %s\n",
50 this.name, this.path, this.dir,
51 this.gitpath, this.vdir, this.vname
55 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
60 public bool shouldIgnore()
64 // vim.. what a seriously brain dead program..
65 if (this.name == "4913") {
69 if (this.name[0] == '.') {
71 if (this.name == ".htaccess") {
78 if (this.name[this.name.length -1] == '~') {
81 // netbeans / android studio.. silly temp files..
83 if (Regex.match_simple("___jb_old___$", this.name)) {
86 if (Regex.match_simple("___jb_bak___$", this.name)) {
89 //if (f.name.match(/^nbproject/)) {
92 // ignore anything in top level!!!!
93 if (this.gitpath.length < 1) {
102 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
104 for(var i =0; i < add_files.length; i++) {
105 if (add_files.index(i).vname == add) {
111 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
112 for(var i =0; i < messages.length; i++) {
113 if (messages.index(i).message == message) {
119 public static string messageToString(Array<GitMonitorQueue> messages ) {
121 for(var i =0; i < messages.length; i++) {
122 ret+= messages.index(i).message;
124 return string.joinv("\n",ret);
126 public static string queueArrayToString(Array<GitMonitorQueue> list) {
128 for(var i =0; i < list.length; i++) {
130 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
136 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
137 for(var i =0; i < list.length; i++) {
138 var test = list.index(i);
139 if (list.index(i).gitpath != cmd_s.gitpath) {
142 if (list.index(i).vname != cmd_s.vname) {
145 if (list.index(i).action != action) {
152 public string fullpath()
154 return this.gitpath + "/" + this.vname;
163 public class GitMonitor : Monitor
166 public static GitMonitor gitmonitor;
168 * @property {String} the "gitlive" directory, normally ~/gitlive
169 * dset by OWNER... - we should do this as a CTOR.
172 public static string gitlive;
175 public Array<GitMonitorQueue> queue ;
176 public bool queueRunning = false;
178 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 print("Error %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 print("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 print("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 stdout.printf("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 stdout.printf("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 print("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 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
474 print( "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 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
520 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
522 // make sure monitoring is paused so it does not recursively pick up
531 this.pauseError(e.message);
532 //failure += e.message;
533 //print("Pull failed:\n");
539 repo.add(add_files_f);
541 this.pauseError(e.message);
543 failure += e.message;
544 print("Add failed:\n");
547 repo.remove(remove_files_f);
549 this.pauseError(e.message);
551 failure += e.message;
552 print("Remove failed:\n");
557 success += repo.commit(
558 GitMonitorQueue.messageToString(messages),
561 success += repo.push();
565 // if the error is 'nothing to commit, working tree clean'
566 // then it's not an error, - just continue;
568 if (/nothing to commit, working tree clean/.match(e.message)) {
569 success += e.message;
576 this.pauseError(e.message);
579 //failure += e.message;
580 //print("Push failed:\n");
587 // finally merge all the commit messages.
590 // catch notification failures.. so we can carry on..
591 if (success.length > 0) {
594 var notification = new Notify.Notification(
596 string.joinv("\n",success),
601 notification.set_timeout(5);
605 //if (failure.length > 0) {
607 // should never get this far...
608 // this.pauseError();
614 this.queueRunning = false;
621 //string[] just_created;
630 public override void onChanged(MonitorNamePathDir src)
632 //print("GitMonitor.onChanged\n");
633 return; // always ignore this..?
634 //this.parsePath(src);
640 * results in git add + git commit..
643 public override void onChangesDoneHint(MonitorNamePathDir src)
649 print("GitMonitor.onChangedHint\n");
651 this.lastAdd = new DateTime.now(new TimeZone.local());
652 var cmd = new GitMonitorQueue(src);
653 if (cmd.shouldIgnore()) {
658 //var add_it = false;
660 if (this.is_just_created(cmd.path)) {
662 if (typeof(this.just_created[src.path]) !='undefined') {
663 delete this.just_created[src.path];
666 [ src.gitpath, 'add', src.vpath ],
667 [ src.gitpath, 'commit', { message: src.vpath} ]
675 this.queue.append_val(cmd);
677 cmd = new GitMonitorQueue(src);
678 cmd.action = "commit";
679 cmd.message = cmd.vname;
680 this.queue.append_val(cmd);
684 public override void onDeleted(MonitorNamePathDir src)
690 print("GitMonitor.onDeleted\n");
691 this.lastAdd = new DateTime.now(new TimeZone.local());
692 var cmd = new GitMonitorQueue(src);
693 if (cmd.shouldIgnore()) {
696 // should check if monitor needs removing..
697 // it should also check if it was a directory.. - so we dont have to commit all..
699 this.queue.append_val(cmd);
701 cmd = new GitMonitorQueue(src);
702 cmd.action = "commit";
703 cmd.message = cmd.vname;
704 cmd.commit_all = true;
706 this.queue.append_val(cmd);
709 public override void onCreated(MonitorNamePathDir src) {
714 print("GitMonitor.onCreated\n");
715 this.lastAdd = new DateTime.now(new TimeZone.local());
716 var cmd = new GitMonitorQueue(src);
717 if (cmd.shouldIgnore()) {
721 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
722 // this.just_created[src.path] = true;
723 return; // we do not handle file create flags... - use done hint.
725 // directory has bee created
726 this.monitor(src.path);
727 //this.top.append_val(src.path);
728 //this.monitor(src.path );
731 // -- no point in adding a dir.. as git does not handle them...
733 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
734 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
740 public override void onAttributeChanged(MonitorNamePathDir src) {
745 print("GitMonitor.onAttributeChanged\n");
746 if (src.dir == GitMonitor.gitlive) {
747 return; // attribute on top level..
750 this.lastAdd = new DateTime.now(new TimeZone.local());
751 var cmd = new GitMonitorQueue(src);
752 if (cmd.shouldIgnore()) {
756 this.queue.append_val(cmd);
758 cmd = new GitMonitorQueue(src);
759 cmd.action = "commit";
760 cmd.message = "Attribute changed " + cmd.vname;
761 this.queue.append_val(cmd);
764 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
770 print("GitMonitor.onMoved\n");
771 this.lastAdd = new DateTime.now(new TimeZone.local());
772 var cmd_s = new GitMonitorQueue(src);
774 var cmd_d = new GitMonitorQueue(dest);
777 if (cmd_d.gitpath != cmd_s.gitpath) {
779 this.onCreated(dest);
780 this.onChangesDoneHint(dest);
783 // needs to handle move to/from unsupported types..
785 if (cmd_s.shouldIgnore()) {
786 this.onCreated(dest);
787 this.onChangesDoneHint(dest);
791 if (cmd_d.shouldIgnore()) {
800 print("RM: %s\n", cmd_s.vname);
802 this.queue.append_val(cmd_s);
806 print("ADD: %s\n", cmd_d.vname);
807 cmd_d.action = "add";
808 this.queue.append_val(cmd_d);
811 var cmd = new GitMonitorQueue(dest);
812 cmd.action = "commit";
813 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
814 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
815 cmd.message = cmd_d.vname;
818 this.queue.append_val(cmd);