4 public class GitMonitor : Monitor
7 public static GitMonitor gitmonitor;
9 * @property {String} the "gitlive" directory, normally ~/gitlive
10 * dset by OWNER... - we should do this as a CTOR.
13 public static string gitlive;
16 private Gee.ArrayList<GitMonitorQueue> queue ;
17 public bool queueRunning = false;
19 public DateTime lastAdd;
26 this.queue = new Gee.ArrayList<GitMonitorQueue>();
27 GitMonitor.gitmonitor = this;
28 Timeout.add_full(Priority.LOW, 500, () => {
30 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
32 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
33 if (this.queue.size < 1 || this.queueRunning) {
37 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
39 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
40 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
42 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
45 //_this.lastAdd = new Date();
54 public new void pauseError(string failure)
57 var notification = new Notify.Notification(
64 notification.set_timeout(60); // show errros for longer
67 Canberra.Context context;
68 Canberra.Proplist props;
70 Canberra.Context.create (out context);
71 Canberra.Proplist.create (out props);
73 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
74 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
77 context.play_full (0, props, null);
83 this.queueRunning = false;
84 // what does this do to the old one...
85 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
87 StatusIconA.statusicon.pauseError();
89 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
90 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
91 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
94 m.set_keep_above(true);
96 m.set_position(Gtk.WindowPosition.CENTER);
97 m.response.connect( (id_pressed) => {
103 public new void pause() {
105 // what does this do to the old one...
106 //this.queue = new Gee.ArrayList<GitMonitorQueue> ();
107 StatusIconA.statusicon.pause();
112 public new void resume () {
114 this.queue = new Array<GitMonitorQueue> ();
115 StatusIconA.statusicon.resume();
121 * Start the monitoring
122 * and run the queue every 500 milliseconds..
125 public new void start()
127 StatusIconA.statusicon.refreshing();
130 this.lastAdd = new DateTime.now(new TimeZone.local());
132 Timeout.add_full(Priority.LOW, 500, () => {
133 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
134 // call this.monitor on each of 'top'
135 for(int i = 0; i < this.top.length ; i++) {
137 this.monitor(this.top.index(i) );
139 StatusIconA.statusicon.resume();
146 var notification = new Notify.Notification(
148 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
152 notification.set_timeout(5);
155 GLib.debug("Error sending notification to screen: %s",e.message);
157 return false; // do not keep doing this..
167 public new void stop() {
168 StatusIconA.statusicon.pause();
173 public override void monitor (string path, int depth = 0)
176 //GLib.debug("GitMonitor : monitor %d %s", depth, path);
177 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
180 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
181 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
187 if (!FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
188 return; // skip non-git directories..
193 // FIXME - check if repo is flagged as not autocommit..
194 //var repo = imports.Scm.Repo.Repo.get(path);
195 //if (!repo || !repo.autocommit()) {
201 // check if the repo is to be monitored.
202 //print("PATH : " + path);
205 base.monitor(path, depth);
212 * - pulls the items off the queue
213 * (as commands run concurrently and new items may get added while it's running)
214 * - runs the queue items
218 public void runQueue()
221 if (this.paused || this.queue.size < 1 ) {
224 GLib.debug("GitMonitor.runQueue\n");
226 this.queueRunning = true;
228 var cmds = new Gee.ArrayList<GitMonitorQueue>();
230 for(var i = 0; i < this.queue.size; i++) {
231 cmds.add(this.queue.get(i));
234 this.queue = new Gee.ArrayList<GitMonitorQueue>();// empty queue!
236 string[] success = {};
237 string[] failure = {};
238 //var repos = new Array<GitRepo>(); //??
239 //var done = new Array<GitMonitorQueue>();
241 // first build a array of repo's to work with
242 var repo_list = new Array<GitRepo>();
246 //print(JSON.stringify(cmds));
247 // make sure nothing get's added to the queue where we are doing this..
251 var leave_queued = new Gee.ArrayList<GitMonitorQueue>();
252 GLib.debug("GitMonitor.runQueue - creating repos");
254 for(var i = 0; i < cmds.size; i++) {
256 var cmd = cmds.get(i);
258 var gitpath = cmd.gitpath;
260 var repo = GitRepo.get( gitpath );
261 if ( !repo.is_wip_branch()) {
262 leave_queued.add(cmd);
266 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
268 var ix = GitRepo.indexOf(repo_list, gitpath);
270 repo_list.append_val( GitRepo.get( gitpath ));
271 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
273 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
275 //if (typeof(repo_list[gitpath]) == 'undefined') {
276 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
277 // repo_list[gitpath].cmds = [];
278 // repo_list[gitpath].pull();
280 repo_list.index(ix).cmds.add(cmd);
283 this.queue = leave_queued;
286 // build add, remove and commit message list..
288 GLib.debug("GitMonitor.runQueue - creating actions");
290 for(var i = 0;i < repo_list.length;i++) {
292 var repo = repo_list.index(i);
294 var add_files = new Gee.ArrayList<GitMonitorQueue>();
295 var add_files_f = new Gee.ArrayList<GitMonitorQueue>();
296 var remove_files = new Gee.ArrayList<GitMonitorQueue>();
297 var messages = new Gee.ArrayList<GitMonitorQueue>();
298 //print(JSON.stringify(repo.cmds,null,4));
300 for(var ii = 0;ii < repo.cmds.size;ii++) {
301 var cmd = repo.cmds.get(ii);
303 if (repo.is_ignore(cmd.vname)) {
311 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
319 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
323 // if file exists, do not try and delete it.
324 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
328 remove_files.add(cmd);
332 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
339 stdout.printf("Opps unmatched action %s\n", cmd.action);
344 repo.cmds.clear(); // reset the repo's command list..
346 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
347 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
350 // these can fail... at present... as we wildcard stuff.
352 // make sure added files do not get removed.. ??
354 var remove_files_f = new Array<GitMonitorQueue>();
355 for(var ii = 0;ii < remove_files.length;ii++) {
356 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
359 remove_files_f.append_val(remove_files.index(ii));
361 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
364 // if file was added, then removed,
365 var remove_files_f = new Gee.ArrayList<GitMonitorQueue>();
366 for(var ii = 0;ii < remove_files.size;ii++) {
370 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.get(ii).vname) > -1 ) {
371 // in add and remove - do not remvove
374 remove_files_f.add(remove_files.get(ii));
376 for(var ii = 0;ii < add_files.size;ii++) {
377 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.get(ii).vname) > -1 ) {
378 // the add file is in the remove list, and it does not exist - do not add it..
379 print("check exists ? %s\n",add_files.get(ii).fullpath());
381 if (!FileUtils.test(add_files.get(ii).fullpath(), FileTest.EXISTS)) {
387 add_files_f.add(add_files.get(ii));
390 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
391 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
393 if (add_files_f.size < 1 && remove_files_f.size < 1) {
397 // make sure monitoring is paused so it does not recursively pick up
406 this.pauseError(e.message);
407 //failure += e.message;
408 //print("Pull failed:\n");
414 repo.add(add_files_f);
416 this.pauseError(e.message);
418 failure += e.message;
419 GLib.debug("Add failed:");
422 repo.remove(remove_files_f);
424 this.pauseError(e.message);
426 failure += e.message;
427 GLib.debug("Remove failed:");
431 success += repo.commit(
432 GitMonitorQueue.messageToString(messages),
435 success += repo.push();
440 // if the error is 'nothing to commit, working tree clean'
441 // then it's not an error, - just continue;
442 if (/nothing to commit, working tree clean/.match(e.message)) {
443 GLib.debug("%s",e.message);
444 success += e.message;
452 this.pauseError(e.message);
455 //failure += e.message;
456 //print("Push failed:\n");
463 // finally merge all the commit messages.
466 // catch notification failures.. so we can carry on..
467 if (success.length > 0) {
470 var notification = new Notify.Notification(
472 string.joinv("\n",success),
477 notification.set_timeout(5);
481 //if (failure.length > 0) {
483 // should never get this far...
484 // this.pauseError();
487 GLib.debug(e.message);
490 this.queueRunning = false;
497 //string[] just_created;
506 public override void onChanged(MonitorNamePathDir src)
508 //print("GitMonitor.onChanged\n");
509 return; // always ignore this..?
510 //this.parsePath(src);
516 * results in git add + git commit..
519 public override void onChangesDoneHint(MonitorNamePathDir src)
525 GLib.debug("GitMonitor.onChangedHint");
527 this.lastAdd = new DateTime.now(new TimeZone.local());
528 var cmd = new GitMonitorQueue(src);
529 if (cmd.shouldIgnore()) {
530 GLib.debug("GitMonitor.onChangedHint - ignored");
534 //var add_it = false;
536 if (this.is_just_created(cmd.path)) {
538 if (typeof(this.just_created[src.path]) !='undefined') {
539 delete this.just_created[src.path];
542 [ src.gitpath, 'add', src.vpath ],
543 [ src.gitpath, 'commit', { message: src.vpath} ]
553 cmd = new GitMonitorQueue(src);
554 cmd.action = "commit";
555 cmd.message = cmd.vname;
559 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
560 NewBranch.singleton().show(cmd.repo);
565 public override void onDeleted(MonitorNamePathDir src)
571 GLib.debug("GitMonitor.onDeleted");
572 this.lastAdd = new DateTime.now(new TimeZone.local());
573 var cmd = new GitMonitorQueue(src);
574 if (cmd.shouldIgnore()) {
577 // should check if monitor needs removing..
578 // it should also check if it was a directory.. - so we dont have to commit all..
582 cmd = new GitMonitorQueue(src);
583 cmd.action = "commit";
584 cmd.message = cmd.vname;
585 cmd.commit_all = true;
589 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
590 NewBranch.singleton().show(cmd.repo);
595 public override void onCreated(MonitorNamePathDir src) {
600 GLib.debug("GitMonitor.onCreated");
601 this.lastAdd = new DateTime.now(new TimeZone.local());
602 var cmd = new GitMonitorQueue(src);
603 if (cmd.shouldIgnore()) {
607 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
608 // this.just_created[src.path] = true;
609 return; // we do not handle file create flags... - use done hint.
611 // directory has bee created
612 this.monitor(src.path);
613 //this.top.append_val(src.path);
614 //this.monitor(src.path );
617 // -- no point in adding a dir.. as git does not handle them...
619 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
620 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
626 public override void onAttributeChanged(MonitorNamePathDir src)
632 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
633 if (src.dir == GitMonitor.gitlive) {
634 return; // attribute on top level..
637 this.lastAdd = new DateTime.now(new TimeZone.local());
638 var cmd = new GitMonitorQueue(src);
639 if (cmd.shouldIgnore()) {
645 cmd = new GitMonitorQueue(src);
646 cmd.action = "commit";
647 cmd.message = "Attribute changed " + cmd.vname;
650 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
651 NewBranch.singleton().show(cmd.repo);
655 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
661 GLib.debug("GitMonitor.onMoved");
662 this.lastAdd = new DateTime.now(new TimeZone.local());
663 var cmd_s = new GitMonitorQueue(src);
665 var cmd_d = new GitMonitorQueue(dest);
668 if (cmd_d.gitpath != cmd_s.gitpath) {
670 this.onCreated(dest);
671 this.onChangesDoneHint(dest);
674 // needs to handle move to/from unsupported types..
676 if (cmd_s.shouldIgnore()) {
677 this.onCreated(dest);
678 this.onChangesDoneHint(dest);
682 if (cmd_d.shouldIgnore()) {
691 GLib.debug("RM: %s", cmd_s.vname);
693 this.queue.add(cmd_s);
697 GLib.debug("ADD: %s", cmd_d.vname);
698 cmd_d.action = "add";
699 this.queue.add(cmd_d);
702 var cmd = new GitMonitorQueue(dest);
703 cmd.action = "commit";
704 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
705 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
706 cmd.message = cmd_d.vname;
712 if (!cmd.shouldIgnore() && !cmd.repo.is_wip_branch()) {
713 NewBranch.singleton().show(cmd.repo);
715 // this is where it get's complicated...
716 // as we might be moving across repo's...