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;
44 "NEW GitMonitorQueue\nname: %s\npath: %s\ndir: %s\n" +
45 "gitpath: %s\nvdir: %s\nvname: %s\n",
46 this.name, this.path, this.dir,
47 this.gitpath, this.vdir, this.vname
51 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
56 public bool shouldIgnore()
60 // vim.. what a seriously brain dead program..
61 if (this.name == "4913") {
65 if (this.name[0] == '.') {
67 if (this.name == ".htaccess") {
73 if (this.name[this.name.length -1] == '~') {
77 //if (f.name.match(/~$/)) {
80 //if (f.name.match(/^nbproject/)) {
83 // ignore anything in top level!!!!
84 if (this.gitpath.length < 1) {
93 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add)
95 for(var i =0; i < add_files.length; i++) {
96 if (add_files.index(i).vname == add) {
102 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
103 for(var i =0; i < messages.length; i++) {
104 if (messages.index(i).message == message) {
110 public static string messageToString(Array<GitMonitorQueue> messages ) {
112 for(var i =0; i < messages.length; i++) {
113 ret+= messages.index(i).message;
115 return string.joinv("\n",ret);
117 public static string queueArrayToString(Array<GitMonitorQueue> list) {
119 for(var i =0; i < list.length; i++) {
121 ret += (ret.length > 0 ? ", " : "") + list.index(i).vname;
127 public static bool queueHas(Array<GitMonitorQueue> list , GitMonitorQueue cmd_s, string action) {
128 for(var i =0; i < list.length; i++) {
129 var test = list.index(i);
130 if (list.index(i).gitpath != cmd_s.gitpath) {
133 if (list.index(i).vname != cmd_s.vname) {
136 if (list.index(i).action != action) {
143 public string fullpath()
145 return this.gitpath + this.vdir + this.vname;
154 public class GitMonitor : Monitor
157 public static GitMonitor gitmonitor;
159 * @property {String} the "gitlive" directory, normally ~/gitlive
160 * dset by OWNER... - we should do this as a CTOR.
163 public static string gitlive;
166 public Array<GitMonitorQueue> queue ;
167 public bool queueRunning = false;
169 public DateTime lastAdd;
172 public GitMonitor () {
173 this.queue = new Array<GitMonitorQueue>();
174 GitMonitor.gitmonitor = this;
180 public new void pause() {
182 // what does this do to the old one...
183 this.queue = new Array<GitMonitorQueue> ();
184 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
188 public new void resume () {
190 this.queue = new Array<GitMonitorQueue> ();
191 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
196 * Start the monitoring
197 * and run the queue every 500 milliseconds..
200 public new void start() {
201 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
204 this.lastAdd = new DateTime.now(new TimeZone.local());
206 Timeout.add_full(Priority.LOW, 500, () => {
207 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
208 // call this.monitor on each of 'top'
209 for(int i = 0; i < this.top.length ; i++) {
211 this.monitor(this.top.index(i) );
213 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
220 var notification = new Notify.Notification(
222 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
226 notification.set_timeout(5);
231 return false; // do not keep doing this..
236 Timeout.add_full(Priority.LOW, 1000, () => {
237 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
239 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
240 if (this.queue.length < 1 || this.queueRunning) {
244 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
246 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
247 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
249 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
252 //_this.lastAdd = new Date();
262 public new void stop() {
263 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
268 public new void monitor (string path, int depth = 0)
271 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
274 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
275 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
280 // FIXME - check if repo is flagged as not autocommit..
281 //var repo = imports.Scm.Repo.Repo.get(path);
282 //if (!repo || !repo.autocommit()) {
288 // check if the repo is to be monitored.
289 //print("PATH : " + path);
292 base.monitor(path, depth);
299 * - pulls the items off the queue
300 * (as commands run concurrently and new items may get added while it's running)
301 * - runs the queue items
305 public void runQueue()
311 print("GitMonitor.runQueue\n");
313 this.queueRunning = true;
315 var cmds = new Array<GitMonitorQueue>();
317 for(var i = 0; i < this.queue.length; i++) {
318 cmds.append_val(this.queue.index(i));
321 this.queue = new Array<GitMonitorQueue>();// empty queue!
324 string[] success = {};
325 string[] failure = {};
326 //var repos = new Array<GitRepo>(); //??
327 //var done = new Array<GitMonitorQueue>();
329 // first build a array of repo's to work with
330 var repo_list = new Array<GitRepo>();
334 //print(JSON.stringify(cmds));
335 // make sure nothing get's added to the queue where we are doing this..
339 print("GitMonitor.runQueue - creating repos\n");
341 for(var i = 0; i < cmds.length; i++) {
343 var cmd = cmds.index(i);
345 var gitpath = cmd.gitpath;
346 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
348 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
350 repo_list.append_val(new GitRepo( gitpath ));
351 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
353 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
355 //if (typeof(repo_list[gitpath]) == 'undefined') {
356 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
357 // repo_list[gitpath].cmds = [];
358 // repo_list[gitpath].pull();
360 repo_list.index(ix).cmds.append_val(cmd);
364 // build add, remove and commit message list..
366 print("GitMonitor.runQueue - creating actions\n");
368 for(var i = 0;i < repo_list.length;i++) {
370 var repo = repo_list.index(i);
372 var add_files = new Array<GitMonitorQueue>();
373 var add_files_f = new Array<GitMonitorQueue>();
374 var remove_files = new Array<GitMonitorQueue>();
375 var messages = new Array<GitMonitorQueue>();
376 //print(JSON.stringify(repo.cmds,null,4));
378 for(var ii = 0;ii < repo.cmds.length;ii++) {
379 var cmd = repo.cmds.index(ii);
385 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
389 add_files.append_val(cmd);
393 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
397 // if file exists, do not try and delete it.
398 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
402 remove_files.append_val(cmd);
406 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
409 messages.append_val(cmd);
413 stdout.printf("Opps unmatched action %s\n", cmd.action);
417 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
418 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
420 // these can fail... at present... as we wildcard stuff.
422 // make sure added files do not get removed.. ??
424 var remove_files_f = new Array<GitMonitorQueue>();
425 for(var ii = 0;ii < remove_files.length;ii++) {
426 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
429 remove_files_f.append_val(remove_files.index(ii));
431 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
434 // if file was added, then removed,
435 var remove_files_f = new Array<GitMonitorQueue>();
436 for(var ii = 0;ii < remove_files.length;ii++) {
437 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
438 // in add and remove - do not remvove
441 remove_files_f.append_val(remove_files.index(ii));
443 for(var ii = 0;ii < add_files.length;ii++) {
444 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
445 // the add file is in the remove list, and it does not exist - do not add it..
446 if (!FileUtils.test(add_files.index(ii).vname, FileTest.EXISTS)) {
454 add_files_f.append_val(add_files.index(ii));
457 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
458 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
461 // make sure monitoring is paused so it does not recursively pick up
466 failure += e.message;
472 repo.add(add_files_f);
474 failure += e.message;
477 repo.remove(remove_files_f);
479 failure += e.message;
486 success += repo.commit(
487 GitMonitorQueue.messageToString(messages),
490 success += repo.push();
493 failure += e.message;
498 // finally merge all the commit messages.
501 // catch notification failures.. so we can carry on..
502 if (success.length > 0) {
505 var notification = new Notify.Notification(
507 string.joinv("\n",success),
512 notification.set_timeout(5);
516 if (failure.length > 0) {
518 var notification = new Notify.Notification(
520 string.joinv("\n",failure),
525 notification.set_timeout(5); // show errros for longer
532 this.queueRunning = false;
539 //string[] just_created;
548 public override void onChanged(MonitorNamePathDir src)
550 print("GitMonitor.onChanged\n");
551 return; // always ignore this..?
552 //this.parsePath(src);
558 * results in git add + git commit..
561 public override void onChangesDoneHint(MonitorNamePathDir src)
563 print("GitMonitor.onChangedHint\n");
569 this.lastAdd = new DateTime.now(new TimeZone.local());
570 var cmd = new GitMonitorQueue(src);
571 if (cmd.shouldIgnore()) {
576 //var add_it = false;
578 if (this.is_just_created(cmd.path)) {
580 if (typeof(this.just_created[src.path]) !='undefined') {
581 delete this.just_created[src.path];
584 [ src.gitpath, 'add', src.vpath ],
585 [ src.gitpath, 'commit', { message: src.vpath} ]
593 this.queue.append_val(cmd);
595 cmd = new GitMonitorQueue(src);
596 cmd.action = "commit";
597 cmd.message = cmd.vname;
598 this.queue.append_val(cmd);
602 public override void onDeleted(MonitorNamePathDir src)
604 print("GitMonitor.onDeleted\n");
608 this.lastAdd = new DateTime.now(new TimeZone.local());
609 var cmd = new GitMonitorQueue(src);
610 if (cmd.shouldIgnore()) {
613 // should check if monitor needs removing..
614 // it should also check if it was a directory.. - so we dont have to commit all..
616 this.queue.append_val(cmd);
618 cmd = new GitMonitorQueue(src);
619 cmd.action = "commit";
620 cmd.message = cmd.vname;
621 cmd.commit_all = true;
623 this.queue.append_val(cmd);
626 public override void onCreated(MonitorNamePathDir src) {
627 print("GitMonitor.onCreated\n");
631 this.lastAdd = new DateTime.now(new TimeZone.local());
632 var cmd = new GitMonitorQueue(src);
633 if (cmd.shouldIgnore()) {
637 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
638 // this.just_created[src.path] = true;
639 return; // we do not handle file create flags... - use done hint.
641 // directory has bee created
642 this.monitor(src.path);
643 //this.top.append_val(src.path);
644 //this.monitor(src.path );
647 // -- no point in adding a dir.. as git does not handle them...
649 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
650 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
656 public override void onAttributeChanged(MonitorNamePathDir src) {
657 print("GitMonitor.onAttributeChanged\n");
661 if (src.dir == GitMonitor.gitlive) {
662 return; // attribute on top level..
665 this.lastAdd = new DateTime.now(new TimeZone.local());
666 var cmd = new GitMonitorQueue(src);
667 if (cmd.shouldIgnore()) {
671 this.queue.append_val(cmd);
673 cmd = new GitMonitorQueue(src);
674 cmd.action = "commit";
675 cmd.message = "Attribute changed " + cmd.vname;
676 this.queue.append_val(cmd);
679 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
681 print("GitMonitor.onMoved\n");
685 this.lastAdd = new DateTime.now(new TimeZone.local());
686 var cmd_s = new GitMonitorQueue(src);
688 var cmd_d = new GitMonitorQueue(dest);
691 if (cmd_d.gitpath != cmd_s.gitpath) {
693 this.onCreated(dest);
694 this.onChangesDoneHint(dest);
697 // needs to handle move to/from unsupported types..
699 if (cmd_s.shouldIgnore()) {
700 this.onCreated(dest);
701 this.onChangesDoneHint(dest);
705 if (cmd_d.shouldIgnore()) {
714 print("RM: %s\n", cmd_s.vname);
716 this.queue.append_val(cmd_s);
720 print("ADD: %s\n", cmd_d.vname);
721 cmd_d.action = "add";
722 this.queue.append_val(cmd_d);
725 var cmd = new GitMonitorQueue(dest);
726 cmd.action = "commit";
727 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
728 if (GitMonitorQueue.queueHas(this.queue, cmd_s, "add")) {
729 cmd.message = cmd_d.vname;
732 this.queue.append_val(cmd);