aboutsummaryrefslogtreecommitdiff
path: root/lib/fetchGitData.js
blob: 6ab020944b4157d1430774cfd615b41f9879e36b (plain)
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
108
109
110
111
112
113
114
115
var exec = require("exec-sync");
var logger = require('./logger')();

var fetchGitData = function(git) {

  var i;
  var execGit = true;
  var head = {
    "author_name": {
      "format": "'%aN'",
      "default": "Unknown Author"
    },
    "author_email": {
      "format": "'%ae'",
      "default": ""
    },
    "committer_name": {
      "format": "'%cN'",
      "default": "Unknown Committer"
    },
    "committer_email": {
      "format": "'%ce'",
      "default" :""
    },
    "message": {
      "format": "'%s'",
      "default": "Unknown Commit Message"
    }
  };
  var remotes = {};

  //-- Malformed/undefined git object
  if ('undefined' === typeof git) {
    throw new Error('No options passed');
  } else if (!git.hasOwnProperty('head')) {
    throw new Error('You must provide the head');
  } else if (!git.head.hasOwnProperty('id')) {
    throw new Error('You must provide the head.id');
  }

  function saveRemote(name, url, push) {
    var key = name + "-" + url;
    if ("undefined" === typeof push || "boolean" !== typeof push) {
      push = true;
    }
    if (!remotes.hasOwnProperty(key)) {
      remotes[key] = true;
      if (push) {
        git.remotes.push({
          "name": name,
          "url": url
        });
      }
    }
  }

  //-- Set required properties of git if they weren"t provided
  if (!git.hasOwnProperty("branch")) {
    git.branch = "";
  }
  if (!git.hasOwnProperty("remotes")) {
    git.remotes = [];
  }

  //-- Assert the property types
  if ("string" !== typeof git.branch) {
    git.branch = "";
  }
  if (!(git.remotes instanceof Array)) {
    git.remotes = [];
  }

  //-- Use git?
  try {
    exec("git log -1 " + git.head.id + " --pretty=format:'%H'");
  } catch (e) {
    execGit = false;
  }

  //-- Head
  for (i in head) {
    if (!git.head.hasOwnProperty(i)) {
      if (execGit) {
        git.head[i] = exec("git log -1 " + git.head.id + " --pretty=format:" + head[i].format);
      } else {
        git.head[i] = head[i].default;
      }
    }
  }

  if (execGit) {

    //-- Branch
    if ("" === git.branch) {
      git.branch = exec("git branch").split("\n")[0].replace(/^\*\ /, "").trim();
    }

    //-- Remotes
    if (0 !== git.remotes.length) {
      for (i in git.remotes) {
        saveRemote(git.remotes[i].name, git.remotes[i].url, false);
      }
    }
    exec("git remote -v").split("\n").forEach(function(remote) {
      remote = remote.split(/\s/);
      saveRemote(remote[0], remote[1]);
    });

  }

  return git;

};

module.exports = fetchGitData;