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, "phone-outgoing-busy");
240 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
243 context.play_full (0, props, null);
249 this.queueRunning = false;
250 // what does this do to the old one...
251 this.queue = new Array<GitMonitorQueue> ();
253 StatusIconA.statusicon.pauseError();
255 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
256 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
257 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
260 m.set_keep_above(true);
262 m.set_position(Gtk.WindowPosition.CENTER);
263 m.response.connect( (id_pressed) => {
269 public new void pause() {
271 // what does this do to the old one...
272 this.queue = new Array<GitMonitorQueue> ();
273 StatusIconA.statusicon.pause();
278 public new void resume () {
280 this.queue = new Array<GitMonitorQueue> ();
281 StatusIconA.statusicon.resume();
287 * Start the monitoring
288 * and run the queue every 500 milliseconds..
291 public new void start()
293 StatusIconA.statusicon.refreshing();
296 this.lastAdd = new DateTime.now(new TimeZone.local());
298 Timeout.add_full(Priority.LOW, 500, () => {
299 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
300 // call this.monitor on each of 'top'
301 for(int i = 0; i < this.top.length ; i++) {
303 this.monitor(this.top.index(i) );
305 StatusIconA.statusicon.resume();
312 var notification = new Notify.Notification(
314 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
318 notification.set_timeout(5);
321 GLib.debug("Error sending notification to screen: %s\n",e.message);
323 return false; // do not keep doing this..
333 public new void stop() {
334 StatusIconA.statusicon.pause();
339 public new void monitor (string path, int depth = 0)
342 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
345 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
346 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
351 // FIXME - check if repo is flagged as not autocommit..
352 //var repo = imports.Scm.Repo.Repo.get(path);
353 //if (!repo || !repo.autocommit()) {
359 // check if the repo is to be monitored.
360 //print("PATH : " + path);
363 base.monitor(path, depth);
370 * - pulls the items off the queue
371 * (as commands run concurrently and new items may get added while it's running)
372 * - runs the queue items
376 public void runQueue()
382 GLib.debug("GitMonitor.runQueue\n");
384 this.queueRunning = true;
386 var cmds = new Array<GitMonitorQueue>();
388 for(var i = 0; i < this.queue.length; i++) {
389 cmds.append_val(this.queue.index(i));
392 this.queue = new Array<GitMonitorQueue>();// empty queue!
395 string[] success = {};
396 string[] failure = {};
397 //var repos = new Array<GitRepo>(); //??
398 //var done = new Array<GitMonitorQueue>();
400 // first build a array of repo's to work with
401 var repo_list = new Array<GitRepo>();
405 //print(JSON.stringify(cmds));
406 // make sure nothing get's added to the queue where we are doing this..
410 GLib.debug("GitMonitor.runQueue - creating repos\n");
412 for(var i = 0; i < cmds.length; i++) {
414 var cmd = cmds.index(i);
416 var gitpath = cmd.gitpath;
417 GLib.debug("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
419 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
421 repo_list.append_val(new GitRepo( gitpath ));
422 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
424 GLib.debug("GitMonitor.runQueue - adding to repolist %d\n", ix);
426 //if (typeof(repo_list[gitpath]) == 'undefined') {
427 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
428 // repo_list[gitpath].cmds = [];
429 // repo_list[gitpath].pull();
431 repo_list.index(ix).cmds.append_val(cmd);
435 // build add, remove and commit message list..
437 GLib.debug("GitMonitor.runQueue - creating actions\n");
439 for(var i = 0;i < repo_list.length;i++) {
441 var repo = repo_list.index(i);
443 var add_files = new Array<GitMonitorQueue>();
444 var add_files_f = new Array<GitMonitorQueue>();
445 var remove_files = new Array<GitMonitorQueue>();
446 var messages = new Array<GitMonitorQueue>();
447 //print(JSON.stringify(repo.cmds,null,4));
449 for(var ii = 0;ii < repo.cmds.length;ii++) {
450 var cmd = repo.cmds.index(ii);
452 if (repo.is_ignore(cmd.vname)) {
460 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
464 add_files.append_val(cmd);
468 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
472 // if file exists, do not try and delete it.
473 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
477 remove_files.append_val(cmd);
481 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
484 messages.append_val(cmd);
488 stdout.printf("Opps unmatched action %s\n", cmd.action);
492 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
493 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
496 // these can fail... at present... as we wildcard stuff.
498 // make sure added files do not get removed.. ??
500 var remove_files_f = new Array<GitMonitorQueue>();
501 for(var ii = 0;ii < remove_files.length;ii++) {
502 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
505 remove_files_f.append_val(remove_files.index(ii));
507 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
510 // if file was added, then removed,
511 var remove_files_f = new Array<GitMonitorQueue>();
512 for(var ii = 0;ii < remove_files.length;ii++) {
516 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
517 // in add and remove - do not remvove
520 remove_files_f.append_val(remove_files.index(ii));
522 for(var ii = 0;ii < add_files.length;ii++) {
523 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
524 // the add file is in the remove list, and it does not exist - do not add it..
525 print("check exists ? %s\n",add_files.index(ii).fullpath());
527 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
535 add_files_f.append_val(add_files.index(ii));
538 GLib.debug( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
539 GLib.debug( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
541 if (add_files_f.length < 1 && remove_files_f.length < 1) {
545 // make sure monitoring is paused so it does not recursively pick up
554 this.pauseError(e.message);
555 //failure += e.message;
556 //print("Pull failed:\n");
562 repo.add(add_files_f);
564 this.pauseError(e.message);
566 failure += e.message;
567 GLib.debug("Add failed:\n");
570 repo.remove(remove_files_f);
572 this.pauseError(e.message);
574 failure += e.message;
575 GLib.debug("Remove failed:\n");
580 success += repo.commit(
581 GitMonitorQueue.messageToString(messages),
584 success += repo.push();
588 // if the error is 'nothing to commit, working tree clean'
589 // then it's not an error, - just continue;
590 if (/nothing to commit, working tree clean/.match(e.message)) {
591 GLib.debug("%s\n",e.message);
592 success += e.message;
600 this.pauseError(e.message);
603 //failure += e.message;
604 //print("Push failed:\n");
611 // finally merge all the commit messages.
614 // catch notification failures.. so we can carry on..
615 if (success.length > 0) {
618 var notification = new Notify.Notification(
620 string.joinv("\n",success),
625 notification.set_timeout(5);
629 //if (failure.length > 0) {
631 // should never get this far...
632 // this.pauseError();
635 GLib.debug(e.message);
638 this.queueRunning = false;
645 //string[] just_created;
654 public override void onChanged(MonitorNamePathDir src)
656 //print("GitMonitor.onChanged\n");
657 return; // always ignore this..?
658 //this.parsePath(src);
664 * results in git add + git commit..
667 public override void onChangesDoneHint(MonitorNamePathDir src)
673 GLib.debug("GitMonitor.onChangedHint\n");
675 this.lastAdd = new DateTime.now(new TimeZone.local());
676 var cmd = new GitMonitorQueue(src);
677 if (cmd.shouldIgnore()) {
682 //var add_it = false;
684 if (this.is_just_created(cmd.path)) {
686 if (typeof(this.just_created[src.path]) !='undefined') {
687 delete this.just_created[src.path];
690 [ src.gitpath, 'add', src.vpath ],
691 [ src.gitpath, 'commit', { message: src.vpath} ]
699 this.queue.append_val(cmd);
701 cmd = new GitMonitorQueue(src);
702 cmd.action = "commit";
703 cmd.message = cmd.vname;
704 this.queue.append_val(cmd);
708 public override void onDeleted(MonitorNamePathDir src)
714 GLib.debug("GitMonitor.onDeleted\n");
715 this.lastAdd = new DateTime.now(new TimeZone.local());
716 var cmd = new GitMonitorQueue(src);
717 if (cmd.shouldIgnore()) {
720 // should check if monitor needs removing..
721 // it should also check if it was a directory.. - so we dont have to commit all..
723 this.queue.append_val(cmd);
725 cmd = new GitMonitorQueue(src);
726 cmd.action = "commit";
727 cmd.message = cmd.vname;
728 cmd.commit_all = true;
730 this.queue.append_val(cmd);
733 public override void onCreated(MonitorNamePathDir src) {
738 GLib.debug("GitMonitor.onCreated\n");
739 this.lastAdd = new DateTime.now(new TimeZone.local());
740 var cmd = new GitMonitorQueue(src);
741 if (cmd.shouldIgnore()) {
745 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
746 // this.just_created[src.path] = true;
747 return; // we do not handle file create flags... - use done hint.
749 // directory has bee created
750 this.monitor(src.path);
751 //this.top.append_val(src.path);
752 //this.monitor(src.path );
755 // -- no point in adding a dir.. as git does not handle them...
757 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
758 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
764 public override void onAttributeChanged(MonitorNamePathDir src) {
769 GLib.debug("GitMonitor.onAttributeChanged\n");
770 if (src.dir == GitMonitor.gitlive) {
771 return; // attribute on top level..
774 this.lastAdd = new DateTime.now(new TimeZone.local());
775 var cmd = new GitMonitorQueue(src);
776 if (cmd.shouldIgnore()) {
780 this.queue.append_val(cmd);
782 cmd = new GitMonitorQueue(src);
783 cmd.action = "commit";
784 cmd.message = "Attribute changed " + cmd.vname;
785 this.queue.append_val(cmd);
788 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
794 GLib.debug("GitMonitor.onMoved\n");
795 this.lastAdd = new DateTime.now(new TimeZone.local());
796 var cmd_s = new GitMonitorQueue(src);
798 var cmd_d = new GitMonitorQueue(dest);
801 if (cmd_d.gitpath != cmd_s.gitpath) {
803 this.onCreated(dest);
804 this.onChangesDoneHint(dest);
807 // needs to handle move to/from unsupported types..
809 if (cmd_s.shouldIgnore()) {
810 this.onCreated(dest);
811 this.onChangesDoneHint(dest);
815 if (cmd_d.shouldIgnore()) {
824 GLib.debug("RM: %s\n", cmd_s.vname);
826 this.queue.append_val(cmd_s);
830 GLib.debug("ADD: %s\n", cmd_d.vname);
831 cmd_d.action = "add";
832 this.queue.append_val(cmd_d);
835 var cmd = new GitMonitorQueue(dest);
836 cmd.action = "commit";
837 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
838 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
839 cmd.message = cmd_d.vname;
842 this.queue.append_val(cmd);