# Code coverage report for lib/git/commands.js

## Statements: 100% (62 / 62) Branches: 100% (20 / 20) Functions: 100% (18 / 18) Lines: 100% (55 / 55) Ignored: 3 statements, 3 branches

All files » lib/git/ » commands.js

    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
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    1081

    1

    1
    455
    455
    455
    455

    455
    455

    455
    2

    453

    1
    42

    1
    78

    1
    2

    1
    1

    1
    80

    1
    3

    1
    47

    1
    38

    1
    34
    1

    34

    34

    1
    28

    28

    28
    28
    28

    34
    34
    34

    34
    34

    28

    1
    46

    46

    46
    46
    46
    46

    46

    1
    90

     var exec = require('child_process').exec;

    var logger = require('../logging.js');

    /*
     * Run a command.  If it fails, return the failure message as the first parameter
     * of the callback.  If it succeeds, return a null first parameter and a trimmed
     * stdout as the second parameter.
     */
    var run_command = function(cmd, cwd, cb) {
      logger.trace('Running %s in %s', cmd, cwd);
      var child = exec(cmd, {cwd: cwd}, function(err, stdout, stderr) {
        if (stdout) stdout = stdout.trim();
        if (stderr) stderr = stderr.trim();

        if (stdout.length > 0) logger.trace("stdout:\n" + stdout);
        if (stderr.length > 0) logger.trace("stderr:\n" + stderr);

        if (err) {
          return cb(new Error(err + ' ' + stderr));
        }

        cb(null, stdout);
      });
    };

    exports.init = function(cwd, cb) {
      run_command('git init', cwd, cb);
    };

    exports.add = function(filename, cwd, cb) {
      run_command('git add "' + filename + '"', cwd, cb);
    };

    exports.mv = function(old_file, new_file, cwd, cb) {
      run_command('git mv "' + old_file + '" "' + new_file + '"', cwd, cb);
    };

    exports.delete = function(filename, cwd, cb) {
      run_command('git rm "' + filename + '"', cwd, cb);
    };

    exports.commit = function(message, cwd, cb) {
      run_command('git commit -m "' + message + '"', cwd, cb);
    };

    exports.checkout_branch = function(branch_name, cwd, cb) {
      run_command('git checkout -b ' + branch_name, cwd, cb);
    };

    exports.clone = function(repo_url, branch_name, cwd, cb) {
      run_command('git clone -b ' + branch_name + ' ' + repo_url + ' ' + branch_name, cwd, cb);
    };

    exports.pull = function(cwd, cb) {
      run_command('git pull', cwd, cb);
    };

    var fixup_path = function(path) {
      if ((path.charAt(0) === '"') && (path.charAt(path.length-1) === '"')) {
        path = path.substring(1, path.length-1);
      }

      path = path.replace(/\\\\/g, '\\');

      return path;
    };

    exports.listChangedFiles = function(from_ref, to_ref, cwd, cb) {
      run_command('git diff --name-status ' + from_ref + ' ' + to_ref, cwd, function(err, output) {
        /* istanbul ignore if */
        Iif (err) return cb(err);

        var records = [];
        var lines = output.split('\n');
        lines.forEach(function(line) {
          /* istanbul ignore if */
          Iif (line.length < 2) return;
          var type = line.charAt(0);
          var rest = fixup_path(line.substring(1).trim());

          logger.trace(rest);
          records.push({'type': type, 'path': rest});
        });

        cb(null, records);
      });
    };

    exports.listAllFiles = function(cwd, cb) {
      run_command('git ls-tree --name-status -r HEAD', cwd, function(err, output) {
        /* istanbul ignore if */
        Iif (err) return cb(err);

        var records = [];
        var files = output.split('\n');
        files.forEach(function(file) {
          records.push({'type': 'M', 'path': file});
        });

        cb(null, records);
      });
    };

    exports.getCurrentRef = function(cwd, cb) {
      run_command('git log -n 1 --pretty=format:"%H"', cwd, cb);
    };