-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit.js
148 lines (120 loc) · 3.68 KB
/
git.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// this is where the logic to run all the Git commands will live
var exec_command = require('child_process').exec;
var utils = require('util');
// include the command that fails on the error message... Why isn't this
// default behavior?
function exec(command, callback) {
exec_command(command, function (err, stdout, stderr) {
if (err) {
err.command = command;
err.message += " Command: " + command;
}
return callback(err, stdout, stderr);
});
}
/**
* clones a Git repo
*
* @param {String} url - assumes it is a Github url
* @param {Function} cb - callback
*/
function clone_repo(url, cb) {
var repoName = url.replace("[email protected]:","").replace(".git","").replace("/","-");
exec(utils.format("git clone %s repos/%s", url, repoName), function (err, stdout, stderr) {
if (err) return cb(err);
return cb(null, "repos/" + repoName);
});
}
/**
* clones a Git repo using https address
*
* @param {String} url - assumes it is a Github url
* @param {Function} cb - callback
*/
function clone_https_repo(url, cb) {
var repoName = url.replace("https://github.com./","").replace(".git","").replace("/","-");
exec(utils.format("git clone %s repos/%s", url, repoName), function (err, stdout, stderr) {
if (err) return cb(err);
return cb(null, "repos/" + repoName);
});
}
/**
* Checkout a specified ref
*
* @param {String} repo_loc - location of the repo on disk
* @param {String} ref - name of the ref to checkout ie. "master"
* @param {Function} cb - callback
*/
function checkout_ref(repo_loc, ref, cb) {
var command = utils.format("cd %s && git fetch origin %s && git checkout %s", repo_loc, ref, ref);
exec(command, cb);
}
/**
* Checkout a specific commit
*
* @param {String} repo_loc - location of the repo on disk
* @param {String} commit - sha of the commit
* @param {Function} cb - callback
*/
function checkout_commit(repo_loc, commit, cb) {
var command = utils.format("cd %s && git checkout %s", repo_loc, commit);
exec(command, cb);
}
/**
* Pushes a specific branch to a specific remote
*
* @param {String} repo_loc - location of the repo on disk
* @param {String} remote - name of the remote
* @param {String} branch - name of the branch
* @param {Function} cb - callback
*/
function push_repo(repo_loc, remote, branch, cb) {
var command = utils.format("cd %s && git push %s %s", repo_loc, remote, branch);
exec(command, cb);
}
/**
* Stage a set of files for commit
*
* @param {String} repo_loc - location of the repo on disk
* @param {Array} fileNames - list of file names to stage
* @param {Function} cb - callback
*/
function add_files(repo_loc, fileNames, cb) {
// in case people decide to only pass a single file, let's not blow up in
// their face
if ('string' == typeof fileNames) {
filesNames = [fileNames];
}
var count = fileNames.length;
next(0);
function next(i) {
var command = utils.format("cd %s && git add %s", repo_loc, fileNames[i]);
exec(command, function (err) {
if (err) return cb(err);
count--;
if (count) {
next(i + 1);
} else {
cb();
}
});
}
}
/**
* Commits a repo
*
* @param {String} repo_loc - location of the repository
* @param {String} commitMsg - commit message to go with the commit
* @param {Function} cb - callback
*/
function commit_repo(repo_loc, commitMsg, cb) {
var command = utils.format("cd %s && git commit -m '%s'", repo_loc, commitMsg);
exec(command, cb);
}
exports.clone = clone_repo;
exports.checkout_ref = checkout_ref;
exports.add_files = add_files;
exports.commit_repo = commit_repo;
exports.push_repo = push_repo;
exports.checkout_commit = checkout_commit;
exports.clone_https = clone_https_repo;