You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ni...@apache.org on 2015/05/20 19:36:13 UTC

cordova-coho git commit: Add command to merge PR. This closes 78.

Repository: cordova-coho
Updated Branches:
  refs/heads/master 1d30ac0bd -> e337cabca


Add command to merge PR. This closes 78.


Project: http://git-wip-us.apache.org/repos/asf/cordova-coho/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-coho/commit/e337cabc
Tree: http://git-wip-us.apache.org/repos/asf/cordova-coho/tree/e337cabc
Diff: http://git-wip-us.apache.org/repos/asf/cordova-coho/diff/e337cabc

Branch: refs/heads/master
Commit: e337cabca2e38fba1e7288df59dd1200d84c41dc
Parents: 1d30ac0
Author: Nikhil Khandelwal <ni...@microsoft.com>
Authored: Fri May 15 16:58:17 2015 -0700
Committer: Nikhil Khandelwal <ni...@microsoft.com>
Committed: Wed May 20 10:33:23 2015 -0700

----------------------------------------------------------------------
 src/executil.js    |  2 +-
 src/flagutil.js    | 18 +---------
 src/gitutil.js     |  8 +++--
 src/main.js        | 24 ++++++++-----
 src/merge-pr.js    | 93 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/repo-update.js |  5 +--
 src/repoutil.js    | 16 +++++++++
 7 files changed, 134 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/executil.js
----------------------------------------------------------------------
diff --git a/src/executil.js b/src/executil.js
index 301f007..6938a82 100644
--- a/src/executil.js
+++ b/src/executil.js
@@ -52,7 +52,7 @@ function execHelper(cmdAndArgs, silent, allowError) {
     var result = superspawn.spawn(cmdAndArgs[0], cmdAndArgs.slice(1), {stdio: (silent && (silent !== 2)) ? 'default' : 'inherit'});
     return result.then(null, function(e) {
         if (allowError) {
-            return null;
+            throw e;
         } else if (+silent != 1) {
             print(e.output);
         }

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/flagutil.js
----------------------------------------------------------------------
diff --git a/src/flagutil.js b/src/flagutil.js
index 29676db..baefb98 100644
--- a/src/flagutil.js
+++ b/src/flagutil.js
@@ -42,22 +42,6 @@ exports.registerDepthFlag = function(opt) {
     });
 }
 
-function resolveCwdRepo() {
-    var curPath = apputil.resolveUserSpecifiedPath('.');
-    var prevPath;
-    for (;;) {
-        var value = path.basename(curPath);
-        if (repoutil.getRepoById(value)) {
-            return value;
-        }
-        curPath = path.resolve(curPath, '..');
-        if (curPath == prevPath) {
-            apputil.fatal('--repo=. could not be resolved because you are not in a cordova repository.');
-        }
-        prevPath = curPath;
-    }
-}
-
 exports.computeReposFromFlag = function(flagValue, includeSvn) {
     var values = flagValue === true ? [] : Array.isArray(flagValue) ? flagValue : [flagValue];
     var ret = [];
@@ -70,7 +54,7 @@ exports.computeReposFromFlag = function(flagValue, includeSvn) {
     }
     values.forEach(function(value) {
         if (value == '.') {
-            value = resolveCwdRepo();
+            value = repoutil.resolveCwdRepo();
         }
         var repo = repoutil.getRepoById(value);
         var group = repoutil.repoGroups[value];

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/gitutil.js
----------------------------------------------------------------------
diff --git a/src/gitutil.js b/src/gitutil.js
index 5f5d187..355197b 100644
--- a/src/gitutil.js
+++ b/src/gitutil.js
@@ -43,12 +43,14 @@ exports.tagExists = function*(tagName) {
 }
 
 exports.retrieveCurrentBranchName = function*(allowDetached) {
-    var ref = yield executil.execHelper(executil.ARGS('git symbolic-ref HEAD'), true, true);
-    if (!ref) {
+    var ref;
+    try {
+        ref = yield executil.execHelper(executil.ARGS('git symbolic-ref HEAD'), true, true);
+    } catch (e) {
         if (allowDetached) {
             return null;
         }
-        throw new Error('Aborted due to repo ' + process.cwd() + ' not being on a named branch');
+        throw new Error('Aborted due to repo ' + process.cwd() + ' not being on a named branch. ' + e.message);
     }
     var match = /refs\/heads\/(.*)/.exec(ref);
     if (!match) {

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/main.js
----------------------------------------------------------------------
diff --git a/src/main.js b/src/main.js
index 5e69f32..374f3f4 100644
--- a/src/main.js
+++ b/src/main.js
@@ -122,6 +122,11 @@ module.exports = function() {
             desc: 'Shows a list of GitHub pull requests for all specified repositories.',
             entryPoint: lazyRequire('./list-pulls')
         }, {
+            name: 'merge-pr',
+            desc: 'Merges specified PR',
+            entryPoint: lazyRequire('./merge-pr'),
+            noChdir : true
+        }, {
             name: 'last-week',
             desc: 'Prints out git logs of things that happened last week.',
             entryPoint: lazyRequire('./last-week')
@@ -172,11 +177,6 @@ module.exports = function() {
     var command;
     var argv = optimist
         .usage(usage)
-        .options('chdir', {
-            desc: 'Use --no-chdir to run in your CWD instead of the parent of cordova-coho/',
-            type: 'boolean',
-            default: true
-         })
         .check(function(argv) {
             command = argv._[0];
             if (!command) {
@@ -189,10 +189,16 @@ module.exports = function() {
                 throw 'No repositories specified, see list-repos';
             }
         }).argv;
-
-    // Change directory to be a sibling of coho.
-    apputil.initWorkingDir(argv.chdir);
-
+    if(!commandMap[command].noChdir) {
+        optimist.options('chdir', {
+            desc: 'Use --no-chdir to run in your CWD instead of the parent of cordova-coho/',
+            type: 'boolean',
+            default: true
+         });
+         
+        // Change directory to be a sibling of coho.
+        apputil.initWorkingDir(argv.chdir);
+    }
     var entry = commandMap[command].entryPoint;
     co(entry)();
 };

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/merge-pr.js
----------------------------------------------------------------------
diff --git a/src/merge-pr.js b/src/merge-pr.js
new file mode 100644
index 0000000..0f0c76d
--- /dev/null
+++ b/src/merge-pr.js
@@ -0,0 +1,93 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+*/
+
+var flagutil = require('./flagutil');
+var optimist = require('optimist');
+var shelljs = require('shelljs');
+var executil = require('./executil');
+var gitutil = require('./gitutil');
+var superspawn = require('./superspawn');
+var chalk = require('chalk');
+var repoutil = require('./repoutil');
+
+module.exports = function *(argv) {
+    var opt = flagutil.registerHelpFlag(optimist);
+    opt.options('pr', {
+            desc: 'PR # that needs to be merged',
+            demand: true
+        });
+    argv = opt
+        .usage('Merges the pull request to master\n' +
+        '\n' +
+        'Usage: $0 merge-pr --pr 111')
+        .argv;
+   if (argv.h) {
+        optimist.showHelp();
+        process.exit(1);
+   }
+   
+   var localBranch = 'pr/' + argv.pr;
+   var currentRepo = repoutil.getRepoById(repoutil.resolveCwdRepo());
+   var remote = 'https://github.com/apache/' + currentRepo.repoName;
+   var origin = 'https://git-wip-us.apache.org/repos/asf/' + currentRepo.repoName;
+   yield gitutil.stashAndPop('', function*() {
+       yield executil.execHelper(executil.ARGS('git checkout master'));
+    
+       var commit = yield executil.execHelper(executil.ARGS('git rev-parse HEAD'), /*silent*/ true);
+       //var afterStartingCommit = new Date(startingCommitDate.valueOf() + 1); /* add 1 ms*/
+
+       yield executil.execHelper(['git', 'pull', origin, 'master']);
+       yield executil.execHelper(['git', 'fetch', /*force update*/ '-fu', remote,
+            'refs/pull/' + argv.pr + '/head:' + localBranch]);
+       try {
+            yield executil.execHelper(executil.ARGS('git merge --ff-only ' + localBranch),
+                /*silent*/ true, /*allowError*/ true);
+       } catch (e) {   
+           if (e.message.indexOf('fatal: Not possible to fast-forward, aborting.') > 0) {
+               // Let's try to rebase
+               yield executil.execHelper(executil.ARGS('git checkout ' + localBranch));
+               yield executil.execHelper('git', 'pull', '--rebase', origin, 'master');
+               yield executil.execHelper(executil.ARGS('git checkout master'));
+               yield executil.execHelper(executil.ARGS('git merge --ff-only ' + localBranch));
+               var commitMessage = yield executil.execHelper(executil.ARGS('git log --format=%B -n 1 HEAD'), /*silent*/ true);
+               yield executil.execHelper(['git', 'commit', '--amend', '-m', 
+                    commitMessage + '. This closes #' + argv.pr]);
+           } else {
+               throw e;
+           }
+       }
+      console.log(); 
+      var commits =  yield executil.execHelper(['git', 'log',
+            '--graph',
+            '--pretty=format:%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset',
+            '--abbrev-commit',
+            '--stat',
+            commit + '..HEAD'], /*silent*/ true);
+
+       if (commits) {
+           console.log('---------------');
+           console.log('Commits merged:');
+           console.log('---------------');
+           console.log(commits);
+           console.log(chalk.red.bold('Please test, squash, and rebase to meaningful commits before pushing. '));
+       } else {
+           console.log(chalk.red.bold('Nothing to merge - Has this already been merged?'));
+       }
+   });
+};
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/repo-update.js
----------------------------------------------------------------------
diff --git a/src/repo-update.js b/src/repo-update.js
index 5c77f00..f6daf58 100644
--- a/src/repo-update.js
+++ b/src/repo-update.js
@@ -129,8 +129,9 @@ function *updateRepos(repos, branches, noFetch) {
                         yield gitutil.gitCheckout(branchName);
                         var ret = yield executil.execHelper(executil.ARGS('git merge --ff-only', repo.remoteName + '/' + branchName), false, true);
                         if (ret === null) {
-                            ret = yield executil.execHelper(executil.ARGS('git rebase ' + repo.remoteName + '/' + branchName), false, true);
-                            if (ret === null) {
+                            try {
+                                ret = yield executil.execHelper(executil.ARGS('git rebase ' + repo.remoteName + '/' + branchName), false, true);
+                            } catch (ret) {
                                 apputil.fatal('\n\nUpdate failed. Run again with --no-fetch to try again without re-fetching.');
                             }
                         }

http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/e337cabc/src/repoutil.js
----------------------------------------------------------------------
diff --git a/src/repoutil.js b/src/repoutil.js
index 83a7bdd..f48fb5a 100644
--- a/src/repoutil.js
+++ b/src/repoutil.js
@@ -501,6 +501,22 @@ exports.forEachRepo = function*(repos, func) {
     }
 }
 
+function resolveCwdRepo() {
+    var curPath = apputil.resolveUserSpecifiedPath('.');
+    var prevPath;
+    for (;;) {
+        var value = path.basename(curPath);
+        if (getRepoById(value)) {
+            return value;
+        }
+        curPath = path.resolve(curPath, '..');
+        if (curPath == prevPath) {
+            apputil.fatal('Repo could not be resolved because you are not in a cordova repository.');
+        }
+        prevPath = curPath;
+    }
+}
+exports.resolveCwdRepo = resolveCwdRepo;
 
 function getRepoDir(repo) {
     var baseWorkingDir = apputil.getBaseDir();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org