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()
61 // vim.. what a seriously brain dead program..
62 if (this.name == "4913") {
63 GLib.debug("ignore name = 4913");
68 if (this.name[0] == '.') {
70 if (this.name == ".htaccess") {
74 if (this.name == ".gitignore") {
77 GLib.debug("ignore name starts with dot %s", this.name);
82 if (this.name[this.name.length -1] == '~') {
83 GLib.debug("ignore name ends with ~");
86 // netbeans / android studio.. silly temp files..
88 if (Regex.match_simple("___jb_old___$", this.name)) {
89 GLib.debug("ignore name includes jb_old");
92 if (Regex.match_simple("___jb_bak___$", this.name)) {
93 GLib.debug("ignore name includes jb_bkc");
96 //if (f.name.match(/^nbproject/)) {
99 // ignore anything in top level!!!!
100 if (this.gitpath.length < 1) {
101 GLib.debug("ignore gitpath length is empty");
110 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
112 for(var i =0; i < add_files.length; i++) {
113 if (add_files.index(i).vname == add) {
119 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
120 for(var i =0; i < messages.length; i++) {
121 if (messages.index(i).message == message) {
127 public static string messageToString(Array<GitMonitorQueue> messages ) {
129 for(var i =0; i < messages.length; i++) {
130 ret+= messages.index(i).message;
132 return string.joinv("\n",ret);
134 public static string queueArrayToString(Array<GitMonitorQueue> list) {
136 for(var i =0; i < list.length; i++) {
138 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
144 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
145 for(var i =0; i < list.length; i++) {
146 var test = list.index(i);
147 if (list.index(i).gitpath != cmd_s.gitpath) {
150 if (list.index(i).vname != cmd_s.vname) {
153 if (list.index(i).action != action) {
160 public string fullpath()
162 return this.gitpath + "/" + this.vname;
171 public class GitMonitor : Monitor
174 public static GitMonitor gitmonitor;
176 * @property {String} the "gitlive" directory, normally ~/gitlive
177 * dset by OWNER... - we should do this as a CTOR.
180 public static string gitlive;
183 public Array<GitMonitorQueue> queue ;
184 public bool queueRunning = false;
186 public DateTime lastAdd;
190 public GitMonitor () {
194 this.queue = new Array<GitMonitorQueue>();
195 GitMonitor.gitmonitor = this;
200 Timeout.add_full(Priority.LOW, 500, () => {
202 //GLib.debug("TIMEOUT queue length = %d, is_runing = %s\n", (int)this.queue.length , this.queueRunning ? "Y" : "N");
204 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
205 if (this.queue.length < 1 || this.queueRunning) {
209 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
211 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
212 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
214 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
217 //_this.lastAdd = new Date();
227 public new void pauseError(string failure)
230 var notification = new Notify.Notification(
237 notification.set_timeout(60); // show errros for longer
240 Canberra.Context context;
241 Canberra.Proplist props;
243 Canberra.Context.create (out context);
244 Canberra.Proplist.create (out props);
246 props.sets (Canberra.PROP_EVENT_ID, "phone-outgoing-busy");
247 props.sets (Canberra.PROP_EVENT_DESCRIPTION, "Gitlive stopped on error");
250 context.play_full (0, props, null);
256 this.queueRunning = false;
257 // what does this do to the old one...
258 this.queue = new Array<GitMonitorQueue> ();
260 StatusIconA.statusicon.pauseError();
262 var m = new Gtk.MessageDialog(null, Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR,Gtk.ButtonsType.CLOSE,
263 "A Serious problem occured running git, you will probably have to resolve this manually\n" +
264 "Committing is paused now, so fix the problem, close this window, then press start again\n\n\n" +
267 m.set_keep_above(true);
269 m.set_position(Gtk.WindowPosition.CENTER);
270 m.response.connect( (id_pressed) => {
276 public new void pause() {
278 // what does this do to the old one...
279 this.queue = new Array<GitMonitorQueue> ();
280 StatusIconA.statusicon.pause();
285 public new void resume () {
287 this.queue = new Array<GitMonitorQueue> ();
288 StatusIconA.statusicon.resume();
294 * Start the monitoring
295 * and run the queue every 500 milliseconds..
298 public new void start()
300 StatusIconA.statusicon.refreshing();
303 this.lastAdd = new DateTime.now(new TimeZone.local());
305 Timeout.add_full(Priority.LOW, 500, () => {
306 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
307 // call this.monitor on each of 'top'
308 for(int i = 0; i < this.top.length ; i++) {
310 this.monitor(this.top.index(i) );
312 StatusIconA.statusicon.resume();
319 var notification = new Notify.Notification(
321 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
325 notification.set_timeout(5);
328 GLib.debug("Error sending notification to screen: %s",e.message);
330 return false; // do not keep doing this..
340 public new void stop() {
341 StatusIconA.statusicon.pause();
346 public new void monitor (string path, int depth = 0)
349 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
352 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
353 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
358 // FIXME - check if repo is flagged as not autocommit..
359 //var repo = imports.Scm.Repo.Repo.get(path);
360 //if (!repo || !repo.autocommit()) {
366 // check if the repo is to be monitored.
367 //print("PATH : " + path);
370 base.monitor(path, depth);
377 * - pulls the items off the queue
378 * (as commands run concurrently and new items may get added while it's running)
379 * - runs the queue items
383 public void runQueue()
389 GLib.debug("GitMonitor.runQueue\n");
391 this.queueRunning = true;
393 var cmds = new Array<GitMonitorQueue>();
395 for(var i = 0; i < this.queue.length; i++) {
396 cmds.append_val(this.queue.index(i));
399 this.queue = new Array<GitMonitorQueue>();// empty queue!
402 string[] success = {};
403 string[] failure = {};
404 //var repos = new Array<GitRepo>(); //??
405 //var done = new Array<GitMonitorQueue>();
407 // first build a array of repo's to work with
408 var repo_list = new Array<GitRepo>();
412 //print(JSON.stringify(cmds));
413 // make sure nothing get's added to the queue where we are doing this..
417 GLib.debug("GitMonitor.runQueue - creating repos");
419 for(var i = 0; i < cmds.length; i++) {
421 var cmd = cmds.index(i);
423 var gitpath = cmd.gitpath;
424 GLib.debug("GitMonitor.runQueue - finding %s", cmd.gitpath);
426 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
428 repo_list.append_val(new GitRepo( gitpath ));
429 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
431 GLib.debug("GitMonitor.runQueue - adding to repolist %d", ix);
433 //if (typeof(repo_list[gitpath]) == 'undefined') {
434 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
435 // repo_list[gitpath].cmds = [];
436 // repo_list[gitpath].pull();
438 repo_list.index(ix).cmds.append_val(cmd);
442 // build add, remove and commit message list..
444 GLib.debug("GitMonitor.runQueue - creating actions");
446 for(var i = 0;i < repo_list.length;i++) {
448 var repo = repo_list.index(i);
450 var add_files = new Array<GitMonitorQueue>();
451 var add_files_f = new Array<GitMonitorQueue>();
452 var remove_files = new Array<GitMonitorQueue>();
453 var messages = new Array<GitMonitorQueue>();
454 //print(JSON.stringify(repo.cmds,null,4));
456 for(var ii = 0;ii < repo.cmds.length;ii++) {
457 var cmd = repo.cmds.index(ii);
459 if (repo.is_ignore(cmd.vname)) {
467 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
471 add_files.append_val(cmd);
475 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
479 // if file exists, do not try and delete it.
480 if (FileUtils.test(cmd.fullpath(), FileTest.EXISTS)) {
484 remove_files.append_val(cmd);
488 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
491 messages.append_val(cmd);
495 stdout.printf("Opps unmatched action %s\n", cmd.action);
499 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files));
500 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files));
503 // these can fail... at present... as we wildcard stuff.
505 // make sure added files do not get removed.. ??
507 var remove_files_f = new Array<GitMonitorQueue>();
508 for(var ii = 0;ii < remove_files.length;ii++) {
509 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
512 remove_files_f.append_val(remove_files.index(ii));
514 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
517 // if file was added, then removed,
518 var remove_files_f = new Array<GitMonitorQueue>();
519 for(var ii = 0;ii < remove_files.length;ii++) {
523 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
524 // in add and remove - do not remvove
527 remove_files_f.append_val(remove_files.index(ii));
529 for(var ii = 0;ii < add_files.length;ii++) {
530 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
531 // the add file is in the remove list, and it does not exist - do not add it..
532 print("check exists ? %s\n",add_files.index(ii).fullpath());
534 if (!FileUtils.test(add_files.index(ii).fullpath(), FileTest.EXISTS)) {
542 add_files_f.append_val(add_files.index(ii));
545 GLib.debug( "ADD : %s", GitMonitorQueue.queueArrayToString(add_files_f));
546 GLib.debug( "REMOVE FILES: %s", GitMonitorQueue.queueArrayToString(remove_files_f));
548 if (add_files_f.length < 1 && remove_files_f.length < 1) {
552 // make sure monitoring is paused so it does not recursively pick up
561 this.pauseError(e.message);
562 //failure += e.message;
563 //print("Pull failed:\n");
569 repo.add(add_files_f);
571 this.pauseError(e.message);
573 failure += e.message;
574 GLib.debug("Add failed:");
577 repo.remove(remove_files_f);
579 this.pauseError(e.message);
581 failure += e.message;
582 GLib.debug("Remove failed:");
587 success += repo.commit(
588 GitMonitorQueue.messageToString(messages),
591 success += repo.push();
595 // if the error is 'nothing to commit, working tree clean'
596 // then it's not an error, - just continue;
597 if (/nothing to commit, working tree clean/.match(e.message)) {
598 GLib.debug("%s",e.message);
599 success += e.message;
607 this.pauseError(e.message);
610 //failure += e.message;
611 //print("Push failed:\n");
618 // finally merge all the commit messages.
621 // catch notification failures.. so we can carry on..
622 if (success.length > 0) {
625 var notification = new Notify.Notification(
627 string.joinv("\n",success),
632 notification.set_timeout(5);
636 //if (failure.length > 0) {
638 // should never get this far...
639 // this.pauseError();
642 GLib.debug(e.message);
645 this.queueRunning = false;
652 //string[] just_created;
661 public override void onChanged(MonitorNamePathDir src)
663 //print("GitMonitor.onChanged\n");
664 return; // always ignore this..?
665 //this.parsePath(src);
671 * results in git add + git commit..
674 public override void onChangesDoneHint(MonitorNamePathDir src)
680 GLib.debug("GitMonitor.onChangedHint");
682 this.lastAdd = new DateTime.now(new TimeZone.local());
683 var cmd = new GitMonitorQueue(src);
684 if (cmd.shouldIgnore()) {
685 GLib.debug("GitMonitor.onChangedHint - ignored");
689 //var add_it = false;
691 if (this.is_just_created(cmd.path)) {
693 if (typeof(this.just_created[src.path]) !='undefined') {
694 delete this.just_created[src.path];
697 [ src.gitpath, 'add', src.vpath ],
698 [ src.gitpath, 'commit', { message: src.vpath} ]
706 this.queue.append_val(cmd);
708 cmd = new GitMonitorQueue(src);
709 cmd.action = "commit";
710 cmd.message = cmd.vname;
711 this.queue.append_val(cmd);
715 public override void onDeleted(MonitorNamePathDir src)
721 GLib.debug("GitMonitor.onDeleted");
722 this.lastAdd = new DateTime.now(new TimeZone.local());
723 var cmd = new GitMonitorQueue(src);
724 if (cmd.shouldIgnore()) {
727 // should check if monitor needs removing..
728 // it should also check if it was a directory.. - so we dont have to commit all..
730 this.queue.append_val(cmd);
732 cmd = new GitMonitorQueue(src);
733 cmd.action = "commit";
734 cmd.message = cmd.vname;
735 cmd.commit_all = true;
737 this.queue.append_val(cmd);
740 public override void onCreated(MonitorNamePathDir src) {
745 GLib.debug("GitMonitor.onCreated");
746 this.lastAdd = new DateTime.now(new TimeZone.local());
747 var cmd = new GitMonitorQueue(src);
748 if (cmd.shouldIgnore()) {
752 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
753 // this.just_created[src.path] = true;
754 return; // we do not handle file create flags... - use done hint.
756 // directory has bee created
757 this.monitor(src.path);
758 //this.top.append_val(src.path);
759 //this.monitor(src.path );
762 // -- no point in adding a dir.. as git does not handle them...
764 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
765 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
771 public override void onAttributeChanged(MonitorNamePathDir src) {
776 GLib.debug("GitMonitor.onAttributeChanged %s", src.name);
777 if (src.dir == GitMonitor.gitlive) {
778 return; // attribute on top level..
781 this.lastAdd = new DateTime.now(new TimeZone.local());
782 var cmd = new GitMonitorQueue(src);
783 if (cmd.shouldIgnore()) {
787 this.queue.append_val(cmd);
789 cmd = new GitMonitorQueue(src);
790 cmd.action = "commit";
791 cmd.message = "Attribute changed " + cmd.vname;
792 this.queue.append_val(cmd);
795 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
801 GLib.debug("GitMonitor.onMoved");
802 this.lastAdd = new DateTime.now(new TimeZone.local());
803 var cmd_s = new GitMonitorQueue(src);
805 var cmd_d = new GitMonitorQueue(dest);
808 if (cmd_d.gitpath != cmd_s.gitpath) {
810 this.onCreated(dest);
811 this.onChangesDoneHint(dest);
814 // needs to handle move to/from unsupported types..
816 if (cmd_s.shouldIgnore()) {
817 this.onCreated(dest);
818 this.onChangesDoneHint(dest);
822 if (cmd_d.shouldIgnore()) {
831 GLib.debug("RM: %s", cmd_s.vname);
833 this.queue.append_val(cmd_s);
837 GLib.debug("ADD: %s", cmd_d.vname);
838 cmd_d.action = "add";
839 this.queue.append_val(cmd_d);
842 var cmd = new GitMonitorQueue(dest);
843 cmd.action = "commit";
844 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
845 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
846 cmd.message = cmd_d.vname;
849 this.queue.append_val(cmd);