2 ///<script type="text/javascript">
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
12 * A) have a gitlive branch - where all our commits go.. - so they can be replicated on the server
13 * B) HEAD branch - where things get merged to..
14 * -- eventually on closing issues..
15 * -- currently when we switch from one feature to another..
19 * Notes on feature branch implementation
20 * We need to add a gitlive branch on the remote server..
21 * git branch gitlive (in the repo..)
23 * we clone directory into gitlive_feature/XXXXX
24 * git branch issue_XXX
25 * git checkout issue_XXX
27 * run this on the feature branch it merges and commits..
28 * git pull origin master << or gitlive..
34 GI = imports.gi.GIRepository
35 GLib = imports.gi.GLib;
37 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
38 GI.IRepository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
42 Notify = imports.gi.Notify;
44 Spawn = imports.Spawn;
46 StatusIcon = imports.StatusIcon.StatusIcon;
47 Monitor = imports.Monitor.Monitor;
50 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
51 Gtk.init (null, null);
53 var gitlive = GLib.get_home_dir() + "/gitlive";
55 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
56 var msg = new Gtk.MessageDialog({message_type:
57 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
65 var monitor = new Monitor({
69 * action: 'add' | rm | update
82 this.lastAdd = new Date();
84 // start monitoring first..
85 Monitor.prototype.start.call(this);
87 // then start our queue runner..
88 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
89 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
90 if (!_this.queue.length || _this.queueRunning) {
93 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
94 if (last < 4) { // wait 1/2 a seconnd before running.
102 var notification = new Notify.Notification({
104 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
107 notification.set_timeout(2000);
112 * - pulls the items off the queue
113 * (as commands run concurrently and new items may get added while it's running)
114 * - runs the queue items
120 this.queueRunning = true;
122 //this.queue.forEach(function (q) {
126 this.action_queue.forEach(function (q) {
129 //this.queue = []; // empty queue!
130 this.action_queue = [];
136 function readResult(sp) {
137 switch (sp.result * 1) {
139 success.push(sp.args.join(' '));
140 if (sp.output.length) success.push(sp.output + '');
141 // if (sp.stderr.length) success.push(sp.stderr + '');
144 failure.push(sp.args.join(' '));
145 if (sp.output.length) failure.push(sp.output);
146 if (sp.stderr.length) failure.push(sp.stderr);
151 cmds.forEach(function(cmd) {
152 // prevent duplicate calls..
153 if (done.indexOf(JSON.stringify(cmd)) > -1) {
156 done.push(JSON.stringify(cmd));
157 // --- we keep a list of repositories that will be pushed to at the end..
159 if (repos.indexOf(cmd.repos) < 0) {
160 repos.push(cmd.repos);
161 // Git.run(cmd.repos , 'pull'); // pull before we push!
164 var gp = gitlive + '/' + cmd.repo;
166 switch( cmd.action ) {
168 readResult(Git.run(gp, 'add', cmd.file ));
169 readResult(Git.run(gp, 'commit', src.file, { message: cmd.file} ));
173 readResult(Git.run(gp, 'rm', cmd.file ));
174 readResult(Git.run(gp, 'commit', { all: true, message: cmd.file} ));
178 readResult(Git.run(gp, 'commit', cmd.file , { message: cmd.file} ));
182 readResult(Git.run(gp, 'mv', cmd.file , cmd.target));
183 readResult(Git.run(gp, 'commit', cmd.file , cmd.target,
184 { message: 'MOVED ' + src.file +' to ' + dest.target } ));
193 repos.forEach(function(r) {
194 var sp = Git.run(gitlive + '/' +r , 'push', { all: true } );
201 if (success.length) {
202 print(success.join("\n"));
203 var notification = new Notify.Notification({
204 summary: "Git Live Commited",
205 body : success.join("\n")
209 notification.set_timeout(2000);
212 if (failure.length) {
214 var notification = new Notify.Notification({
215 summary: "Git Live ERROR!!",
216 body : failure.join("\n")
220 notification.set_timeout(5000); // show errros for longer
223 this.queueRunning = false;
226 shouldIgnore: function(f)
228 if (f.name[0] == '.') {
230 if (f.name == '.htaccess') {
236 if (f.name.match(/~$/)) {
239 // ignore anything in top level!!!!
240 if (!f.vpath.length) {
249 * set gitpath and vpath
254 parsePath: function(f)
257 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
258 f.repo = vpath_ar.shift();
259 f.gitpath = gitlive + '/' + f.repo;
260 f.vpath = vpath_ar.join('/');
267 onChanged : function(src)
269 return; // always ignore this..?
270 //this.parsePath(src);
274 * results in git add + git commit..
280 onChangesDoneHint : function(src)
283 if (this.shouldIgnore(src)) {
288 if (typeof(this.just_created[src.path]) !='undefined') {
289 delete this.just_created[src.path];
290 this.lastAdd = new Date();
292 // [ src.gitpath, 'add', src.vpath ],
293 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
296 this.action_queue.push({
306 this.lastAdd = new Date();
308 // [ src.gitpath, 'add', src.vpath ],
309 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
313 this.action_queue.push({
321 onDeleted : function(src)
324 if (this.shouldIgnore(src)) {
327 // should check if monitor needs removing..
328 // it should also check if it was a directory.. - so we dont have to commit all..
330 this.lastAdd = new Date();
332 // [ src.gitpath, 'rm' , src.vpath ],
333 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
336 this.action_queue.push({
343 onCreated : function(src)
346 if (this.shouldIgnore(src)) {
350 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
351 this.just_created[src.path] = true;
352 return; // we do not handle file create flags... - use done hint.
354 // director has bee created
355 this.monitor(src.path);
358 since git does not really handle directory adds...
360 this.lastAdd = new Date();
361 this.action_queue.push({
368 [ src.gitpath, 'add' , src.vpath, { all: true } ],
369 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
376 onAttributeChanged : function(src) {
378 if (this.shouldIgnore(src)) {
381 this.lastAdd = new Date();
385 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
387 this.action_queue.push({
396 onMoved : function(src,dest)
399 this.parsePath(dest);
401 if (src.gitpath != dest.gitpath) {
403 this.onCreated(dest);
404 this.onChangedDoneHint(dest);
407 // needs to handle move to/from unsupported types..
409 if (this.shouldIgnore(src)) {
412 if (this.shouldIgnore(dest)) {
415 this.lastAdd = new Date();
417 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
418 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
419 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
422 this.action_queue.push({
438 function errorDialog(data) {
439 var msg = new Gtk.MessageDialog({
440 message_type: Gtk.MessageType.ERROR,
441 buttons : Gtk.ButtonsType.OK,
453 // need a better icon...
459 Notify.init("gitlive");
461 monitor.add(GLib.get_home_dir() + "/gitlive");
464 //icon.signal["activate"].connect(on_left_click);