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) {
94 for(var i =0; i < add_files.length; i++) {
95 if (add_files.index(i).vname == add) {
101 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
102 for(var i =0; i < messages.length; i++) {
103 if (messages.index(i).message == message) {
109 public static string messageToString(Array<GitMonitorQueue> messages ) {
111 for(var i =0; i < messages.length; i++) {
112 ret+= messages.index(i).message;
114 return string.joinv("\n",ret);
121 public class GitMonitor : Monitor
124 public static GitMonitor gitmonitor;
126 * @property {String} the "gitlive" directory, normally ~/gitlive
127 * dset by OWNER... - we should do this as a CTOR.
130 public static string gitlive;
133 public Array<GitMonitorQueue> queue ;
134 public bool queueRunning = false;
136 public DateTime lastAdd;
139 public GitMonitor () {
140 this.queue = new Array<GitMonitorQueue>();
141 GitMonitor.gitmonitor = this;
147 public new void pause() {
149 // what does this do to the old one...
150 this.queue = new Array<GitMonitorQueue> ();
151 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
155 public new void resume () {
157 this.queue = new Array<GitMonitorQueue> ();
158 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
163 * Start the monitoring
164 * and run the queue every 500 milliseconds..
167 public new void start() {
168 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
171 this.lastAdd = new DateTime.now(new TimeZone.local());
173 Timeout.add_full(Priority.LOW, 500, () => {
174 //stdout.printf("GitMonitor.start :: top.length = %u\n", this.top.length);
175 // call this.monitor on each of 'top'
176 for(int i = 0; i < this.top.length ; i++) {
178 this.monitor(this.top.index(i) );
180 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
187 var notification = new Notify.Notification(
189 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
193 notification.set_timeout(5);
198 return false; // do not keep doing this..
203 Timeout.add_full(Priority.LOW, 1000, () => {
204 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
206 //stdout.printf("QL %u: QR: %d\n", this.queue.length, this.queueRunning ? 1 : 0);
207 if (this.queue.length < 1 || this.queueRunning) {
211 var last = -1 * this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
213 // stdout.printf("LAST RUN: %s (expect %s) \n" ,
214 // last.to_string(), (5 * TimeSpan.SECOND).to_string() );
216 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
219 //_this.lastAdd = new Date();
229 public new void stop() {
230 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
235 public new void monitor (string path, int depth = 0)
238 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
241 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
242 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
247 // FIXME - check if repo is flagged as not autocommit..
248 //var repo = imports.Scm.Repo.Repo.get(path);
249 //if (!repo || !repo.autocommit()) {
255 // check if the repo is to be monitored.
256 //print("PATH : " + path);
259 base.monitor(path, depth);
266 * - pulls the items off the queue
267 * (as commands run concurrently and new items may get added while it's running)
268 * - runs the queue items
272 public void runQueue()
278 print("GitMonitor.runQueue\n");
280 this.queueRunning = true;
282 var cmds = new Array<GitMonitorQueue>();
284 for(var i = 0; i < this.queue.length; i++) {
285 cmds.append_val(this.queue.index(i));
288 this.queue = new Array<GitMonitorQueue>();// empty queue!
291 string[] success = {};
292 string[] failure = {};
293 //var repos = new Array<GitRepo>(); //??
294 //var done = new Array<GitMonitorQueue>();
296 // first build a array of repo's to work with
297 var repo_list = new Array<GitRepo>();
301 //print(JSON.stringify(cmds));
302 // make sure nothing get's added to the queue where we are doing this..
306 print("GitMonitor.runQueue - creating repos\n");
308 for(var i = 0; i < cmds.length; i++) {
310 var cmd = cmds.index(i);
312 var gitpath = cmd.gitpath;
313 stdout.printf("GitMonitor.runQueue - finding %s\n", cmd.gitpath);
315 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
317 repo_list.append_val(new GitRepo( gitpath ));
318 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
320 stdout.printf("GitMonitor.runQueue - adding to repolist %d\n", ix);
322 //if (typeof(repo_list[gitpath]) == 'undefined') {
323 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
324 // repo_list[gitpath].cmds = [];
325 // repo_list[gitpath].pull();
327 repo_list.index(ix).cmds.append_val(cmd);
331 // build add, remove and commit message list..
333 print("GitMonitor.runQueue - creating actions\n");
335 for(var i = 0;i < repo_list.length;i++) {
337 var repo = repo_list.index(i);
339 var add_files = new Array<GitMonitorQueue>();
340 var remove_files = new Array<GitMonitorQueue>();
341 var messages = new Array<GitMonitorQueue>();
342 //print(JSON.stringify(repo.cmds,null,4));
344 for(var ii = 0;ii < repo.cmds.length;ii++) {
345 var cmd = repo.cmds.index(ii);
351 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
355 add_files.append_val(cmd);
359 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
363 // if file exists, do not try and delete it.
364 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
368 remove_files.append_val(cmd);
372 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
375 messages.append_val(cmd);
379 stdout.printf("Opps unmatched action %s\n", cmd.action);
385 // these can fail... at present... as we wildcard stuff.
386 stdout.printf("ADD : %u files\n" , add_files.length);
388 // make sure added files do not get removed..
390 var remove_files_f = new Array<GitMonitorQueue>();
391 for(var ii = 0;ii < remove_files.length;ii++) {
392 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
395 remove_files_f.append_val(remove_files.index(ii));
397 stdout.printf("REMOVE : %u files\n" , remove_files_f.length);
399 // make sure monitoring is paused so it does not recursively pick up
402 // -- DO STUFF.. try {
405 failure += e.message;
408 repo.remove(remove_files_f);
410 failure += e.message;
417 success += repo.commit(
418 GitMonitorQueue.messageToString(messages),
421 success += repo.push();
424 failure += e.message;
429 // finally merge all the commit messages.
432 // catch notification failures.. so we can carry on..
433 if (success.length > 0) {
436 var notification = new Notify.Notification(
438 string.joinv("\n",success),
443 notification.set_timeout(5);
447 if (failure.length > 0) {
449 var notification = new Notify.Notification(
451 string.joinv("\n",failure),
456 notification.set_timeout(5); // show errros for longer
463 this.queueRunning = false;
470 //string[] just_created;
479 public override void onChanged(MonitorNamePathDir src)
481 print("GitMonitor.onChanged\n");
482 return; // always ignore this..?
483 //this.parsePath(src);
489 * results in git add + git commit..
492 public override void onChangesDoneHint(MonitorNamePathDir src)
494 print("GitMonitor.onChangedHint\n");
500 this.lastAdd = new DateTime.now(new TimeZone.local());
501 var cmd = new GitMonitorQueue(src);
502 if (cmd.shouldIgnore()) {
507 //var add_it = false;
509 if (this.is_just_created(cmd.path)) {
511 if (typeof(this.just_created[src.path]) !='undefined') {
512 delete this.just_created[src.path];
515 [ src.gitpath, 'add', src.vpath ],
516 [ src.gitpath, 'commit', { message: src.vpath} ]
524 this.queue.append_val(cmd);
526 cmd = new GitMonitorQueue(src);
527 cmd.action = "commit";
528 cmd.message = cmd.vname;
529 this.queue.append_val(cmd);
533 public override void onDeleted(MonitorNamePathDir src)
535 print("GitMonitor.onDeleted\n");
539 this.lastAdd = new DateTime.now(new TimeZone.local());
540 var cmd = new GitMonitorQueue(src);
541 if (cmd.shouldIgnore()) {
544 // should check if monitor needs removing..
545 // it should also check if it was a directory.. - so we dont have to commit all..
547 this.queue.append_val(cmd);
549 cmd = new GitMonitorQueue(src);
550 cmd.action = "commit";
551 cmd.message = cmd.vname;
552 cmd.commit_all = true;
554 this.queue.append_val(cmd);
557 public override void onCreated(MonitorNamePathDir src) {
558 print("GitMonitor.onCreated\n");
562 this.lastAdd = new DateTime.now(new TimeZone.local());
563 var cmd = new GitMonitorQueue(src);
564 if (cmd.shouldIgnore()) {
568 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
569 // this.just_created[src.path] = true;
570 return; // we do not handle file create flags... - use done hint.
572 // directory has bee created
573 this.monitor(src.path);
574 //this.top.append_val(src.path);
575 //this.monitor(src.path );
578 // -- no point in adding a dir.. as git does not handle them...
580 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
581 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
587 public override void onAttributeChanged(MonitorNamePathDir src) {
588 print("GitMonitor.onAttributeChanged\n");
592 this.lastAdd = new DateTime.now(new TimeZone.local());
593 var cmd = new GitMonitorQueue(src);
594 if (cmd.shouldIgnore()) {
598 this.queue.append_val(cmd);
600 cmd = new GitMonitorQueue(src);
601 cmd.action = "commit";
602 cmd.message = "Attribute changed " + cmd.vname;
603 this.queue.append_val(cmd);
606 public override void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
608 print("GitMonitor.onMoved\n");
612 this.lastAdd = new DateTime.now(new TimeZone.local());
613 var cmd_s = new GitMonitorQueue(src);
615 var cmd_d = new GitMonitorQueue(src);
618 if (cmd_d.gitpath != cmd_s.gitpath) {
620 this.onCreated(dest);
621 this.onChangesDoneHint(dest);
624 // needs to handle move to/from unsupported types..
626 if (cmd_s.shouldIgnore()) {
627 this.onCreated(dest);
628 this.onChangesDoneHint(dest);
632 if (cmd_d.shouldIgnore()) {
641 this.queue.append_val(cmd_s);
646 cmd_d.action = "add";
647 this.queue.append_val(cmd_d);
650 var cmd = new GitMonitorQueue(dest);
651 cmd.action = "commit";
652 cmd.message = "MOVED " + cmd_s.vname + " to " + cmd_d.vname;
653 this.queue.append_val(cmd);