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;
181 Timeout.add_full(Priority.LOW, 1000, () => {
183 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
185 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
186 if (this.queue.length < 1 || this.queueRunning) {
190 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
192 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
193 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
195 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
198 //_this.lastAdd = new Date();
208 public new void pauseError(string failure)
211 var notification = new Notify.Notification(
218 notification.set_timeout(60); // show errros for longer
226 this.queueRunning = false;
227 // what does this do to the old one...
228 this.queue = new Array<GitMonitorQueue> ();
230 StatusIconA.statusicon.pauseError();
232 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
233 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
234 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
237 m.set_keep_above(true);
239 m.set_position(Gtk.WindowPosition.CENTER);
240 m.response.connect( (id_pressed) => {
246 public new void pause() {
248 // what does this do to the old one...
249 this.queue = new Array<GitMonitorQueue> ();
250 StatusIconA.statusicon.pause();
255 public new void resume () {
257 this.queue = new Array<GitMonitorQueue> ();
258 StatusIconA.statusicon.resume();
264 * Start the monitoring
265 * and run the queue every 500 milliseconds..
268 public new void start()
270 StatusIconA.statusicon.refreshing();
273 this.lastAdd = new DateTime.now(new TimeZone.local());
275 Timeout.add_full(Priority.LOW, 500, () => {
276 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
277 // call this.monitor on each of 'top'
278 for(int i = 0; i < this.top.length ; i++) {
280 this.monitor(this.top.index(i) );
282 StatusIconA.statusicon.resume();
289 var notification = new Notify.Notification(
291 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
295 notification.set_timeout(5);
298 print("Error %s\n",e.message);
300 return false; // do not keep doing this..
310 public new void stop() {
311 StatusIconA.statusicon.pause();
316 public new void monitor (string path, int depth = 0)
319 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
322 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
323 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
328 // FIXME - check if repo is flagged as not autocommit..
329 //var repo = imports.Scm.Repo.Repo.get(path);
330 //if (!repo || !repo.autocommit()) {
336 // check if the repo is to be monitored.
337 //print("PATH : " + path);
340 base.monitor(path, depth);
347 * - pulls the items off the queue
348 * (as commands run concurrently and new items may get added while it's running)
349 * - runs the queue items
353 public void runQueue()
359 print("GitMonitor.runQueue\n");
361 this.queueRunning = true;
363 var cmds = new Array<GitMonitorQueue>();
365 for(var i = 0; i < this.queue.length; i++) {
366 cmds.append_val(this.queue.index(i));
369 this.queue = new Array<GitMonitorQueue>();// empty queue!
372 string[] success = {};
373 string[] failure = {};
374 //var repos = new Array<GitRepo>(); //??
375 //var done = new Array<GitMonitorQueue>();
377 // first build a array of repo's to work with
378 var repo_list = new Array<GitRepo>();
382 //print(JSON.stringify(cmds));
383 // make sure nothing get's added to the queue where we are doing this..
387 print("GitMonitor.runQueue - creating repos\n");
389 for(var i = 0; i < cmds.length; i++) {
391 var cmd = cmds.index(i);
393 var gitpath = cmd.gitpath;
394 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
396 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
398 repo_list.append_val(new GitRepo( gitpath ));
399 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
401 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
403 //if (typeof(repo_list[gitpath]) == 'undefined') {
404 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
405 // repo_list[gitpath].cmds = [];
406 // repo_list[gitpath].pull();
408 repo_list.index(ix).cmds.append_val(cmd);
412 // build add, remove and commit message list..
414 print("GitMonitor.runQueue - creating actions\n");
416 for(var i = 0;i < repo_list.length;i++) {
418 var repo = repo_list.index(i);
420 var add_files = new Array<GitMonitorQueue>();
421 var add_files_f = new Array<GitMonitorQueue>();
422 var remove_files = new Array<GitMonitorQueue>();
423 var messages = new Array<GitMonitorQueue>();
424 //print(JSON.stringify(repo.cmds,null,4));
426 for(var ii = 0;ii < repo.cmds.length;ii++) {
427 var cmd = repo.cmds.index(ii);
433 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
437 add_files.append_val(cmd);
441 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
445 // if file exists, do not try and delete it.
446 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
450 remove_files.append_val(cmd);
454 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
457 messages.append_val(cmd);
461 stdout.printf("Opps unmatched action %s\n", cmd.action);
465 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
466 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
469 // these can fail... at present... as we wildcard stuff.
471 // make sure added files do not get removed.. ??
473 var remove_files_f = new Array<GitMonitorQueue>();
474 for(var ii = 0;ii < remove_files.length;ii++) {
475 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
478 remove_files_f.append_val(remove_files.index(ii));
480 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
483 // if file was added, then removed,
484 var remove_files_f = new Array<GitMonitorQueue>();
485 for(var ii = 0;ii < remove_files.length;ii++) {
489 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
490 // in add and remove - do not remvove
493 remove_files_f.append_val(remove_files.index(ii));
495 for(var ii = 0;ii < add_files.length;ii++) {
496 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
497 // the add file is in the remove list, and it does not exist - do not add it..
498 print("check exists ? %s\n",add_files.index(ii).fullpath());
500 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
508 add_files_f.append_val(add_files.index(ii));
511 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
512 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
515 // make sure monitoring is paused so it does not recursively pick up
524 this.pauseError(e.message);
525 //failure += e.message;
526 //print("Pull failed:\n");
532 repo.add(add_files_f);
534 this.pauseError(e.message);
536 failure += e.message;
537 print("Add failed:\n");
540 repo.remove(remove_files_f);
542 this.pauseError(e.message);
544 failure += e.message;
545 print("Remove failed:\n");
552 success += repo.commit(
553 GitMonitorQueue.messageToString(messages),
556 success += repo.push();
560 this.pauseError(e.message);
563 //failure += e.message;
564 //print("Push failed:\n");
571 // finally merge all the commit messages.
574 // catch notification failures.. so we can carry on..
575 if (success.length > 0) {
578 var notification = new Notify.Notification(
580 string.joinv("\n",success),
585 notification.set_timeout(5);
589 //if (failure.length > 0) {
591 // should never get this far...
592 // this.pauseError();
598 this.queueRunning = false;
605 //string[] just_created;
614 public override void onChanged(MonitorNamePathDir src)
616 //print("GitMonitor.onChanged\n");
617 return; // always ignore this..?
618 //this.parsePath(src);
624 * results in git add + git commit..
627 public override void onChangesDoneHint(MonitorNamePathDir src)
633 print("GitMonitor.onChangedHint\n");
635 this.lastAdd = new DateTime.now(new TimeZone.local());
636 var cmd = new GitMonitorQueue(src);
637 if (cmd.shouldIgnore()) {
642 //var add_it = false;
644 if (this.is_just_created(cmd.path)) {
646 if (typeof(this.just_created[src.path]) !='undefined') {
647 delete this.just_created[src.path];
650 [ src.gitpath, 'add', src.vpath ],
651 [ src.gitpath, 'commit', { message: src.vpath} ]
659 this.queue.append_val(cmd);
661 cmd = new GitMonitorQueue(src);
662 cmd.action = "commit";
663 cmd.message = cmd.vname;
664 this.queue.append_val(cmd);
668 public override void onDeleted(MonitorNamePathDir src)
674 print("GitMonitor.onDeleted\n");
675 this.lastAdd = new DateTime.now(new TimeZone.local());
676 var cmd = new GitMonitorQueue(src);
677 if (cmd.shouldIgnore()) {
680 // should check if monitor needs removing..
681 // it should also check if it was a directory.. - so we dont have to commit all..
683 this.queue.append_val(cmd);
685 cmd = new GitMonitorQueue(src);
686 cmd.action = "commit";
687 cmd.message = cmd.vname;
688 cmd.commit_all = true;
690 this.queue.append_val(cmd);
693 public override void onCreated(MonitorNamePathDir src) {
698 print("GitMonitor.onCreated\n");
699 this.lastAdd = new DateTime.now(new TimeZone.local());
700 var cmd = new GitMonitorQueue(src);
701 if (cmd.shouldIgnore()) {
705 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
706 // this.just_created[src.path] = true;
707 return; // we do not handle file create flags... - use done hint.
709 // directory has bee created
710 this.monitor(src.path);
711 //this.top.append_val(src.path);
712 //this.monitor(src.path );
715 // -- no point in adding a dir.. as git does not handle them...
717 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
718 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
724 public override void onAttributeChanged(MonitorNamePathDir src) {
729 print("GitMonitor.onAttributeChanged\n");
730 if (src.dir == GitMonitor.gitlive) {
731 return; // attribute on top level..
734 this.lastAdd = new DateTime.now(new TimeZone.local());
735 var cmd = new GitMonitorQueue(src);
736 if (cmd.shouldIgnore()) {
740 this.queue.append_val(cmd);
742 cmd = new GitMonitorQueue(src);
743 cmd.action = "commit";
744 cmd.message = "Attribute changed " + cmd.vname;
745 this.queue.append_val(cmd);
748 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
754 print("GitMonitor.onMoved\n");
755 this.lastAdd = new DateTime.now(new TimeZone.local());
756 var cmd_s = new GitMonitorQueue(src);
758 var cmd_d = new GitMonitorQueue(dest);
761 if (cmd_d.gitpath != cmd_s.gitpath) {
763 this.onCreated(dest);
764 this.onChangesDoneHint(dest);
767 // needs to handle move to/from unsupported types..
769 if (cmd_s.shouldIgnore()) {
770 this.onCreated(dest);
771 this.onChangesDoneHint(dest);
775 if (cmd_d.shouldIgnore()) {
784 print("RM: %s\n", cmd_s.vname);
786 this.queue.append_val(cmd_s);
790 print("ADD: %s\n", cmd_d.vname);
791 cmd_d.action = "add";
792 this.queue.append_val(cmd_d);
795 var cmd = new GitMonitorQueue(dest);
796 cmd.action = "commit";
797 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
798 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
799 cmd.message = cmd_d.vname;
802 this.queue.append_val(cmd);