3 The monitor suffers from various issues - basically event flows while it's running...
7 - monitors for file changes
8 -- adds to QUEUE when occurs.
10 - queue runs in background.
11 - if it's got stuff in it..
13 - now?? - if on master - try and branch
14 == do user selection, until we have branched
15 == then start monitoring again..
25 public class GitMonitor : Monitor
28 public static GitMonitor gitmonitor;
30 * @property {String} the "gitlive" directory, normally ~/gitlive
31 * dset by OWNER... - we should do this as a CTOR.
34 public static string gitlive;
37 private Gee.ArrayList<GitMonitorQueue> queue ;
38 public bool queueRunning = false;
40 public DateTime lastAdd;
47 this.queue = new Gee.ArrayList<GitMonitorQueue>();
48 GitMonitor.gitmonitor = this;
49 Timeout.add_full(Priority.LOW, 500, () => {
51 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
53 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
54 if (this.queue.size < 1 || this.queueRunning) {
57 var first = this.queue.get(0);
60 // eg. on master.... and is_auto branch
61 if (first.repo.is_master_branch() && first.repo.is_auto_branch()) {
66 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
68 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
69 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
71 if (delay && last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
74 //_this.lastAdd = new Date();
83 public new void pauseError(string failure)
86 var notification = new Notify.Notification(
93 notification.set_timeout(60); // show errros for longer
96 Canberra.Context context;
97 Canberra.Proplist props;
99 Canberra.Context.create (out context);
100 Canberra.Proplist.create (out props);
102 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
103 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
106 context.play_full (0, props, null);
112 this.queueRunning = false;
113 // what does this do to the old one...
114 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
116 StatusIconA.statusicon.pauseError();
118 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
119 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
120 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
123 m.set_keep_above(true);
125 m.set_position(Gtk.WindowPosition.CENTER);
126 m.response.connect( (id_pressed) => {
132 public new void pause() {
134 // what does this do to the old one...
135 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
136 StatusIconA.statusicon.pause();
140 public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
142 //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
147 public new void resume () {
149 this.queue = new Array<GitMonitorQueue> ();
150 StatusIconA.statusicon.resume();
156 * Start the monitoring
157 * and run the queue every 500 milliseconds..
160 public new void start()
162 StatusIconA.statusicon.refreshing();
165 this.lastAdd = new DateTime.now(new TimeZone.local());
167 Timeout.add_full(Priority.LOW, 500, () => {
168 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
169 // call this.monitor on each of 'top'
170 for(int i = 0; i < this.top.length ; i++) {
172 this.monitor(this.top.index(i) );
176 return false; // do not keep doing this..
185 int scancomplete = 0;
186 public void updateMonitorsComplete()
188 StatusIconA.statusicon.resume();
195 var notification = new Notify.Notification(
197 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
201 notification.set_timeout(5);
204 GLib.debug("Error sending notification to screen: %s",e.message);
208 public new void stop() {
209 StatusIconA.statusicon.pause();
214 // this is called recursively until we end..
216 public override void monitor (string path, int depth = 0)
219 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
220 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
223 this.scancomplete = 0;
227 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
228 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
234 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
235 return; // skip non-git directories..
238 GitRepo.get.begin(path, (ojb,res) => {
240 GitRepo.get.end(res);
241 base.monitor(path, depth);
243 if (this.scanning == 0 && this.scancomplete == 1) {
244 this.updateMonitorsComplete();
248 // FIXME - check if repo is flagged as not autocommit..
249 //var repo = imports.Scm.Repo.Repo.get(path);
250 //if (!repo || !repo.autocommit()) {
256 // check if the repo is to be monitored.
257 //print("PATH : " + path);
260 base.monitor(path, depth);
262 this.scancomplete = 1;
272 * - pulls the items off the queue
273 * (as commands run concurrently and new items may get added while it's running)
274 * - runs the queue items
278 public void runQueue()
281 if (this.paused || this.queue.size < 1 ) {
285 foreach(var q in this.queue) {
286 if (!q.shouldIgnore() && q.repo.is_master_branch() && q.repo.is_auto_branch()) {
287 var oldq = this.queue;
288 this.queue = new Gee.ArrayList<GitMonitorQueue>();
289 NewBranch.singleton().show(q.repo, oldq);
298 GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
300 this.queueRunning = true;
302 var cmds = new Gee.ArrayList<GitMonitorQueue>();
304 for(var i = 0; i < this.queue.size; i++) {
305 cmds.add(this.queue.get(i));
308 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
310 string[] success = {};
311 string[] failure = {};
312 //var repos = new Array<GitRepo>(); //??
313 //var done = new Array<GitMonitorQueue>();
315 // first build a array of repo's to work with
316 var repo_list = new Array<GitRepo>();
320 //print(JSON.stringify(cmds));
321 // make sure nothing get's added to the queue where we are doing this..
325 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
326 GLib.debug("GitMonitor.runQueue - creating repos");
328 for(var i = 0; i < cmds.size; i++) {
330 var cmd = cmds.get(i);
332 var gitpath = cmd.gitpath;
334 var repo = GitRepo.get_sync( gitpath );
335 if ( repo.is_master_branch() && repo.is_auto_branch()) {
336 leave_queued.add(cmd);
340 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
342 var ix = GitRepo.indexOf(repo_list, gitpath);
344 repo_list.append_val( GitRepo.get_sync( gitpath ));
345 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
347 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
349 //if (typeof(repo_list[gitpath]) == 'undefined') {
350 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
351 // repo_list[gitpath].cmds = [];
352 // repo_list[gitpath].pull();
354 repo_list.index(ix).cmds.add(cmd);
357 this.queue = leave_queued;
360 // build add, remove and commit message list..
362 GLib.debug("GitMonitor.runQueue - creating actions");
364 for(var i = 0;i < repo_list.length;i++) {
366 var repo = repo_list.index(i);
368 var add_files = new Gee.ArrayList<GitMonitorQueue>();
369 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
370 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
371 var messages = new Gee.ArrayList<GitMonitorQueue>();
372 //print(JSON.stringify(repo.cmds,null,4));
374 for(var ii = 0;ii < repo.cmds.size;ii++) {
375 var cmd = repo.cmds.get(ii);
377 if (repo.is_ignore(cmd.vname)) {
385 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
393 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
397 // if file exists, do not try and delete it.
398 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
402 remove_files.add(cmd);
406 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
413 stdout.printf("Opps unmatched action %s\n", cmd.action);
418 repo.cmds.clear(); // reset the repo's command list..
420 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
421 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
424 // these can fail... at present... as we wildcard stuff.
426 // make sure added files do not get removed.. ??
428 var remove_files_f = new Array<GitMonitorQueue>();
429 for(var ii = 0;ii < remove_files.length;ii++) {
430 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
433 remove_files_f.append_val(remove_files.index(ii));
435 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
438 // if file was added, then removed,
439 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
440 for(var ii = 0;ii < remove_files.size;ii++) {
444 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
445 // in add and remove - do not remvove
448 remove_files_f.add(remove_files.get(ii));
450 for(var ii = 0;ii < add_files.size;ii++) {
451 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
452 // the add file is in the remove list, and it does not exist - do not add it..
453 print("check exists ? %s\n",add_files.get(ii).fullpath());
455 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
461 add_files_f.add(add_files.get(ii));
464 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
465 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
467 if (add_files_f.size < 1 && remove_files_f.size < 1) {
471 // make sure monitoring is paused so it does not recursively pick up
480 this.pauseError(e.message);
481 //failure += e.message;
482 //print("Pull failed:\n");
488 repo.add(add_files_f);
490 this.pauseError(e.message);
492 failure += e.message;
493 GLib.debug("Add failed:");
496 repo.remove(remove_files_f);
498 this.pauseError(e.message);
500 failure += e.message;
501 GLib.debug("Remove failed:");
505 success += repo.commit(
506 GitMonitorQueue.messageToString(messages),
509 success += repo.push();
514 // if the error is 'nothing to commit, working tree clean'
515 // then it's not an error, - just continue;
516 if (/nothing to commit, working tree clean/.match(e.message)) {
517 GLib.debug("%s",e.message);
518 success += e.message;
526 this.pauseError(e.message);
529 //failure += e.message;
530 //print("Push failed:\n");
537 // finally merge all the commit messages.
540 // catch notification failures.. so we can carry on..
541 if (success.length > 0) {
544 var notification = new Notify.Notification(
546 string.joinv("\n",success),
551 notification.set_timeout(5);
555 //if (failure.length > 0) {
557 // should never get this far...
558 // this.pauseError();
561 GLib.debug(e.message);
564 this.queueRunning = false;
571 //string[] just_created;
580 public override void onChanged(MonitorNamePathDir src)
582 //print("GitMonitor.onChanged\n");
583 return; // always ignore this..?
584 //this.parsePath(src);
590 * results in git add + git commit..
593 public override void onChangesDoneHint(MonitorNamePathDir src)
599 GLib.debug("GitMonitor.onChangedHint");
601 this.lastAdd = new DateTime.now(new TimeZone.local());
602 var cmd = new GitMonitorQueue(src);
603 if (cmd.shouldIgnore()) {
604 GLib.debug("GitMonitor.onChangedHint - ignored");
608 //var add_it = false;
610 if (this.is_just_created(cmd.path)) {
612 if (typeof(this.just_created[src.path]) !='undefined') {
613 delete this.just_created[src.path];
616 [ src.gitpath, 'add', src.vpath ],
617 [ src.gitpath, 'commit', { message: src.vpath} ]
627 cmd = new GitMonitorQueue(src);
628 cmd.action = "commit";
629 cmd.message = cmd.vname;
634 public override void onDeleted(MonitorNamePathDir src)
640 GLib.debug("GitMonitor.onDeleted");
641 this.lastAdd = new DateTime.now(new TimeZone.local());
642 var cmd = new GitMonitorQueue(src);
643 if (cmd.shouldIgnore()) {
646 // should check if monitor needs removing..
647 // it should also check if it was a directory.. - so we dont have to commit all..
651 cmd = new GitMonitorQueue(src);
652 cmd.action = "commit";
653 cmd.message = cmd.vname;
654 cmd.commit_all = true;
659 public override void onCreated(MonitorNamePathDir src) {
664 GLib.debug("GitMonitor.onCreated");
665 this.lastAdd = new DateTime.now(new TimeZone.local());
666 var cmd = new GitMonitorQueue(src);
667 if (cmd.shouldIgnore()) {
671 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
672 // this.just_created[src.path] = true;
673 return; // we do not handle file create flags... - use done hint.
675 // directory has bee created
676 this.monitor(src.path);
677 //this.top.append_val(src.path);
678 //this.monitor(src.path );
681 // -- no point in adding a dir.. as git does not handle them...
683 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
684 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
690 public override void onAttributeChanged(MonitorNamePathDir src)
696 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
697 if (src.dir == GitMonitor.gitlive) {
698 return; // attribute on top level..
701 this.lastAdd = new DateTime.now(new TimeZone.local());
702 var cmd = new GitMonitorQueue(src);
703 if (cmd.shouldIgnore()) {
709 cmd = new GitMonitorQueue(src);
710 cmd.action = "commit";
711 cmd.message = "Attribute changed " + cmd.vname;
715 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
721 GLib.debug("GitMonitor.onMoved");
722 this.lastAdd = new DateTime.now(new TimeZone.local());
723 var cmd_s = new GitMonitorQueue(src);
725 var cmd_d = new GitMonitorQueue(dest);
728 if (cmd_d.gitpath != cmd_s.gitpath) {
730 this.onCreated(dest);
731 this.onChangesDoneHint(dest);
734 // needs to handle move to/from unsupported types..
736 if (cmd_s.shouldIgnore()) {
737 this.onCreated(dest);
738 this.onChangesDoneHint(dest);
742 if (cmd_d.shouldIgnore()) {
748 GLib.debug("RM: %s", cmd_s.vname);
750 this.queue.add(cmd_s);
754 GLib.debug("ADD: %s", cmd_d.vname);
755 cmd_d.action = "add";
756 this.queue.add(cmd_d);
759 var cmd = new GitMonitorQueue(dest);
760 cmd.action = "commit";
761 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
762 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
763 cmd.message = cmd_d.vname;