all files / src/git/ commit.js

82.35% Statements 14/17
87.5% Branches 7/8
75% Functions 3/4
80% Lines 12/15
2 statements, 3 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                                                                   
import os from 'os';
import git from 'gulp-git';
import gulp from 'gulp';
import dedent from 'dedent';
import {isString} from '../common/util';
 
export { commit };
 
/**
 * Asynchronously git commit at a given path with a message
 */
function commit(sh, repoPath, message, options, done) I{
 
  var alreadyEnded = false;
  let dedentedMessage = dedent(message);
  let escapedMessage = dedentedMessage.replace(/"/g, '\\"');
  let operatingSystemNormalizedMessage;
  // On windows we must use an array in gulp-git instead of a string because
  // command line parsing works differently
  if(os.platform()=="win32") {
    operatingSystemNormalizedMessage = escapedMessage.split(/\r?\n/);
  } else {
    operatingSystemNormalizedMessage = escapedMessage;
  }
  
  // Get a gulp stream based off the config
  gulp.src(repoPath)
 
    // Format then commit
    .pipe(git.commit(operatingSystemNormalizedMessage, options))
    
    // Write progress to the screen
    .on('data',function(data) {
      
      // Ignore this for code coverage since it is only there 
      // to make our testing suite pretty
      /* istanbul ignore if  */
      Iif(!options.quiet) {
        if(isString(data))
        {
         process.stdout.write(data); 
        } 
      }
    })
    
    // Handle commit success
    .on('end', function() {
      // TODO: Bug? Done is fired twice :(
      if(!alreadyEnded)
      {
        done();
        alreadyEnded=true; 
      }
    })
    
    // Handle commit failure
    .on('error', function (err) {
      console.error(err);
      done(err);
    });
 
}