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) );
174 StatusIconA.statusicon.resume();
181 var notification = new Notify.Notification(
183 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
187 notification.set_timeout(5);
190 GLib.debug("Error sending notification to screen: %s",e.message);
192 return false; // do not keep doing this..
202 public new void stop() {
203 StatusIconA.statusicon.pause();
208 public override void monitor (string path, int depth = 0)
211 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
212 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
215 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
216 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
222 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
223 return; // skip non-git directories..
228 // FIXME - check if repo is flagged as not autocommit..
229 //var repo = imports.Scm.Repo.Repo.get(path);
230 //if (!repo || !repo.autocommit()) {
236 // check if the repo is to be monitored.
237 //print("PATH : " + path);
240 base.monitor(path, depth);
247 * - pulls the items off the queue
248 * (as commands run concurrently and new items may get added while it's running)
249 * - runs the queue items
253 public void runQueue()
256 if (this.paused || this.queue.size < 1 ) {
260 foreach(var q in this.queue) {
261 if (!q.shouldIgnore() && q.repo.is_master_branch() && q.repo.is_auto_branch()) {
262 var oldq = this.queue;
263 this.queue = new Gee.ArrayList<GitMonitorQueue>();
264 NewBranch.singleton().show(q.repo, oldq);
273 GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
275 this.queueRunning = true;
277 var cmds = new Gee.ArrayList<GitMonitorQueue>();
279 for(var i = 0; i < this.queue.size; i++) {
280 cmds.add(this.queue.get(i));
283 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
285 string[] success = {};
286 string[] failure = {};
287 //var repos = new Array<GitRepo>(); //??
288 //var done = new Array<GitMonitorQueue>();
290 // first build a array of repo's to work with
291 var repo_list = new Array<GitRepo>();
295 //print(JSON.stringify(cmds));
296 // make sure nothing get's added to the queue where we are doing this..
300 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
301 GLib.debug("GitMonitor.runQueue - creating repos");
303 for(var i = 0; i < cmds.size; i++) {
305 var cmd = cmds.get(i);
307 var gitpath = cmd.gitpath;
309 var repo = GitRepo.get( gitpath );
310 if ( repo.is_master_branch() && repo.is_auto_branch()) {
311 leave_queued.add(cmd);
315 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
317 var ix = GitRepo.indexOf(repo_list, gitpath);
319 repo_list.append_val( GitRepo.get( gitpath ));
320 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
322 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
324 //if (typeof(repo_list[gitpath]) == 'undefined') {
325 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
326 // repo_list[gitpath].cmds = [];
327 // repo_list[gitpath].pull();
329 repo_list.index(ix).cmds.add(cmd);
332 this.queue = leave_queued;
335 // build add, remove and commit message list..
337 GLib.debug("GitMonitor.runQueue - creating actions");
339 for(var i = 0;i < repo_list.length;i++) {
341 var repo = repo_list.index(i);
343 var add_files = new Gee.ArrayList<GitMonitorQueue>();
344 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
345 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
346 var messages = new Gee.ArrayList<GitMonitorQueue>();
347 //print(JSON.stringify(repo.cmds,null,4));
349 for(var ii = 0;ii < repo.cmds.size;ii++) {
350 var cmd = repo.cmds.get(ii);
352 if (repo.is_ignore(cmd.vname)) {
360 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
368 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
372 // if file exists, do not try and delete it.
373 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
377 remove_files.add(cmd);
381 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
388 stdout.printf("Opps unmatched action %s\n", cmd.action);
393 repo.cmds.clear(); // reset the repo's command list..
395 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
396 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
399 // these can fail... at present... as we wildcard stuff.
401 // make sure added files do not get removed.. ??
403 var remove_files_f = new Array<GitMonitorQueue>();
404 for(var ii = 0;ii < remove_files.length;ii++) {
405 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
408 remove_files_f.append_val(remove_files.index(ii));
410 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
413 // if file was added, then removed,
414 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
415 for(var ii = 0;ii < remove_files.size;ii++) {
419 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
420 // in add and remove - do not remvove
423 remove_files_f.add(remove_files.get(ii));
425 for(var ii = 0;ii < add_files.size;ii++) {
426 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
427 // the add file is in the remove list, and it does not exist - do not add it..
428 print("check exists ? %s\n",add_files.get(ii).fullpath());
430 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
436 add_files_f.add(add_files.get(ii));
439 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
440 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
442 if (add_files_f.size < 1 && remove_files_f.size < 1) {
446 // make sure monitoring is paused so it does not recursively pick up
455 this.pauseError(e.message);
456 //failure += e.message;
457 //print("Pull failed:\n");
463 repo.add(add_files_f);
465 this.pauseError(e.message);
467 failure += e.message;
468 GLib.debug("Add failed:");
471 repo.remove(remove_files_f);
473 this.pauseError(e.message);
475 failure += e.message;
476 GLib.debug("Remove failed:");
480 success += repo.commit(
481 GitMonitorQueue.messageToString(messages),
484 success += repo.push();
489 // if the error is 'nothing to commit, working tree clean'
490 // then it's not an error, - just continue;
491 if (/nothing to commit, working tree clean/.match(e.message)) {
492 GLib.debug("%s",e.message);
493 success += e.message;
501 this.pauseError(e.message);
504 //failure += e.message;
505 //print("Push failed:\n");
512 // finally merge all the commit messages.
515 // catch notification failures.. so we can carry on..
516 if (success.length > 0) {
519 var notification = new Notify.Notification(
521 string.joinv("\n",success),
526 notification.set_timeout(5);
530 //if (failure.length > 0) {
532 // should never get this far...
533 // this.pauseError();
536 GLib.debug(e.message);
539 this.queueRunning = false;
546 //string[] just_created;
555 public override void onChanged(MonitorNamePathDir src)
557 //print("GitMonitor.onChanged\n");
558 return; // always ignore this..?
559 //this.parsePath(src);
565 * results in git add + git commit..
568 public override void onChangesDoneHint(MonitorNamePathDir src)
574 GLib.debug("GitMonitor.onChangedHint");
576 this.lastAdd = new DateTime.now(new TimeZone.local());
577 var cmd = new GitMonitorQueue(src);
578 if (cmd.shouldIgnore()) {
579 GLib.debug("GitMonitor.onChangedHint - ignored");
583 //var add_it = false;
585 if (this.is_just_created(cmd.path)) {
587 if (typeof(this.just_created[src.path]) !='undefined') {
588 delete this.just_created[src.path];
591 [ src.gitpath, 'add', src.vpath ],
592 [ src.gitpath, 'commit', { message: src.vpath} ]
602 cmd = new GitMonitorQueue(src);
603 cmd.action = "commit";
604 cmd.message = cmd.vname;
609 public override void onDeleted(MonitorNamePathDir src)
615 GLib.debug("GitMonitor.onDeleted");
616 this.lastAdd = new DateTime.now(new TimeZone.local());
617 var cmd = new GitMonitorQueue(src);
618 if (cmd.shouldIgnore()) {
621 // should check if monitor needs removing..
622 // it should also check if it was a directory.. - so we dont have to commit all..
626 cmd = new GitMonitorQueue(src);
627 cmd.action = "commit";
628 cmd.message = cmd.vname;
629 cmd.commit_all = true;
634 public override void onCreated(MonitorNamePathDir src) {
639 GLib.debug("GitMonitor.onCreated");
640 this.lastAdd = new DateTime.now(new TimeZone.local());
641 var cmd = new GitMonitorQueue(src);
642 if (cmd.shouldIgnore()) {
646 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
647 // this.just_created[src.path] = true;
648 return; // we do not handle file create flags... - use done hint.
650 // directory has bee created
651 this.monitor(src.path);
652 //this.top.append_val(src.path);
653 //this.monitor(src.path );
656 // -- no point in adding a dir.. as git does not handle them...
658 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
659 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
665 public override void onAttributeChanged(MonitorNamePathDir src)
671 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
672 if (src.dir == GitMonitor.gitlive) {
673 return; // attribute on top level..
676 this.lastAdd = new DateTime.now(new TimeZone.local());
677 var cmd = new GitMonitorQueue(src);
678 if (cmd.shouldIgnore()) {
684 cmd = new GitMonitorQueue(src);
685 cmd.action = "commit";
686 cmd.message = "Attribute changed " + cmd.vname;
690 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
696 GLib.debug("GitMonitor.onMoved");
697 this.lastAdd = new DateTime.now(new TimeZone.local());
698 var cmd_s = new GitMonitorQueue(src);
700 var cmd_d = new GitMonitorQueue(dest);
703 if (cmd_d.gitpath != cmd_s.gitpath) {
705 this.onCreated(dest);
706 this.onChangesDoneHint(dest);
709 // needs to handle move to/from unsupported types..
711 if (cmd_s.shouldIgnore()) {
712 this.onCreated(dest);
713 this.onChangesDoneHint(dest);
717 if (cmd_d.shouldIgnore()) {
723 GLib.debug("RM: %s", cmd_s.vname);
725 this.queue.add(cmd_s);
729 GLib.debug("ADD: %s", cmd_d.vname);
730 cmd_d.action = "add";
731 this.queue.add(cmd_d);
734 var cmd = new GitMonitorQueue(dest);
735 cmd.action = "commit";
736 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
737 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
738 cmd.message = cmd_d.vname;