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;
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);
38 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
43 public bool shouldIgnore(GitMonitor gm)
48 // vim.. what a seriously brain dead program..
49 if (this.name == "4913") {
53 if (this.name[0] == '.') {
55 if (this.name == ".htaccess") {
61 //if (f.name.match(/~$/)) {
64 //if (f.name.match(/^nbproject/)) {
67 // ignore anything in top level!!!!
68 if (this.vpath.length < 1) {
77 public static int indexOfAdd( Array<GitMonitorQueue> add_files, string add) {
78 for(var i =0; i < add_files.length; i++) {
79 if (add_files.index(i).add == add) {
85 public static int indexOfMessage(Array<GitMonitorQueue> messages, string message) {
86 for(var i =0; i < messages.length; i++) {
87 if (messages.index(i).message == message) {
93 public static string messageToString(Array<GitMonitorQueue> messages ) {
95 for(var i =0; i < messages.length; i++) {
96 ret+= messages.index(i).message;
98 return string.joinv("\n",ret);
105 public class GitMonitor : Monitor
110 * @property {String} the "gitlive" directory, normally ~/gitlive
111 * dset by OWNER... - we should do this as a CTOR.
114 public static string gitlive = "";
117 public Array<GitMonitorQueue> queue ;
118 public bool queueRunning = false;
120 public DateTime lastAdd;
123 public void pause() {
125 // what does this do to the old one...
126 this.queue = new Array<GitMonitorQueue> ();
127 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );
131 public void resume () {
133 this.queue = new Array<GitMonitorQueue> ();
134 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
139 * Start the monitoring
140 * and run the queue every 500 milliseconds..
143 public void start() {
144 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_REFRESH );
147 this.lastAdd = new DateTime.now();
149 Timeout.add_full(GLib.PRIORITY_LOW, 500, () => {
151 // call this.monitor on each of 'top'
152 for(int i = 0; i < this.top.length ; i++) {
153 this.monitor(this.top.index(i) );
155 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PLAY );
160 var notification = new Notify.Notification({
162 GitMonitor.gitlive + "\nMonitoring " + _this.monitors.length + " Directories",
166 notification.set_timeout(5);
174 Timeout.add_full(GLib.PRIORITY_LOW, 1000, () => {
175 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
176 if (!_this.queue.length || _this.queueRunning) {
180 var last = this.lastAdd.difference(new DateTime.now());
183 //print("LAST RUN?" + last);
185 if (last < 5 * Timespan.SECOND) { // wait 1/2 a seconnd before running.
188 //_this.lastAdd = new Date();
200 StatusIconA.statusicon.set_from_stock( Gtk.Stock.MEDIA_PAUSE );;
205 public void monitor (string path, int depth = 0)
208 //var depth = typeof(depth) == 'number' ? depth *1 : 0;
211 // if we are not at top level.. and there is a .git directory (it's a submodule .. ignore)
212 if (depth > 1 && GLib.file_test(path + "/.git" , GLib.FileTest.IS_DIR)) {
217 // FIXME - check if repo is flagged as not autocommit..
218 //var repo = imports.Scm.Repo.Repo.get(path);
219 //if (!repo || !repo.autocommit()) {
225 // check if the repo is to be monitored.
226 //print("PATH : " + path);
229 base.monitor(path, depth);
236 * - pulls the items off the queue
237 * (as commands run concurrently and new items may get added while it's running)
238 * - runs the queue items
242 public void runQueue()
248 this.queueRunning = true;
250 var cmds = new Array<GitMonitorQueue>();
251 for(var i = 0; i < this.queue.length; i++) {
252 cmds.append_val(this.queue.item(i));
255 this.queue = new Array<GitMonitorQueue>();// empty queue!
258 string[] success = {};
259 string[] failure = {};
260 var repos = new Array<GitRepo>(); //??
261 var done = new Array<GitMonitorQueue>();
263 // first build a array of repo's to work with
264 var repo_list = new Array<GitRepo>();
268 //print(JSON.stringify(cmds));
271 for(var i = 0; i < cmds.length; i++) {
274 var gitpath = cmd.gitpath;
275 var ix = GitRepo.indexOf(this.repos, cmd.gitpath);
277 repo_list.append_val(new GitRepo( gitpath ));
278 ix = GitRepo.indexOf(this.repos, cmd.gitpath);
282 //if (typeof(repo_list[gitpath]) == 'undefined') {
283 // repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
284 // repo_list[gitpath].cmds = [];
285 // repo_list[gitpath].pull();
287 repo_list.item(ix).cmds.append_val(cmd);
291 // build add, remove and commit message list..
293 for(var i = 0;i < repo_list.length;i++) {
296 var repo = repo_list.item(i);
298 var add_files = new Array<GitMonitorQueue>();
299 var remove_files = new Array<GitMonitorQueue>();
300 var messages = new Array<GitMonitorQueue>();
301 //print(JSON.stringify(repo.cmds,null,4));
303 for(var ii = 0;ii < repo.cmds.length;ii++) {
304 var cmd = repo.cmds.item(ii);
310 if (GitMonitorQueue.indexOfAdd(add_files, cmd.add) > -1) {
315 add_files.append_val(cmd);
319 if (GitMonitorQueue.indexOfAdd(add_files, cmd.rm) > -1 ) {
323 // if file exists, do not try and delete it.
324 if (GLib.file_test(cmd.rm, GLib.FileTest.EXISTS)) {
328 remove_files.append_val(cmd);
332 if (GitMonitorQueue.indexOfMessage(messages, cmd.message) > -1 ) {
336 messages.append_val(cmd);
340 print("Opps unmatched action");
346 // these can fail... at present... as we wildcard stuff.
347 stdout.printf("ADD : %d files" , add_files.length);
349 // make sure added files do not get removed..
351 var remove_files_f = new Array<GitMonitorQueue>();
352 for(var ii = 0;ii < remove_files.length;ii++) {
353 if (GitMonitorQueue.indexOfAdd(add_files, remove_files.item(ii).rm) > -1 ) {
356 remove_files_f.append_val(remove_files.item(ii));
358 stdout.printf("REMOVE : %d files" , remove_files.length);
360 // make sure monitoring is paused so it does not recursively pick up
366 repo.remove(remove_files_f);
371 success += repo.commit(
372 GitMonitorQueue.messageToString(messages),
375 success += repo.push();
378 failure += e.message;
383 // finally merge all the commit messages.
386 // catch notification failures.. so we can carry on..
387 if (success.length) {
390 var notification = new Notify.Notification(
392 string.joinv("\n",success),
397 notification.set_timeout(5);
401 if (failure.length) {
403 var notification = new Notify.Notification({
404 summary: "Git Live ERROR!!",
405 string.joinv("\n",failure),
410 notification.set_timeout(5); // show errros for longer
417 this.queueRunning = false;
424 //string[] just_created;
433 public void onChanged(MonitorNamePathDir src)
435 return; // always ignore this..?
436 //this.parsePath(src);
442 * results in git add + git commit..
445 public void onChangesDoneHint(MonitorNamePathDir src)
453 this.lastAdd = new DateTime.now();
454 var cmd = new GitMonitorQueue(src);
455 if (cmd.shouldIgnore()) {
462 if (this.is_just_created(cmd.path)) {
464 if (typeof(this.just_created[src.path]) !='undefined') {
465 delete this.just_created[src.path];
468 [ src.gitpath, 'add', src.vpath ],
469 [ src.gitpath, 'commit', { message: src.vpath} ]
477 this.queue.append_val(cmd);
479 var cmd = new GitMonitorQueue(src);
480 cmd.action = "commit";
481 cmd.message = src.vpath;
482 this.queue.append_val(cmd);
486 public void onDeleted(MonitorNamePathDir src)
491 this.lastAdd = new DateTime.now();
492 var cmd = new GitMonitorQueue(src);
493 if (cmd.shouldIgnore()) {
496 // should check if monitor needs removing..
497 // it should also check if it was a directory.. - so we dont have to commit all..
500 this.queue.append_val(cmd);
502 var cmd = new GitMonitorQueue(src);
503 cmd.action = "commit";
504 cmd.message = src.vpath;
505 cmd.commit_all = true;
507 this.queue.append_val(cmd);
510 public void onCreated(MonitorNamePathDir src) {
515 this.lastAdd = new DateTime.now();
516 var cmd = new GitMonitorQueue(src);
517 if (cmd.shouldIgnore()) {
521 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
522 // this.just_created[src.path] = true;
523 return; // we do not handle file create flags... - use done hint.
525 // directory has bee created
526 this.monitor(src.path);
527 this.top.append_val(src.path);
528 this.monitor(src.path );
531 // -- no point in adding a dir.. as git does not handle them...
533 // [ src.gitpath, 'add' , src.vpath, { all: true } ],
534 // [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
540 public void onAttributeChanged(MonitorNamePathDir src) {
545 this.lastAdd = new DateTime.now();
546 var cmd = new GitMonitorQueue(src);
547 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 cmd_s.rm = src.vpath;
593 this.queue.append_val(cmd_s);
598 cmd_d.action = "add";
599 cmd_d.add = src.vpath;
600 this.queue.append_val(cmd_d);
603 var cmd = new GitMonitorQueue(dest);
604 cmd.action = "commit";
605 cmd.message = "MOVED " + cmd_s.vpath + " to " + cmd_d.vpath;
606 this.queue.append_val(cmd);