jenkins-bot has submitted this change and it was merged.

Change subject: Avoid use of `child_process.exec`.
......................................................................


Avoid use of `child_process.exec`.

The `exec` function spawns a subshell, which is unnecessary here.
In general, one should always use `execFile` instead of `exec`, which
also avoids a possible security issue with quoted arguments (although
that issue is moot here).  Sets the working directory, just to
be safe.

Also fixes a minor race condition: if a second request for `/_version`
comes in while the first one is still waiting for `git` to return,
the second call wouldn't get the `sha` field added.

Change-Id: Icc1eb9be85a288a457973116dc8ded7aca3e97b8
---
M api/routes.js
1 file changed, 20 insertions(+), 13 deletions(-)

Approvals:
  Arlolra: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/api/routes.js b/api/routes.js
index 3214573..6b8ecc9 100644
--- a/api/routes.js
+++ b/api/routes.js
@@ -7,7 +7,7 @@
        fs = require('fs'),
        url = require('url'),
        util = require('util'),
-       childProc = require('child_process'),
+       child_process = require('child_process'),
        cluster = require('cluster'),
        domino = require('domino'),
        pkg = require('../package.json'),
@@ -432,19 +432,26 @@
 // Return Parsoid version based on package.json + git sha1 if available
 var versionCache;
 routes.version = function( req, res ) {
-       if ( versionCache ) {
-               return res.json( versionCache );
+       if ( !versionCache ) {
+               versionCache = Promise.resolve({
+                       name: pkg.name,
+                       version: pkg.version
+               }).then(function( v ) {
+                       return Promise.promisify(
+                               child_process.execFile, ['stdout', 'stderr'], 
child_process
+                       )( 'git', ['rev-parse','HEAD'], {
+                               cwd: path.join(__dirname, '..')
+                       }).then(function( out ) {
+                               v.sha = out.stdout.slice(0, -1);
+                               return v;
+                       }, function( err ) {
+                               /* ignore the error, maybe this isn't a git 
checkout */
+                               return v;
+                       });
+               });
        }
-       versionCache = {
-               name: pkg.name,
-               version: pkg.version
-       };
-       Promise.promisify(
-               childProc.exec, false, childProc
-       )( 'git rev-parse HEAD' ).then(function( stdout ) {
-               versionCache.sha = stdout.slice(0, -1);
-       }).finally(function() {
-               res.json( versionCache );
+       return versionCache.then(function( v ) {
+               res.json( v );
        });
 };
 

-- 
To view, visit https://gerrit.wikimedia.org/r/169773
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icc1eb9be85a288a457973116dc8ded7aca3e97b8
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to