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);
59 if (!first.repo.is_wip_branch() && first.repo.is_auto_branch()) {
64 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
66 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
67 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
69 if (delay && last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
72 //_this.lastAdd = new Date();
81 public new void pauseError(string failure)
84 var notification = new Notify.Notification(
91 notification.set_timeout(60); // show errros for longer
94 Canberra.Context context;
95 Canberra.Proplist props;
97 Canberra.Context.create (out context);
98 Canberra.Proplist.create (out props);
100 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
101 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
104 context.play_full (0, props, null);
110 this.queueRunning = false;
111 // what does this do to the old one...
112 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
114 StatusIconA.statusicon.pauseError();
116 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
117 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
118 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
121 m.set_keep_above(true);
123 m.set_position(Gtk.WindowPosition.CENTER);
124 m.response.connect( (id_pressed) => {
130 public new void pause() {
132 // what does this do to the old one...
133 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
134 StatusIconA.statusicon.pause();
138 public void restoreQueue( Gee.ArrayList<GitMonitorQueue> queue)
140 //used to restore teh queue after switch branches?/ - breaks our privte queue idea..
145 public new void resume () {
147 this.queue = new Array<GitMonitorQueue> ();
148 StatusIconA.statusicon.resume();
154 * Start the monitoring
155 * and run the queue every 500 milliseconds..
158 public new void start()
160 StatusIconA.statusicon.refreshing();
163 this.lastAdd = new DateTime.now(new TimeZone.local());
165 Timeout.add_full(Priority.LOW, 500, () => {
166 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
167 // call this.monitor on each of 'top'
168 for(int i = 0; i < this.top.length ; i++) {
170 this.monitor(this.top.index(i) );
172 StatusIconA.statusicon.resume();
179 var notification = new Notify.Notification(
181 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
185 notification.set_timeout(5);
188 GLib.debug("Error sending notification to screen: %s",e.message);
190 return false; // do not keep doing this..
200 public new void stop() {
201 StatusIconA.statusicon.pause();
206 public override void monitor (string path, int depth = 0)
209 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
210 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
213 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
214 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
220 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
221 return; // skip non-git directories..
226 // FIXME - check if repo is flagged as not autocommit..
227 //var repo = imports.Scm.Repo.Repo.get(path);
228 //if (!repo || !repo.autocommit()) {
234 // check if the repo is to be monitored.
235 //print("PATH : " + path);
238 base.monitor(path, depth);
245 * - pulls the items off the queue
246 * (as commands run concurrently and new items may get added while it's running)
247 * - runs the queue items
251 public void runQueue()
254 if (this.paused || this.queue.size < 1 ) {
258 foreach(var q in this.queue) {
259 if (!q.shouldIgnore() && !q.repo.is_wip_branch() && q.repo.is_auto_branch()) {
260 var oldq = this.queue;
261 this.queue = new Gee.ArrayList<GitMonitorQueue>();
262 NewBranch.singleton().show(q.repo, oldq);
271 GLib.debug("GitMonitor.runQueue size =%d\n", this.queue.size);
273 this.queueRunning = true;
275 var cmds = new Gee.ArrayList<GitMonitorQueue>();
277 for(var i = 0; i < this.queue.size; i++) {
278 cmds.add(this.queue.get(i));
281 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
283 string[] success = {};
284 string[] failure = {};
285 //var repos = new Array<GitRepo>(); //??
286 //var done = new Array<GitMonitorQueue>();
288 // first build a array of repo's to work with
289 var repo_list = new Array<GitRepo>();
293 //print(JSON.stringify(cmds));
294 // make sure nothing get's added to the queue where we are doing this..
298 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
299 GLib.debug("GitMonitor.runQueue - creating repos");
301 for(var i = 0; i < cmds.size; i++) {
303 var cmd = cmds.get(i);
305 var gitpath = cmd.gitpath;
307 var repo = GitRepo.get( gitpath );
308 if ( !repo.is_wip_branch() && repo.is_auto_branch()) {
309 leave_queued.add(cmd);
313 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
315 var ix = GitRepo.indexOf(repo_list, gitpath);
317 repo_list.append_val( GitRepo.get( gitpath ));
318 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
320 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
322 //if (typeof(repo_list[gitpath]) == 'undefined') {
323 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
324 // repo_list[gitpath].cmds = [];
325 // repo_list[gitpath].pull();
327 repo_list.index(ix).cmds.add(cmd);
330 this.queue = leave_queued;
333 // build add, remove and commit message list..
335 GLib.debug("GitMonitor.runQueue - creating actions");
337 for(var i = 0;i < repo_list.length;i++) {
339 var repo = repo_list.index(i);
341 var add_files = new Gee.ArrayList<GitMonitorQueue>();
342 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
343 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
344 var messages = new Gee.ArrayList<GitMonitorQueue>();
345 //print(JSON.stringify(repo.cmds,null,4));
347 for(var ii = 0;ii < repo.cmds.size;ii++) {
348 var cmd = repo.cmds.get(ii);
350 if (repo.is_ignore(cmd.vname)) {
358 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
366 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
370 // if file exists, do not try and delete it.
371 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
375 remove_files.add(cmd);
379 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
386 stdout.printf("Opps unmatched action %s\n", cmd.action);
391 repo.cmds.clear(); // reset the repo's command list..
393 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
394 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
397 // these can fail... at present... as we wildcard stuff.
399 // make sure added files do not get removed.. ??
401 var remove_files_f = new Array<GitMonitorQueue>();
402 for(var ii = 0;ii < remove_files.length;ii++) {
403 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
406 remove_files_f.append_val(remove_files.index(ii));
408 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
411 // if file was added, then removed,
412 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
413 for(var ii = 0;ii < remove_files.size;ii++) {
417 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
418 // in add and remove - do not remvove
421 remove_files_f.add(remove_files.get(ii));
423 for(var ii = 0;ii < add_files.size;ii++) {
424 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
425 // the add file is in the remove list, and it does not exist - do not add it..
426 print("check exists ? %s\n",add_files.get(ii).fullpath());
428 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
434 add_files_f.add(add_files.get(ii));
437 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
438 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
440 if (add_files_f.size < 1 && remove_files_f.size < 1) {
444 // make sure monitoring is paused so it does not recursively pick up
453 this.pauseError(e.message);
454 //failure += e.message;
455 //print("Pull failed:\n");
461 repo.add(add_files_f);
463 this.pauseError(e.message);
465 failure += e.message;
466 GLib.debug("Add failed:");
469 repo.remove(remove_files_f);
471 this.pauseError(e.message);
473 failure += e.message;
474 GLib.debug("Remove failed:");
478 success += repo.commit(
479 GitMonitorQueue.messageToString(messages),
482 success += repo.push();
487 // if the error is 'nothing to commit, working tree clean'
488 // then it's not an error, - just continue;
489 if (/nothing to commit, working tree clean/.match(e.message)) {
490 GLib.debug("%s",e.message);
491 success += e.message;
499 this.pauseError(e.message);
502 //failure += e.message;
503 //print("Push failed:\n");
510 // finally merge all the commit messages.
513 // catch notification failures.. so we can carry on..
514 if (success.length > 0) {
517 var notification = new Notify.Notification(
519 string.joinv("\n",success),
524 notification.set_timeout(5);
528 //if (failure.length > 0) {
530 // should never get this far...
531 // this.pauseError();
534 GLib.debug(e.message);
537 this.queueRunning = false;
544 //string[] just_created;
553 public override void onChanged(MonitorNamePathDir src)
555 //print("GitMonitor.onChanged\n");
556 return; // always ignore this..?
557 //this.parsePath(src);
563 * results in git add + git commit..
566 public override void onChangesDoneHint(MonitorNamePathDir src)
572 GLib.debug("GitMonitor.onChangedHint");
574 this.lastAdd = new DateTime.now(new TimeZone.local());
575 var cmd = new GitMonitorQueue(src);
576 if (cmd.shouldIgnore()) {
577 GLib.debug("GitMonitor.onChangedHint - ignored");
581 //var add_it = false;
583 if (this.is_just_created(cmd.path)) {
585 if (typeof(this.just_created[src.path]) !='undefined') {
586 delete this.just_created[src.path];
589 [ src.gitpath, 'add', src.vpath ],
590 [ src.gitpath, 'commit', { message: src.vpath} ]
600 cmd = new GitMonitorQueue(src);
601 cmd.action = "commit";
602 cmd.message = cmd.vname;
607 public override void onDeleted(MonitorNamePathDir src)
613 GLib.debug("GitMonitor.onDeleted");
614 this.lastAdd = new DateTime.now(new TimeZone.local());
615 var cmd = new GitMonitorQueue(src);
616 if (cmd.shouldIgnore()) {
619 // should check if monitor needs removing..
620 // it should also check if it was a directory.. - so we dont have to commit all..
624 cmd = new GitMonitorQueue(src);
625 cmd.action = "commit";
626 cmd.message = cmd.vname;
627 cmd.commit_all = true;
632 public override void onCreated(MonitorNamePathDir src) {
637 GLib.debug("GitMonitor.onCreated");
638 this.lastAdd = new DateTime.now(new TimeZone.local());
639 var cmd = new GitMonitorQueue(src);
640 if (cmd.shouldIgnore()) {
644 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
645 // this.just_created[src.path] = true;
646 return; // we do not handle file create flags... - use done hint.
648 // directory has bee created
649 this.monitor(src.path);
650 //this.top.append_val(src.path);
651 //this.monitor(src.path );
654 // -- no point in adding a dir.. as git does not handle them...
656 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
657 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
663 public override void onAttributeChanged(MonitorNamePathDir src)
669 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
670 if (src.dir == GitMonitor.gitlive) {
671 return; // attribute on top level..
674 this.lastAdd = new DateTime.now(new TimeZone.local());
675 var cmd = new GitMonitorQueue(src);
676 if (cmd.shouldIgnore()) {
682 cmd = new GitMonitorQueue(src);
683 cmd.action = "commit";
684 cmd.message = "Attribute changed " + cmd.vname;
688 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
694 GLib.debug("GitMonitor.onMoved");
695 this.lastAdd = new DateTime.now(new TimeZone.local());
696 var cmd_s = new GitMonitorQueue(src);
698 var cmd_d = new GitMonitorQueue(dest);
701 if (cmd_d.gitpath != cmd_s.gitpath) {
703 this.onCreated(dest);
704 this.onChangesDoneHint(dest);
707 // needs to handle move to/from unsupported types..
709 if (cmd_s.shouldIgnore()) {
710 this.onCreated(dest);
711 this.onChangesDoneHint(dest);
715 if (cmd_d.shouldIgnore()) {
721 GLib.debug("RM: %s", cmd_s.vname);
723 this.queue.add(cmd_s);
727 GLib.debug("ADD: %s", cmd_d.vname);
728 cmd_d.action = "add";
729 this.queue.add(cmd_d);
732 var cmd = new GitMonitorQueue(dest);
733 cmd.action = "commit";
734 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
735 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
736 cmd.message = cmd_d.vname;