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;
130 public class GitMonitor : Monitor
133 public static GitMonitor gitmonitor;
135 * @property {String} the "gitlive" directory, normally ~/gitlive
136 * dset by OWNER... - we should do this as a CTOR.
139 public static string gitlive;
142 public Array<GitMonitorQueue> queue ;
143 public bool queueRunning = false;
145 public DateTime lastAdd;
148 public GitMonitor () {
149 this.queue = new Array<GitMonitorQueue>();
150 GitMonitor.gitmonitor = this;
156 public new void pause() {
158 // what does this do to the old one...
159 this.queue = new Array<GitMonitorQueue> ();
160 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
164 public new void resume () {
166 this.queue = new Array<GitMonitorQueue> ();
167 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
172 * Start the monitoring
173 * and run the queue every 500 milliseconds..
176 public new void start() {
177 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
180 this.lastAdd = new DateTime.now(new TimeZone.local());
182 Timeout.add_full(Priority.LOW, 500, () => {
183 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
184 // call this.monitor on each of 'top'
185 for(int i = 0; i < this.top.length ; i++) {
187 this.monitor(this.top.index(i) );
189 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
196 var notification = new Notify.Notification(
198 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
202 notification.set_timeout(5);
207 return false; // do not keep doing this..
212 Timeout.add_full(Priority.LOW, 1000, () => {
213 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
215 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
216 if (this.queue.length < 1 || this.queueRunning) {
220 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
222 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
223 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
225 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
228 //_this.lastAdd = new Date();
238 public new void stop() {
239 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
244 public new void monitor (string path, int depth = 0)
247 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
250 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
251 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
256 // FIXME - check if repo is flagged as not autocommit..
257 //var repo = imports.Scm.Repo.Repo.get(path);
258 //if (!repo || !repo.autocommit()) {
264 // check if the repo is to be monitored.
265 //print("PATH : " + path);
268 base.monitor(path, depth);
275 * - pulls the items off the queue
276 * (as commands run concurrently and new items may get added while it's running)
277 * - runs the queue items
281 public void runQueue()
287 print("GitMonitor.runQueue\n");
289 this.queueRunning = true;
291 var cmds = new Array<GitMonitorQueue>();
293 for(var i = 0; i < this.queue.length; i++) {
294 cmds.append_val(this.queue.index(i));
297 this.queue = new Array<GitMonitorQueue>();// empty queue!
300 string[] success = {};
301 string[] failure = {};
302 //var repos = new Array<GitRepo>(); //??
303 //var done = new Array<GitMonitorQueue>();
305 // first build a array of repo's to work with
306 var repo_list = new Array<GitRepo>();
310 //print(JSON.stringify(cmds));
311 // make sure nothing get's added to the queue where we are doing this..
315 print("GitMonitor.runQueue - creating repos\n");
317 for(var i = 0; i < cmds.length; i++) {
319 var cmd = cmds.index(i);
321 var gitpath = cmd.gitpath;
322 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
324 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
326 repo_list.append_val(new GitRepo( gitpath ));
327 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
329 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
331 //if (typeof(repo_list[gitpath]) == 'undefined') {
332 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
333 // repo_list[gitpath].cmds = [];
334 // repo_list[gitpath].pull();
336 repo_list.index(ix).cmds.append_val(cmd);
340 // build add, remove and commit message list..
342 print("GitMonitor.runQueue - creating actions\n");
344 for(var i = 0;i < repo_list.length;i++) {
346 var repo = repo_list.index(i);
348 var add_files = new Array<GitMonitorQueue>();
349 var add_files_f = new Array<GitMonitorQueue>();
350 var remove_files = new Array<GitMonitorQueue>();
351 var messages = new Array<GitMonitorQueue>();
352 //print(JSON.stringify(repo.cmds,null,4));
354 for(var ii = 0;ii < repo.cmds.length;ii++) {
355 var cmd = repo.cmds.index(ii);
361 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
365 add_files.append_val(cmd);
369 if (GitMonitorQueue.indexOfAdd(remove_files, cmd.vname) > -1 ) {
373 // if file exists, do not try and delete it.
374 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
378 remove_files.append_val(cmd);
382 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
385 messages.append_val(cmd);
389 stdout.printf("Opps unmatched action %s\n", cmd.action);
393 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files));
394 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files));
396 // these can fail... at present... as we wildcard stuff.
398 // make sure added files do not get removed.. ??
400 var remove_files_f = new Array<GitMonitorQueue>();
401 for(var ii = 0;ii < remove_files.length;ii++) {
402 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
405 remove_files_f.append_val(remove_files.index(ii));
407 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
410 // if file was added, then removed,
411 var remove_files_f = new Array<GitMonitorQueue>();
412 for(var ii = 0;ii < remove_files.length;ii++) {
413 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
414 // in add and remove - do not remvove
417 remove_files_f.append_val(remove_files.index(ii));
419 for(var ii = 0;ii < add_files.length;ii++) {
420 if (GitMonitorQueue.indexOfAdd(remove_files, add_files.index(ii).vname) > -1 ) {
421 // in add and remove - do not remvove
424 add_files_f.append_val(add_files.index(ii));
427 print( "ADD : %s\n", GitMonitorQueue.queueArrayToString(add_files_f));
428 print( "REMOVE FILES: %s\n", GitMonitorQueue.queueArrayToString(remove_files_f));
431 // make sure monitoring is paused so it does not recursively pick up
436 failure += e.message;
442 repo.add(add_files_f);
444 failure += e.message;
447 repo.remove(remove_files_f);
449 failure += e.message;
456 success += repo.commit(
457 GitMonitorQueue.messageToString(messages),
460 success += repo.push();
463 failure += e.message;
468 // finally merge all the commit messages.
471 // catch notification failures.. so we can carry on..
472 if (success.length > 0) {
475 var notification = new Notify.Notification(
477 string.joinv("\n",success),
482 notification.set_timeout(5);
486 if (failure.length > 0) {
488 var notification = new Notify.Notification(
490 string.joinv("\n",failure),
495 notification.set_timeout(5); // show errros for longer
502 this.queueRunning = false;
509 //string[] just_created;
518 public override void onChanged(MonitorNamePathDir src)
520 print("GitMonitor.onChanged\n");
521 return; // always ignore this..?
522 //this.parsePath(src);
528 * results in git add + git commit..
531 public override void onChangesDoneHint(MonitorNamePathDir src)
533 print("GitMonitor.onChangedHint\n");
539 this.lastAdd = new DateTime.now(new TimeZone.local());
540 var cmd = new GitMonitorQueue(src);
541 if (cmd.shouldIgnore()) {
546 //var add_it = false;
548 if (this.is_just_created(cmd.path)) {
550 if (typeof(this.just_created[src.path]) !='undefined') {
551 delete this.just_created[src.path];
554 [ src.gitpath, 'add', src.vpath ],
555 [ src.gitpath, 'commit', { message: src.vpath} ]
563 this.queue.append_val(cmd);
565 cmd = new GitMonitorQueue(src);
566 cmd.action = "commit";
567 cmd.message = cmd.vname;
568 this.queue.append_val(cmd);
572 public override void onDeleted(MonitorNamePathDir src)
574 print("GitMonitor.onDeleted\n");
578 this.lastAdd = new DateTime.now(new TimeZone.local());
579 var cmd = new GitMonitorQueue(src);
580 if (cmd.shouldIgnore()) {
583 // should check if monitor needs removing..
584 // it should also check if it was a directory.. - so we dont have to commit all..
586 this.queue.append_val(cmd);
588 cmd = new GitMonitorQueue(src);
589 cmd.action = "commit";
590 cmd.message = cmd.vname;
591 cmd.commit_all = true;
593 this.queue.append_val(cmd);
596 public override void onCreated(MonitorNamePathDir src) {
597 print("GitMonitor.onCreated\n");
601 this.lastAdd = new DateTime.now(new TimeZone.local());
602 var cmd = new GitMonitorQueue(src);
603 if (cmd.shouldIgnore()) {
607 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
608 // this.just_created[src.path] = true;
609 return; // we do not handle file create flags... - use done hint.
611 // directory has bee created
612 this.monitor(src.path);
613 //this.top.append_val(src.path);
614 //this.monitor(src.path );
617 // -- no point in adding a dir.. as git does not handle them...
619 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
620 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
626 public override void onAttributeChanged(MonitorNamePathDir src) {
627 print("GitMonitor.onAttributeChanged\n");
631 if (src.dir == GitMonitor.gitlive) {
632 return; // attribute on top level..
635 this.lastAdd = new DateTime.now(new TimeZone.local());
636 var cmd = new GitMonitorQueue(src);
637 if (cmd.shouldIgnore()) {
641 this.queue.append_val(cmd);
643 cmd = new GitMonitorQueue(src);
644 cmd.action = "commit";
645 cmd.message = "Attribute changed " + cmd.vname;
646 this.queue.append_val(cmd);
649 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
651 print("GitMonitor.onMoved\n");
655 this.lastAdd = new DateTime.now(new TimeZone.local());
656 var cmd_s = new GitMonitorQueue(src);
658 var cmd_d = new GitMonitorQueue(dest);
661 if (cmd_d.gitpath != cmd_s.gitpath) {
663 this.onCreated(dest);
664 this.onChangesDoneHint(dest);
667 // needs to handle move to/from unsupported types..
669 if (cmd_s.shouldIgnore()) {
670 this.onCreated(dest);
671 this.onChangesDoneHint(dest);
675 if (cmd_d.shouldIgnore()) {
682 print("RM: %s\n", cmd_s.vname);
684 this.queue.append_val(cmd_s);
686 print("ADD: %s\n", cmd_d.vname);
687 cmd_d.action = "add";
688 this.queue.append_val(cmd_d);
691 var cmd = new GitMonitorQueue(dest);
692 cmd.action = "commit";
693 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
694 this.queue.append_val(cmd);