'use strict';
var path = require('path');
var Q = require('q');
var shell = require('shelljs');

module.exports = function(grunt) {
    grunt.registerTask('dorelease', 'MelonJS Release', function() {
        var repo = path.join(__dirname, '..'));
        var config = grunt.file.readJSON(path.join(repo, 'package.json'));
        var buildPath = path.join(__dirname, '..', 'build');
        var version = config.version;
        var done = this.async();

        // using Q for promises. Using the grunt-release project's same idea
        Q()
          .then(checkout)
          .then(add)
          .then(commit)
          .then(tag)
          .then(push)
          .catch(function(msg){
            grunt.fail.warn(msg || 'release failed')
          })
          .finally(done);

        function run(cmd, msg){
            var deferred = Q.defer();
            grunt.verbose.writeln('Running: ' + cmd);

            if (nowrite) {
                grunt.log.ok(msg || cmd);
                deferred.resolve();
            }
            else {
                var success = shell.exec(cmd, {silent:true}).code === 0;

                if (success){
                    grunt.log.ok(msg || cmd);
                    deferred.resolve();
                }
                else{
                    // fail and stop execution of further tasks
                    deferred.reject('Failed when executing: `' + cmd + '`\n');
                }
            }
            return deferred.promise;
        }

        function checkout() {
            run('git checkout -f master');
        }

        function add() {
            grunt.log.oklns('ACTUAL VERSION ==> ' + config.version);
            grunt.log.oklns('BUILD FILES');
            var filenames = [
                path.join(buildPath, 'melonjs-' + version + '.js'),
                path.join(buildPath, 'melonjs-' + version + '-min.js')
            ];
            // check the build files from the actual version
            // and add the js files to be commited
            for (var i = 0; i < filenames.length; i++) {
                if (grunt.file.exists(filenames[i])) {
                    grunt.log.writeln(filenames[i]);
                    run('git add -f ' + filenames[i]);
                }
            }
        }

        // commit the new version release
        function commit() {
            run('git commit "Release '+ version  +'"');
        }

        // create new tag
        function tag() {
            run('git tag ' + version);
        }

        // push to master
        function push() {
            run('git push origin master');
        }

    });
}

@@ -652,16 +652,38 @@
 n(push)%0A
+          .then(back)%0A
         
@@ -927,151 +927,8 @@
 d);%0A
-%0A            if (nowrite) %7B%0A                grunt.log.ok(msg %7C%7C cmd);%0A                deferred.resolve();%0A            %7D%0A            else %7B%0A    
     
@@ -1001,28 +1001,24 @@
             
-    
 if (success)
@@ -1019,20 +1019,16 @@
 ccess)%7B%0A
-    
         
@@ -1069,36 +1069,32 @@
                 
-   
- 
 deferred.resolve
@@ -1105,34 +1105,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1145,20 +1145,16 @@
         
-   
- 
 // fail 
@@ -1189,20 +1189,16 @@
 r tasks%0A
-    
         
@@ -1264,34 +1264,16 @@
 '%60%5Cn');%0A
-                %7D%0A
         
@@ -1391,16 +1391,15 @@
 ut -
-f master
+-detach
 ');%0A
@@ -2446,22 +2446,98 @@
 %0A        %7D%0A%0A
+        function back() %7B%0A            run('git checkout master');%0A        %7D%0A
     %7D);%0A%7D%0A

Add back function / fix run function