3 public class GitMonitorQueue : MonitorNamePathDir {
9 public string vpath; // relative path (within git)
10 public string vname; // relative filename (within git)
11 public string message ; // for commit
12 public bool commit_all;
18 public GitMonitorQueue(MonitorNamePathDir f) {
20 base(f.name, f.path, f.dir);
24 this.commit_all = false;
27 var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
29 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
32 for (var i = 1; i< vpath_ar.length; i++) {
36 this.vpath = string.joinv("/", vpath);
38 this.vname = this.vpath + (this.vpath.length > 0 ? "/" : "") + this.name;
39 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
44 public bool shouldIgnore(GitMonitor gm)
49 // vim.. what a seriously brain dead program..
50 if (this.name == "4913") {
54 if (this.name[0] == '.') {
56 if (this.name == ".htaccess") {
62 //if (f.name.match(/~$/)) {
65 //if (f.name.match(/^nbproject/)) {
68 // ignore anything in top level!!!!
69 if (this.vpath.length < 1) {
78 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
79 for(var i =0; i < add_files.length; i++) {
80 if (add_files.index(i).vname == add) {
86 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
87 for(var i =0; i < messages.length; i++) {
88 if (messages.index(i).message == message) {
94 public static string messageToString(Array<GitMonitorQueue> messages ) {
96 for(var i =0; i < messages.length; i++) {
97 ret+= messages.index(i).message;
99 return string.joinv("\n",ret);
106 public class GitMonitor : Monitor
111 * @property {String} the "gitlive" directory, normally ~/gitlive
112 * dset by OWNER... - we should do this as a CTOR.
115 public static string gitlive = "";
118 public Array<GitMonitorQueue> queue ;
119 public bool queueRunning = false;
121 public DateTime lastAdd;
124 public new void pause() {
126 // what does this do to the old one...
127 this.queue = new Array<GitMonitorQueue> ();
128 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
132 public new void resume () {
134 this.queue = new Array<GitMonitorQueue> ();
135 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
140 * Start the monitoring
141 * and run the queue every 500 milliseconds..
144 public new void start() {
145 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
148 this.lastAdd = new DateTime.now();
150 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
152 // call this.monitor on each of 'top'
153 for(int i = 0; i < this.top.length ; i++) {
154 this.monitor(this.top.index(i) );
156 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
161 var notification = new Notify.Notification({
163 GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
167 notification.set_timeout(5);
175 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
176 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
177 if (!_this.queue.length || _this.queueRunning) {
181 var last = this.lastAdd.difference(new DateTime.now());
184 //print("LAST RUN?" + last);
186 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
189 //_this.lastAdd = new Date();
200 public new void stop() {
201 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
206 public new void monitor (string path, int depth = 0)
209 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
212 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
213 if (depth > 1 && GLib.file_test(path + "/.git" , GLib.FileTest.IS_DIR)) {
218 // FIXME - check if repo is flagged as not autocommit..
219 //var repo = imports.Scm.Repo.Repo.get(path);
220 //if (!repo || !repo.autocommit()) {
226 // check if the repo is to be monitored.
227 //print("PATH : " + path);
230 base.monitor(path, depth);
237 * - pulls the items off the queue
238 * (as commands run concurrently and new items may get added while it's running)
239 * - runs the queue items
243 public void runQueue()
249 this.queueRunning = true;
251 var cmds = new Array<GitMonitorQueue>();
252 for(var i = 0; i < this.queue.length; i++) {
253 cmds.append_val(this.queue.item(i));
256 this.queue = new Array<GitMonitorQueue>();// empty queue!
259 string[] success = {};
260 string[] failure = {};
261 var repos = new Array<GitRepo>(); //??
262 var done = new Array<GitMonitorQueue>();
264 // first build a array of repo's to work with
265 var repo_list = new Array<GitRepo>();
269 //print(JSON.stringify(cmds));
272 for(var i = 0; i < cmds.length; i++) {
275 var gitpath = cmd.gitpath;
276 var ix = GitRepo.indexOf(this.repos, cmd.gitpath);
278 repo_list.append_val(new GitRepo( gitpath ));
279 ix = GitRepo.indexOf(this.repos, cmd.gitpath);
283 //if (typeof(repo_list[gitpath]) == 'undefined') {
284 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
285 // repo_list[gitpath].cmds = [];
286 // repo_list[gitpath].pull();
288 repo_list.item(ix).cmds.append_val(cmd);
292 // build add, remove and commit message list..
294 for(var i = 0;i < repo_list.length;i++) {
297 var repo = repo_list.item(i);
299 var add_files = new Array<GitMonitorQueue>();
300 var remove_files = new Array<GitMonitorQueue>();
301 var messages = new Array<GitMonitorQueue>();
302 //print(JSON.stringify(repo.cmds,null,4));
304 for(var ii = 0;ii < repo.cmds.length;ii++) {
305 var cmd = repo.cmds.item(ii);
311 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
316 add_files.append_val(cmd);
320 if (GitMonitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
324 // if file exists, do not try and delete it.
325 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
329 remove_files.append_val(cmd);
333 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
337 messages.append_val(cmd);
341 print("Opps unmatched action");
347 // these can fail... at present... as we wildcard stuff.
348 stdout.printf("ADD : %d files" , add_files.length);
350 // make sure added files do not get removed..
352 var remove_files_f = new Array<GitMonitorQueue>();
353 for(var ii = 0;ii < remove_files.length;ii++) {
354 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.item(ii).rm) > -1 ) {
357 remove_files_f.append_val(remove_files.item(ii));
359 stdout.printf("REMOVE : %d files" , remove_files.length);
361 // make sure monitoring is paused so it does not recursively pick up
367 repo.remove(remove_files_f);
372 success += repo.commit(
373 GitMonitorQueue.messageToString(messages),
376 success += repo.push();
379 failure += e.message;
384 // finally merge all the commit messages.
387 // catch notification failures.. so we can carry on..
388 if (success.length) {
391 var notification = new Notify.Notification(
393 string.joinv("\n",success),
398 notification.set_timeout(5);
402 if (failure.length) {
404 var notification = new Notify.Notification({
405 summary: "Git Live ERROR!!",
406 string.joinv("\n",failure),
411 notification.set_timeout(5); // show errros for longer
418 this.queueRunning = false;
425 //string[] just_created;
434 public void onChanged(MonitorNamePathDir src)
436 return; // always ignore this..?
437 //this.parsePath(src);
443 * results in git add + git commit..
446 public void onChangesDoneHint(MonitorNamePathDir src)
454 this.lastAdd = new DateTime.now();
455 var cmd = new GitMonitorQueue(src);
456 if (cmd.shouldIgnore()) {
463 if (this.is_just_created(cmd.path)) {
465 if (typeof(this.just_created[src.path]) !='undefined') {
466 delete this.just_created[src.path];
469 [ src.gitpath, 'add', src.vpath ],
470 [ src.gitpath, 'commit', { message: src.vpath} ]
478 this.queue.append_val(cmd);
480 var cmd = new GitMonitorQueue(src);
481 cmd.action = "commit";
482 cmd.message = src.vpath;
483 this.queue.append_val(cmd);
487 public void onDeleted(MonitorNamePathDir src)
492 this.lastAdd = new DateTime.now();
493 var cmd = new GitMonitorQueue(src);
494 if (cmd.shouldIgnore()) {
497 // should check if monitor needs removing..
498 // it should also check if it was a directory.. - so we dont have to commit all..
501 this.queue.append_val(cmd);
503 var cmd = new GitMonitorQueue(src);
504 cmd.action = "commit";
505 cmd.message = src.vpath;
506 cmd.commit_all = true;
508 this.queue.append_val(cmd);
511 public void onCreated(MonitorNamePathDir src) {
516 this.lastAdd = new DateTime.now();
517 var cmd = new GitMonitorQueue(src);
518 if (cmd.shouldIgnore()) {
522 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
523 // this.just_created[src.path] = true;
524 return; // we do not handle file create flags... - use done hint.
526 // directory has bee created
527 this.monitor(src.path);
528 this.top.append_val(src.path);
529 this.monitor(src.path );
532 // -- no point in adding a dir.. as git does not handle them...
534 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
535 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
541 public void onAttributeChanged(MonitorNamePathDir src) {
546 this.lastAdd = new DateTime.now();
547 var cmd = new GitMonitorQueue(src);
548 if (cmd.shouldIgnore()) {
552 this.queue.append_val(cmd);
554 var cmd = new GitMonitorQueue(src);
555 cmd.action = "commit";
556 cmd.message = "Attribute changed " + cmd.vpath;
557 this.queue.append_val(cmd);
561 public void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
563 this.lastAdd = new DateTime.now();
564 var cmd_s = new GitMonitorQueue(src);
566 var cmd_d = new GitMonitorQueue(src);
569 if (cmd_d.gitpath != cmd_s.gitpath) {
571 this.onCreated(dest);
572 this.onChangedDoneHint(dest);
575 // needs to handle move to/from unsupported types..
577 if (this.shouldIgnore(src)) {
578 this.onCreated(dest);
579 this.onChangedDoneHint(dest);
583 if (this.shouldIgnore(dest)) {
592 this.queue.append_val(cmd_s);
597 cmd_d.action = "add";
598 this.queue.append_val(cmd_d);
601 var cmd = new GitMonitorQueue(dest);
602 cmd.action = "commit";
603 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
604 this.queue.append_val(cmd);