4 public class GitMonitorQueue : MonitorNamePathDir {
10 public string vdir; // relative path (within git)
11 public string vname; // relative filename (within git)
12 public string message ; // for commit
13 public bool commit_all;
15 public GitMonitorQueue(MonitorNamePathDir f) {
17 base(f.name, f.path, f.dir);
20 this.commit_all = false;
22 var vpath_ar = this.dir.substring(GitMonitor.gitlive.length +1).split("/", 0);
24 if (vpath_ar[0].length < 1) {
32 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
35 for (var i = 1; i< vpath_ar.length; i++) {
39 this.vdir = string.joinv("/", vpath);
41 this.vname = this.vdir + (this.vdir.length > 0 ? "/" : "") + this.name;
46 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
47 "gitpath: %s\nvdir: %s\nvname: %s\n",
48 this.name, this.path, this.dir,
49 this.gitpath, this.vdir, this.vname
53 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
58 public bool shouldIgnore()
62 // vim.. what a seriously brain dead program..
63 if (this.name == "4913") {
67 if (this.name[0] == '.') {
69 if (this.name == ".htaccess") {
72 if (this.name == ".gitignore") {
79 if (this.name[this.name.length -1] == '~') {
82 // netbeans / android studio.. silly temp files..
84 if (Regex.match_simple("___jb_old___$", this.name)) {
87 if (Regex.match_simple("___jb_bak___$", this.name)) {
90 //if (f.name.match(/^nbproject/)) {
93 // ignore anything in top level!!!!
94 if (this.gitpath.length < 1) {
103 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
105 for(var i =0; i < add_files.length; i++) {
106 if (add_files.index(i).vname == add) {
112 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
113 for(var i =0; i < messages.length; i++) {
114 if (messages.index(i).message == message) {
120 public static string messageToString(Array<GitMonitorQueue> messages ) {
122 for(var i =0; i < messages.length; i++) {
123 ret+= messages.index(i).message;
125 return string.joinv("\n",ret);
127 public static string queueArrayToString(Array<GitMonitorQueue> list) {
129 for(var i =0; i < list.length; i++) {
131 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
137 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
138 for(var i =0; i < list.length; i++) {
139 var test = list.index(i);
140 if (list.index(i).gitpath != cmd_s.gitpath) {
143 if (list.index(i).vname != cmd_s.vname) {
146 if (list.index(i).action != action) {
153 public string fullpath()
155 return this.gitpath + "/" + this.vname;
164 public class GitMonitor : Monitor
167 public static GitMonitor gitmonitor;
169 * @property {String} the "gitlive" directory, normally ~/gitlive
170 * dset by OWNER... - we should do this as a CTOR.
173 public static string gitlive;
176 public Array<GitMonitorQueue> queue ;
177 public bool queueRunning = false;
179 public DateTime lastAdd;
183 public GitMonitor () {
187 this.queue = new Array<GitMonitorQueue>();
188 GitMonitor.gitmonitor = this;
193 Timeout.add_full(Priority.LOW, 1000, () => {
195 //print("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
197 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
198 if (this.queue.length < 1 || this.queueRunning) {
202 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
204 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
205 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
207 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
210 //_this.lastAdd = new Date();
220 public new void pauseError(string failure)
223 var notification = new Notify.Notification(
230 notification.set_timeout(60); // show errros for longer
233 Canberra.Context context;
234 Canberra.Proplist props;
236 Canberra.Context.create (out context);
237 Canberra.Proplist.create (out props);
239 props.sets (Canberra.PROP_EVENT_ID, "gitlive-error");
240 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
241 props.sets (Canberra.PROP_CANBERRA_CACHE_CONTROL, "permanent");
242 props.sets (Canberra.PROP_MEDIA_ROLE, "event");
244 context.play_full (0, props, null);
250 this.queueRunning = false;
251 // what does this do to the old one...
252 this.queue = new Array<GitMonitorQueue> ();
254 StatusIconA.statusicon.pauseError();
256 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
257 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
258 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
261 m.set_keep_above(true);
263 m.set_position(Gtk.WindowPosition.CENTER);
264 m.response.connect( (id_pressed) => {
270 public new void pause() {
272 // what does this do to the old one...
273 this.queue = new Array<GitMonitorQueue> ();
274 StatusIconA.statusicon.pause();
279 public new void resume () {
281 this.queue = new Array<GitMonitorQueue> ();
282 StatusIconA.statusicon.resume();
288 * Start the monitoring
289 * and run the queue every 500 milliseconds..
292 public new void start()
294 StatusIconA.statusicon.refreshing();
297 this.lastAdd = new DateTime.now(new TimeZone.local());
299 Timeout.add_full(Priority.LOW, 500, () => {
300 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
301 // call this.monitor on each of 'top'
302 for(int i = 0; i < this.top.length ; i++) {
304 this.monitor(this.top.index(i) );
306 StatusIconA.statusicon.resume();
313 var notification = new Notify.Notification(
315 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
319 notification.set_timeout(5);
322 GLib.debug("Error sending notification to screen: %s\n",e.message);
324 return false; // do not keep doing this..
334 public new void stop() {
335 StatusIconA.statusicon.pause();
340 public new void monitor (string path, int depth = 0)
343 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
346 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
347 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
352 // FIXME - check if repo is flagged as not autocommit..
353 //var repo = imports.Scm.Repo.Repo.get(path);
354 //if (!repo || !repo.autocommit()) {
360 // check if the repo is to be monitored.
361 //print("PATH : " + path);
364 base.monitor(path, depth);
371 * - pulls the items off the queue
372 * (as commands run concurrently and new items may get added while it's running)
373 * - runs the queue items
377 public void runQueue()
383 GLib.debug("GitMonitor.runQueue\n");
385 this.queueRunning = true;
387 var cmds = new Array<GitMonitorQueue>();
389 for(var i = 0; i < this.queue.length; i++) {
390 cmds.append_val(this.queue.index(i));
393 this.queue = new Array<GitMonitorQueue>();// empty queue!
396 string[] success = {};
397 string[] failure = {};
398 //var repos = new Array<GitRepo>(); //??
399 //var done = new Array<GitMonitorQueue>();
401 // first build a array of repo's to work with
402 var repo_list = new Array<GitRepo>();
406 //print(JSON.stringify(cmds));
407 // make sure nothing get's added to the queue where we are doing this..
411 GLib.debug("GitMonitor.runQueue - creating repos\n");
413 for(var i = 0; i < cmds.length; i++) {
415 var cmd = cmds.index(i);
417 var gitpath = cmd.gitpath;
418 GLib.debug("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
420 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
422 repo_list.append_val(new GitRepo( gitpath ));
423 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
425 GLib.debug("GitMonitor.runQueue - adding to repolist %d\n", ix);
427 //if (typeof(repo_list[gitpath]) == 'undefined') {
428 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
429 // repo_list[gitpath].cmds = [];
430 // repo_list[gitpath].pull();
432 repo_list.index(ix).cmds.append_val(cmd);
436 // build add, remove and commit message list..
438 GLib.debug("GitMonitor.runQueue - creating actions\n");
440 for(var i = 0;i < repo_list.length;i++) {
442 var repo = repo_list.index(i);
444 var add_files = new Array<GitMonitorQueue>();
445 var add_files_f = new Array<GitMonitorQueue>();
446 var remove_files = new Array<GitMonitorQueue>();
447 var messages = new Array<GitMonitorQueue>();
448 //print(JSON.stringify(repo.cmds,null,4));
450 for(var ii = 0;ii < repo.cmds.length;ii++) {
451 var cmd = repo.cmds.index(ii);
453 if (repo.is_ignore(cmd.vname)) {
461 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
465 add_files.append_val(cmd);
469 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
473 // if file exists, do not try and delete it.
474 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
478 remove_files.append_val(cmd);
482 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
485 messages.append_val(cmd);
489 stdout.printf("Opps unmatched action %s\n", cmd.action);
493 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
494 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
497 // these can fail... at present... as we wildcard stuff.
499 // make sure added files do not get removed.. ??
501 var remove_files_f = new Array<GitMonitorQueue>();
502 for(var ii = 0;ii < remove_files.length;ii++) {
503 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
506 remove_files_f.append_val(remove_files.index(ii));
508 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
511 // if file was added, then removed,
512 var remove_files_f = new Array<GitMonitorQueue>();
513 for(var ii = 0;ii < remove_files.length;ii++) {
517 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
518 // in add and remove - do not remvove
521 remove_files_f.append_val(remove_files.index(ii));
523 for(var ii = 0;ii < add_files.length;ii++) {
524 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
525 // the add file is in the remove list, and it does not exist - do not add it..
526 print("check exists ? %s\n",add_files.index(ii).fullpath());
528 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
536 add_files_f.append_val(add_files.index(ii));
539 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
540 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
542 if (add_files_f.length < 1 && remove_files_f.length < 1) {
546 // make sure monitoring is paused so it does not recursively pick up
555 this.pauseError(e.message);
556 //failure += e.message;
557 //print("Pull failed:\n");
563 repo.add(add_files_f);
565 this.pauseError(e.message);
567 failure += e.message;
568 GLib.debug("Add failed:\n");
571 repo.remove(remove_files_f);
573 this.pauseError(e.message);
575 failure += e.message;
576 GLib.debug("Remove failed:\n");
581 success += repo.commit(
582 GitMonitorQueue.messageToString(messages),
585 success += repo.push();
589 // if the error is 'nothing to commit, working tree clean'
590 // then it's not an error, - just continue;
591 if (/nothing to commit, working tree clean/.match(e.message)) {
592 GLib.debug("%s\n",e.message);
593 success += e.message;
601 this.pauseError(e.message);
604 //failure += e.message;
605 //print("Push failed:\n");
612 // finally merge all the commit messages.
615 // catch notification failures.. so we can carry on..
616 if (success.length > 0) {
619 var notification = new Notify.Notification(
621 string.joinv("\n",success),
626 notification.set_timeout(5);
630 //if (failure.length > 0) {
632 // should never get this far...
633 // this.pauseError();
636 GLib.debug(e.message);
639 this.queueRunning = false;
646 //string[] just_created;
655 public override void onChanged(MonitorNamePathDir src)
657 //print("GitMonitor.onChanged\n");
658 return; // always ignore this..?
659 //this.parsePath(src);
665 * results in git add + git commit..
668 public override void onChangesDoneHint(MonitorNamePathDir src)
674 GLib.debug("GitMonitor.onChangedHint\n");
676 this.lastAdd = new DateTime.now(new TimeZone.local());
677 var cmd = new GitMonitorQueue(src);
678 if (cmd.shouldIgnore()) {
683 //var add_it = false;
685 if (this.is_just_created(cmd.path)) {
687 if (typeof(this.just_created[src.path]) !='undefined') {
688 delete this.just_created[src.path];
691 [ src.gitpath, 'add', src.vpath ],
692 [ src.gitpath, 'commit', { message: src.vpath} ]
700 this.queue.append_val(cmd);
702 cmd = new GitMonitorQueue(src);
703 cmd.action = "commit";
704 cmd.message = cmd.vname;
705 this.queue.append_val(cmd);
709 public override void onDeleted(MonitorNamePathDir src)
715 GLib.debug("GitMonitor.onDeleted\n");
716 this.lastAdd = new DateTime.now(new TimeZone.local());
717 var cmd = new GitMonitorQueue(src);
718 if (cmd.shouldIgnore()) {
721 // should check if monitor needs removing..
722 // it should also check if it was a directory.. - so we dont have to commit all..
724 this.queue.append_val(cmd);
726 cmd = new GitMonitorQueue(src);
727 cmd.action = "commit";
728 cmd.message = cmd.vname;
729 cmd.commit_all = true;
731 this.queue.append_val(cmd);
734 public override void onCreated(MonitorNamePathDir src) {
739 GLib.debug("GitMonitor.onCreated\n");
740 this.lastAdd = new DateTime.now(new TimeZone.local());
741 var cmd = new GitMonitorQueue(src);
742 if (cmd.shouldIgnore()) {
746 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
747 // this.just_created[src.path] = true;
748 return; // we do not handle file create flags... - use done hint.
750 // directory has bee created
751 this.monitor(src.path);
752 //this.top.append_val(src.path);
753 //this.monitor(src.path );
756 // -- no point in adding a dir.. as git does not handle them...
758 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
759 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
765 public override void onAttributeChanged(MonitorNamePathDir src) {
770 GLib.debug("GitMonitor.onAttributeChanged\n");
771 if (src.dir == GitMonitor.gitlive) {
772 return; // attribute on top level..
775 this.lastAdd = new DateTime.now(new TimeZone.local());
776 var cmd = new GitMonitorQueue(src);
777 if (cmd.shouldIgnore()) {
781 this.queue.append_val(cmd);
783 cmd = new GitMonitorQueue(src);
784 cmd.action = "commit";
785 cmd.message = "Attribute changed " + cmd.vname;
786 this.queue.append_val(cmd);
789 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
795 GLib.debug("GitMonitor.onMoved\n");
796 this.lastAdd = new DateTime.now(new TimeZone.local());
797 var cmd_s = new GitMonitorQueue(src);
799 var cmd_d = new GitMonitorQueue(dest);
802 if (cmd_d.gitpath != cmd_s.gitpath) {
804 this.onCreated(dest);
805 this.onChangesDoneHint(dest);
808 // needs to handle move to/from unsupported types..
810 if (cmd_s.shouldIgnore()) {
811 this.onCreated(dest);
812 this.onChangesDoneHint(dest);
816 if (cmd_d.shouldIgnore()) {
825 GLib.debug("RM: %s\n", cmd_s.vname);
827 this.queue.append_val(cmd_s);
831 GLib.debug("ADD: %s\n", cmd_d.vname);
832 cmd_d.action = "add";
833 this.queue.append_val(cmd_d);
836 var cmd = new GitMonitorQueue(dest);
837 cmd.action = "commit";
838 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
839 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
840 cmd.message = cmd_d.vname;
843 this.queue.append_val(cmd);