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 public Array<GitMonitorQueue> queue ;
17 public bool queueRunning = false;
19 public DateTime lastAdd;
23 public GitMonitor () {
27 this.queue = new Array<GitMonitorQueue>();
28 GitMonitor.gitmonitor = this;
29 Timeout.add_full(Priority.LOW, 500, () => {
31 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
33 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
34 if (this.queue.length < 1 || this.queueRunning) {
38 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
40 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
41 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
43 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
46 //_this.lastAdd = new Date();
55 public new void pauseError(string failure)
58 var notification = new Notify.Notification(
65 notification.set_timeout(60); // show errros for longer
68 Canberra.Context context;
69 Canberra.Proplist props;
71 Canberra.Context.create (out context);
72 Canberra.Proplist.create (out props);
74 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
75 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
78 context.play_full (0, props, null);
84 this.queueRunning = false;
85 // what does this do to the old one...
86 this.queue = new Array<GitMonitorQueue> ();
88 StatusIconA.statusicon.pauseError();
90 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
91 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
92 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
95 m.set_keep_above(true);
97 m.set_position(Gtk.WindowPosition.CENTER);
98 m.response.connect( (id_pressed) => {
104 public new void pause() {
106 // what does this do to the old one...
107 this.queue = new Array<GitMonitorQueue> ();
108 StatusIconA.statusicon.pause();
113 public new void resume () {
115 this.queue = new Array<GitMonitorQueue> ();
116 StatusIconA.statusicon.resume();
122 * Start the monitoring
123 * and run the queue every 500 milliseconds..
126 public new void start()
128 StatusIconA.statusicon.refreshing();
131 this.lastAdd = new DateTime.now(new TimeZone.local());
133 Timeout.add_full(Priority.LOW, 500, () => {
134 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
135 // call this.monitor on each of 'top'
136 for(int i = 0; i < this.top.length ; i++) {
138 this.monitor(this.top.index(i) );
140 StatusIconA.statusicon.resume();
147 var notification = new Notify.Notification(
149 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
153 notification.set_timeout(5);
156 GLib.debug("Error sending notification to screen: %s",e.message);
158 return false; // do not keep doing this..
168 public new void stop() {
169 StatusIconA.statusicon.pause();
174 public new void monitor (string path, int depth = 0)
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)) {
186 // FIXME - check if repo is flagged as not autocommit..
187 //var repo = imports.Scm.Repo.Repo.get(path);
188 //if (!repo || !repo.autocommit()) {
194 // check if the repo is to be monitored.
195 //print("PATH : " + path);
198 base.monitor(path, depth);
205 * - pulls the items off the queue
206 * (as commands run concurrently and new items may get added while it's running)
207 * - runs the queue items
211 public void runQueue()
217 GLib.debug("GitMonitor.runQueue\n");
219 this.queueRunning = true;
221 var cmds = new Array<GitMonitorQueue>();
223 for(var i = 0; i < this.queue.length; i++) {
224 cmds.append_val(this.queue.index(i));
227 this.queue = new Array<GitMonitorQueue>();// empty queue!
230 string[] success = {};
231 string[] failure = {};
232 //var repos = new Array<GitRepo>(); //??
233 //var done = new Array<GitMonitorQueue>();
235 // first build a array of repo's to work with
236 var repo_list = new Array<GitRepo>();
240 //print(JSON.stringify(cmds));
241 // make sure nothing get's added to the queue where we are doing this..
245 GLib.debug("GitMonitor.runQueue - creating repos");
247 for(var i = 0; i < cmds.length; i++) {
249 var cmd = cmds.index(i);
251 var gitpath = cmd.gitpath;
252 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
254 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
256 repo_list.append_val( GitRepo.get( gitpath ));
257 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
259 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
261 //if (typeof(repo_list[gitpath]) == 'undefined') {
262 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
263 // repo_list[gitpath].cmds = [];
264 // repo_list[gitpath].pull();
266 repo_list.index(ix).cmds.append_val(cmd);
270 // build add, remove and commit message list..
272 GLib.debug("GitMonitor.runQueue - creating actions");
274 for(var i = 0;i < repo_list.length;i++) {
276 var repo = repo_list.index(i);
278 var add_files = new Array<GitMonitorQueue>();
279 var add_files_f = new Array<GitMonitorQueue>();
280 var remove_files = new Array<GitMonitorQueue>();
281 var messages = new Array<GitMonitorQueue>();
282 //print(JSON.stringify(repo.cmds,null,4));
284 for(var ii = 0;ii < repo.cmds.length;ii++) {
285 var cmd = repo.cmds.index(ii);
287 if (repo.is_ignore(cmd.vname)) {
295 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
299 add_files.append_val(cmd);
303 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
307 // if file exists, do not try and delete it.
308 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
312 remove_files.append_val(cmd);
316 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
319 messages.append_val(cmd);
323 stdout.printf("Opps unmatched action %s\n", cmd.action);
327 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
328 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
331 // these can fail... at present... as we wildcard stuff.
333 // make sure added files do not get removed.. ??
335 var remove_files_f = new Array<GitMonitorQueue>();
336 for(var ii = 0;ii < remove_files.length;ii++) {
337 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
340 remove_files_f.append_val(remove_files.index(ii));
342 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
345 // if file was added, then removed,
346 var remove_files_f = new Array<GitMonitorQueue>();
347 for(var ii = 0;ii < remove_files.length;ii++) {
351 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
352 // in add and remove - do not remvove
355 remove_files_f.append_val(remove_files.index(ii));
357 for(var ii = 0;ii < add_files.length;ii++) {
358 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
359 // the add file is in the remove list, and it does not exist - do not add it..
360 print("check exists ? %s\n",add_files.index(ii).fullpath());
362 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
370 add_files_f.append_val(add_files.index(ii));
373 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
374 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
376 if (add_files_f.length < 1 && remove_files_f.length < 1) {
380 // make sure monitoring is paused so it does not recursively pick up
389 this.pauseError(e.message);
390 //failure += e.message;
391 //print("Pull failed:\n");
397 repo.add(add_files_f);
399 this.pauseError(e.message);
401 failure += e.message;
402 GLib.debug("Add failed:");
405 repo.remove(remove_files_f);
407 this.pauseError(e.message);
409 failure += e.message;
410 GLib.debug("Remove failed:");
415 success += repo.commit(
416 GitMonitorQueue.messageToString(messages),
419 success += repo.push();
423 // if the error is 'nothing to commit, working tree clean'
424 // then it's not an error, - just continue;
425 if (/nothing to commit, working tree clean/.match(e.message)) {
426 GLib.debug("%s",e.message);
427 success += e.message;
435 this.pauseError(e.message);
438 //failure += e.message;
439 //print("Push failed:\n");
446 // finally merge all the commit messages.
449 // catch notification failures.. so we can carry on..
450 if (success.length > 0) {
453 var notification = new Notify.Notification(
455 string.joinv("\n",success),
460 notification.set_timeout(5);
464 //if (failure.length > 0) {
466 // should never get this far...
467 // this.pauseError();
470 GLib.debug(e.message);
473 this.queueRunning = false;
480 //string[] just_created;
489 public override void onChanged(MonitorNamePathDir src)
491 //print("GitMonitor.onChanged\n");
492 return; // always ignore this..?
493 //this.parsePath(src);
499 * results in git add + git commit..
502 public override void onChangesDoneHint(MonitorNamePathDir src)
508 GLib.debug("GitMonitor.onChangedHint");
510 this.lastAdd = new DateTime.now(new TimeZone.local());
511 var cmd = new GitMonitorQueue(src);
512 if (cmd.shouldIgnore()) {
513 GLib.debug("GitMonitor.onChangedHint - ignored");
517 //var add_it = false;
519 if (this.is_just_created(cmd.path)) {
521 if (typeof(this.just_created[src.path]) !='undefined') {
522 delete this.just_created[src.path];
525 [ src.gitpath, 'add', src.vpath ],
526 [ src.gitpath, 'commit', { message: src.vpath} ]
534 this.queue.append_val(cmd);
536 cmd = new GitMonitorQueue(src);
537 cmd.action = "commit";
538 cmd.message = cmd.vname;
539 this.queue.append_val(cmd);
543 public override void onDeleted(MonitorNamePathDir src)
549 GLib.debug("GitMonitor.onDeleted");
550 this.lastAdd = new DateTime.now(new TimeZone.local());
551 var cmd = new GitMonitorQueue(src);
552 if (cmd.shouldIgnore()) {
555 // should check if monitor needs removing..
556 // it should also check if it was a directory.. - so we dont have to commit all..
558 this.queue.append_val(cmd);
560 cmd = new GitMonitorQueue(src);
561 cmd.action = "commit";
562 cmd.message = cmd.vname;
563 cmd.commit_all = true;
565 this.queue.append_val(cmd);
568 public override void onCreated(MonitorNamePathDir src) {
573 GLib.debug("GitMonitor.onCreated");
574 this.lastAdd = new DateTime.now(new TimeZone.local());
575 var cmd = new GitMonitorQueue(src);
576 if (cmd.shouldIgnore()) {
580 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
581 // this.just_created[src.path] = true;
582 return; // we do not handle file create flags... - use done hint.
584 // directory has bee created
585 this.monitor(src.path);
586 //this.top.append_val(src.path);
587 //this.monitor(src.path );
590 // -- no point in adding a dir.. as git does not handle them...
592 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
593 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
599 public override void onAttributeChanged(MonitorNamePathDir src) {
604 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
605 if (src.dir == GitMonitor.gitlive) {
606 return; // attribute on top level..
609 this.lastAdd = new DateTime.now(new TimeZone.local());
610 var cmd = new GitMonitorQueue(src);
611 if (cmd.shouldIgnore()) {
615 this.queue.append_val(cmd);
617 cmd = new GitMonitorQueue(src);
618 cmd.action = "commit";
619 cmd.message = "Attribute changed " + cmd.vname;
620 this.queue.append_val(cmd);
623 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
629 GLib.debug("GitMonitor.onMoved");
630 this.lastAdd = new DateTime.now(new TimeZone.local());
631 var cmd_s = new GitMonitorQueue(src);
633 var cmd_d = new GitMonitorQueue(dest);
636 if (cmd_d.gitpath != cmd_s.gitpath) {
638 this.onCreated(dest);
639 this.onChangesDoneHint(dest);
642 // needs to handle move to/from unsupported types..
644 if (cmd_s.shouldIgnore()) {
645 this.onCreated(dest);
646 this.onChangesDoneHint(dest);
650 if (cmd_d.shouldIgnore()) {
659 GLib.debug("RM: %s", cmd_s.vname);
661 this.queue.append_val(cmd_s);
665 GLib.debug("ADD: %s", cmd_d.vname);
666 cmd_d.action = "add";
667 this.queue.append_val(cmd_d);
670 var cmd = new GitMonitorQueue(dest);
671 cmd.action = "commit";
672 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
673 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
674 cmd.message = cmd_d.vname;
677 this.queue.append_val(cmd);