4 public class GitMonitorQueue : MonitorNamePathDir {
10 public string vpath; // relative path (within git)
11 public string vname; // relative filename (within git)
12 public string message ; // for commit
13 public bool commit_all;
19 public GitMonitorQueue(MonitorNamePathDir f) {
21 base(f.name, f.path, f.dir);
25 this.commit_all = false;
28 var vpath_ar = this.path.substring(GitMonitor.gitlive.length +1).split("/", 0);
30 this.gitpath = GitMonitor.gitlive + "/" + vpath_ar[0];
33 for (var i = 1; i< vpath_ar.length; i++) {
37 this.vpath = string.joinv("/", vpath);
39 this.vname = this.vpath + (this.vpath.length > 0 ? "/" : "") + this.name;
40 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
45 public bool shouldIgnore(GitMonitor gm)
50 // vim.. what a seriously brain dead program..
51 if (this.name == "4913") {
55 if (this.name[0] == '.') {
57 if (this.name == ".htaccess") {
63 //if (f.name.match(/~$/)) {
66 //if (f.name.match(/^nbproject/)) {
69 // ignore anything in top level!!!!
70 if (this.vpath.length < 1) {
79 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
80 for(var i =0; i < add_files.length; i++) {
81 if (add_files.index(i).vname == add) {
87 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
88 for(var i =0; i < messages.length; i++) {
89 if (messages.index(i).message == message) {
95 public static string messageToString(Array<GitMonitorQueue> messages ) {
97 for(var i =0; i < messages.length; i++) {
98 ret+= messages.index(i).message;
100 return string.joinv("\n",ret);
107 public class GitMonitor : Monitor
112 * @property {String} the "gitlive" directory, normally ~/gitlive
113 * dset by OWNER... - we should do this as a CTOR.
116 public static string gitlive = "";
119 public Array<GitMonitorQueue> queue ;
120 public bool queueRunning = false;
122 public DateTime lastAdd;
125 public new void pause() {
127 // what does this do to the old one...
128 this.queue = new Array<GitMonitorQueue> ();
129 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
133 public new void resume () {
135 this.queue = new Array<GitMonitorQueue> ();
136 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
141 * Start the monitoring
142 * and run the queue every 500 milliseconds..
145 public new void start() {
146 StatusIconA.statusicon.set_from_stock( Gtk.Stock.REFRESH );
149 this.lastAdd = new DateTime.now(new TimeZone.local());
151 Timeout.add_full(Priority.LOW, 500, () => {
153 // call this.monitor on each of 'top'
154 for(int i = 0; i < this.top.length ; i++) {
155 this.monitor(this.top.index(i) );
157 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
164 var notification = new Notify.Notification(
166 "%s\nMonitoring %u Directories".printf(GitMonitor.gitlive, this.monitors.length),
170 notification.set_timeout(5);
178 Timeout.add_full(Priority.LOW, 1000, () => {
179 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
180 if (this.queue.length < 1 || this.queueRunning) {
184 var last = this.lastAdd.difference(new DateTime.now(new TimeZone.local()));
187 //print("LAST RUN?" + last);
189 if (last < 5 * TimeSpan.SECOND) { // wait 5 seconds before running. ????
192 //_this.lastAdd = new Date();
203 public new void stop() {
204 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
209 public new void monitor (string path, int depth = 0)
212 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
215 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
216 if (depth > 1 && FileUtils.test(path + "/.git" , FileTest.IS_DIR)) {
221 // FIXME - check if repo is flagged as not autocommit..
222 //var repo = imports.Scm.Repo.Repo.get(path);
223 //if (!repo || !repo.autocommit()) {
229 // check if the repo is to be monitored.
230 //print("PATH : " + path);
233 base.monitor(path, depth);
240 * - pulls the items off the queue
241 * (as commands run concurrently and new items may get added while it's running)
242 * - runs the queue items
246 public void runQueue()
252 this.queueRunning = true;
254 var cmds = new Array<GitMonitorQueue>();
255 for(var i = 0; i < this.queue.length; i++) {
256 cmds.append_val(this.queue.index(i));
259 this.queue = new Array<GitMonitorQueue>();// empty queue!
262 string[] success = {};
263 string[] failure = {};
264 //var repos = new Array<GitRepo>(); //??
265 var done = new Array<GitMonitorQueue>();
267 // first build a array of repo's to work with
268 var repo_list = new Array<GitRepo>();
272 //print(JSON.stringify(cmds));
275 for(var i = 0; i < cmds.length; i++) {
277 var cmd = cmds.index(i);
279 var gitpath = cmd.gitpath;
280 var ix = GitRepo.indexOf(repo_list, cmd.gitpath);
282 repo_list.append_val(new GitRepo( gitpath ));
283 ix = GitRepo.indexOf(repo_list, cmd.gitpath);
287 //if (typeof(repo_list[gitpath]) == 'undefined') {
288 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
289 // repo_list[gitpath].cmds = [];
290 // repo_list[gitpath].pull();
292 repo_list.index(ix).cmds.append_val(cmd);
296 // build add, remove and commit message list..
298 for(var i = 0;i < repo_list.length;i++) {
301 var repo = repo_list.index(i);
303 var add_files = new Array<GitMonitorQueue>();
304 var remove_files = new Array<GitMonitorQueue>();
305 var messages = new Array<GitMonitorQueue>();
306 //print(JSON.stringify(repo.cmds,null,4));
308 for(var ii = 0;ii < repo.cmds.length;ii++) {
309 var cmd = repo.cmds.index(ii);
315 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1) {
320 add_files.append_val(cmd);
324 if (GitMonitorQueue.indexOfAdd(add_files, cmd.vname) > -1 ) {
328 // if file exists, do not try and delete it.
329 if (FileUtils.test(cmd.vname, FileTest.EXISTS)) {
333 remove_files.append_val(cmd);
337 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
341 messages.append_val(cmd);
345 print("Opps unmatched action");
351 // these can fail... at present... as we wildcard stuff.
352 stdout.printf("ADD : %u files" , add_files.length);
354 // make sure added files do not get removed..
356 var remove_files_f = new Array<GitMonitorQueue>();
357 for(var ii = 0;ii < remove_files.length;ii++) {
358 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.index(ii).vname) > -1 ) {
361 remove_files_f.append_val(remove_files.index(ii));
363 stdout.printf("REMOVE : %u files" , remove_files.length);
365 // make sure monitoring is paused so it does not recursively pick up
372 failure += e.message;
375 repo.remove(remove_files_f);
377 failure += e.message;
384 success += repo.commit(
385 GitMonitorQueue.messageToString(messages),
388 success += repo.push();
391 failure += e.message;
396 // finally merge all the commit messages.
399 // catch notification failures.. so we can carry on..
400 if (success.length > 0) {
403 var notification = new Notify.Notification(
405 string.joinv("\n",success),
410 notification.set_timeout(5);
414 if (failure.length > 0) {
416 var notification = new Notify.Notification(
417 summary: "Git Live ERROR!!",
418 string.joinv("\n",failure),
423 notification.set_timeout(5); // show errros for longer
430 this.queueRunning = false;
437 //string[] just_created;
446 public new void onChanged(MonitorNamePathDir src)
448 return; // always ignore this..?
449 //this.parsePath(src);
455 * results in git add + git commit..
458 public new void onChangesDoneHint(MonitorNamePathDir src)
466 this.lastAdd = new DateTime.now(new TimeZone.local());
467 var cmd = new GitMonitorQueue(src);
468 if (cmd.shouldIgnore()) {
475 if (this.is_just_created(cmd.path)) {
477 if (typeof(this.just_created[src.path]) !='undefined') {
478 delete this.just_created[src.path];
481 [ src.gitpath, 'add', src.vpath ],
482 [ src.gitpath, 'commit', { message: src.vpath} ]
490 this.queue.append_val(cmd);
492 var cmd = new GitMonitorQueue(src);
493 cmd.action = "commit";
494 cmd.message = src.vpath;
495 this.queue.append_val(cmd);
499 public new void onDeleted(MonitorNamePathDir src)
504 this.lastAdd = new DateTime.now(new TimeZone.local());
505 var cmd = new GitMonitorQueue(src);
506 if (cmd.shouldIgnore()) {
509 // should check if monitor needs removing..
510 // it should also check if it was a directory.. - so we dont have to commit all..
513 this.queue.append_val(cmd);
515 var cmd = new GitMonitorQueue(src);
516 cmd.action = "commit";
517 cmd.message = src.vpath;
518 cmd.commit_all = true;
520 this.queue.append_val(cmd);
523 public new void onCreated(MonitorNamePathDir src) {
528 this.lastAdd = new DateTime.now(new TimeZone.local());
529 var cmd = new GitMonitorQueue(src);
530 if (cmd.shouldIgnore()) {
534 if (!FileUtils.test(src.path, GLib.FileTest.IS_DIR)) {
535 // this.just_created[src.path] = true;
536 return; // we do not handle file create flags... - use done hint.
538 // directory has bee created
539 this.monitor(src.path);
540 this.top.append_val(src.path);
541 this.monitor(src.path );
544 // -- no point in adding a dir.. as git does not handle them...
546 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
547 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
553 public new void onAttributeChanged(MonitorNamePathDir src) {
558 this.lastAdd = new DateTime.now(new TimeZone.local());
559 var cmd = new GitMonitorQueue(src);
560 if (cmd.shouldIgnore()) {
564 this.queue.append_val(cmd);
566 var cmd = new GitMonitorQueue(src);
567 cmd.action = "commit";
568 cmd.message = "Attribute changed " + cmd.vpath;
569 this.queue.append_val(cmd);
573 public new void onMoved(MonitorNamePathDir src,MonitorNamePathDir dest)
578 this.lastAdd = new DateTime.now(new TimeZone.local());
579 var cmd_s = new GitMonitorQueue(src);
581 var cmd_d = new GitMonitorQueue(src);
584 if (cmd_d.gitpath != cmd_s.gitpath) {
586 this.onCreated(dest);
587 this.onChangedDoneHint(dest);
590 // needs to handle move to/from unsupported types..
592 if (this.shouldIgnore(src)) {
593 this.onCreated(dest);
594 this.onChangedDoneHint(dest);
598 if (this.shouldIgnore(dest)) {
607 this.queue.append_val(cmd_s);
612 cmd_d.action = "add";
613 this.queue.append_val(cmd_d);
616 var cmd = new GitMonitorQueue(dest);
617 cmd.action = "commit";
618 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
619 this.queue.append_val(cmd);