Skip to content

Commit c3ea635

Browse files
committed
adding some formatting consistency
1 parent 8f85d2a commit c3ea635

File tree

2 files changed

+152
-130
lines changed

2 files changed

+152
-130
lines changed

lib/util/blamer.js

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,96 +19,108 @@ var Blamer = function(repo) {
1919
this.initialize();
2020
}
2121

22-
/**
23-
* Initializes this Blamer instance, by creating git-tools repos for the root
24-
* repository and submodules.
25-
*/
26-
Blamer.prototype.initialize = function() {
27-
this.tools = {};
28-
this.tools.root = new GitCommander(this.repo.getWorkingDirectory());
29-
30-
var submodules = this.repo.submodules;
31-
if (submodules) {
32-
for (var submodulePath in submodules) {
33-
this.tools[submodulePath] = new GitCommander(this.repo.getWorkingDirectory() + '/' + submodulePath);
22+
// ================
23+
// Instance Methods
24+
// ================
25+
26+
_.extend(Blamer.prototype, {
27+
28+
/**
29+
* Initializes this Blamer instance, by creating git-tools repos for the root
30+
* repository and submodules.
31+
*/
32+
initialize: function() {
33+
this.tools = {};
34+
this.tools.root = new GitCommander(this.repo.getWorkingDirectory());
35+
36+
var submodules = this.repo.submodules;
37+
if (submodules) {
38+
for (var submodulePath in submodules) {
39+
this.tools[submodulePath] = new GitCommander(this.repo.getWorkingDirectory() + '/' + submodulePath);
40+
}
3441
}
35-
}
36-
}
42+
},
3743

38-
/**
39-
* Blames the given filePath and calls callback with blame lines or error.
40-
*
41-
* @param {string} filePath - filePath to blame
42-
* @param {function} callback - callback to call back with blame data
43-
*/
44-
Blamer.prototype.blame = function(filePath, callback) {
45-
// Ensure file path is relative to root repo
46-
filePath = this.repo.relativize(filePath);
47-
var repoUtil = this.repoUtilForPath(filePath);
44+
/**
45+
* Blames the given filePath and calls callback with blame lines or error.
46+
*
47+
* @param {string} filePath - filePath to blame
48+
* @param {function} callback - callback to call back with blame data
49+
*/
50+
blame: function(filePath, callback) {
51+
// Ensure file path is relative to root repo
52+
filePath = this.repo.relativize(filePath);
53+
var repoUtil = this.repoUtilForPath(filePath);
4854

49-
// Ensure that if this file is in a submodule, we remove the submodule dir
50-
// from the path
51-
filePath = this.removeSubmodulePrefix(filePath);
55+
// Ensure that if this file is in a submodule, we remove the submodule dir
56+
// from the path
57+
filePath = this.removeSubmodulePrefix(filePath);
5258

53-
if (!_.isFunction(callback)) {
54-
throw new Error('Must be called with a callback function');
55-
}
59+
if (!_.isFunction(callback)) {
60+
throw new Error('Must be called with a callback function');
61+
}
5662

57-
// Make the async blame call on the git repo
58-
repoUtil.blame(filePath, function(err, blame) {
59-
callback(err, blame);
60-
});
61-
}
63+
// Make the async blame call on the git repo
64+
repoUtil.blame(filePath, function(err, blame) {
65+
callback(err, blame);
66+
});
67+
},
6268

63-
/**
64-
* Utility to get the GitCommander repository for the given filePath. Takes into
65-
* account whether the file is part of a submodule and returns that repository
66-
* if necessary.
67-
*
68-
* @param {string} filePath - the path to the file in question.
69-
*/
70-
Blamer.prototype.repoUtilForPath = function(filePath) {
71-
var submodules = this.repo.submodules;
72-
73-
// By default, we return the root GitCommander repository.
74-
var repoUtil = this.tools.root;
75-
76-
// if we have submodules, loop through them and see if the given file path
77-
// belongs inside one of the repositories. If so, we return the GitCommander repo
78-
// for that submodule.
79-
if (submodules) {
80-
for (var submodulePath in submodules) {
81-
var submoduleRegex = new RegExp('^' + submodulePath);
82-
if (submoduleRegex.test(filePath)) {
83-
repoUtil = this.tools[submodulePath];
69+
/**
70+
* Utility to get the GitCommander repository for the given filePath. Takes into
71+
* account whether the file is part of a submodule and returns that repository
72+
* if necessary.
73+
*
74+
* @param {string} filePath - the path to the file in question.
75+
*/
76+
repoUtilForPath: function(filePath) {
77+
var submodules = this.repo.submodules;
78+
79+
// By default, we return the root GitCommander repository.
80+
var repoUtil = this.tools.root;
81+
82+
// if we have submodules, loop through them and see if the given file path
83+
// belongs inside one of the repositories. If so, we return the GitCommander repo
84+
// for that submodule.
85+
if (submodules) {
86+
for (var submodulePath in submodules) {
87+
var submoduleRegex = new RegExp('^' + submodulePath);
88+
if (submoduleRegex.test(filePath)) {
89+
repoUtil = this.tools[submodulePath];
90+
}
8491
}
8592
}
86-
}
8793

88-
return repoUtil;
89-
}
94+
return repoUtil;
95+
},
9096

91-
/**
92-
* If the file path given is inside a submodule, removes the submodule
93-
* directory prefix.
94-
*
95-
* @param {string} filePath - path to file to relativize
96-
* @param {Repo} toolsRepo - git-tools Repo
97-
*/
98-
Blamer.prototype.removeSubmodulePrefix = function(filePath) {
99-
var submodules = this.repo.submodules;
100-
if (submodules) {
101-
for (var submodulePath in submodules) {
102-
var submoduleRegex = new RegExp('^' + submodulePath);
103-
if (submoduleRegex.test(filePath)) {
104-
filePath = filePath.replace(submoduleRegex, '');
97+
/**
98+
* If the file path given is inside a submodule, removes the submodule
99+
* directory prefix.
100+
*
101+
* @param {string} filePath - path to file to relativize
102+
* @param {Repo} toolsRepo - git-tools Repo
103+
*/
104+
removeSubmodulePrefix: function(filePath) {
105+
var submodules = this.repo.submodules;
106+
if (submodules) {
107+
for (var submodulePath in submodules) {
108+
var submoduleRegex = new RegExp('^' + submodulePath);
109+
if (submoduleRegex.test(filePath)) {
110+
filePath = filePath.replace(submoduleRegex, '');
111+
}
105112
}
106113
}
114+
115+
// remove leading '/' if there is one before returning
116+
filePath = filePath.replace(/^\//, '');
117+
return filePath;
107118
}
108119

109-
// remove leading '/' if there is one before returning
110-
filePath = filePath.replace(/^\//, '');
111-
return filePath;
112-
}
120+
});
121+
122+
// ================
123+
// Exports
124+
// ================
113125

114126
module.exports = Blamer;

lib/util/gitCommander.js

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,79 @@ function GitCommander(path) {
1313
this.workingDirectory = path;
1414
}
1515

16-
/**
17-
* Spawns a process to execute a git command in the GitCommander instances
18-
* working directory.
19-
*
20-
* @param {array|string} args - arguments to call `git` with on the command line
21-
* @param {function} callback - node callback for error and command output
22-
*/
23-
GitCommander.prototype.exec = function(args, callback) {
24-
if (!_.isArray(args) || !_.isFunction(callback)) {
25-
return;
26-
}
16+
// ================
17+
// Instance Methods
18+
// ================
2719

28-
var stdout = '';
29-
var stderr = '';
30-
var child = child_process.spawn('git', args, {cwd: this.workingDirectory});
31-
var processError;
20+
_.extend(GitCommander.prototype, {
3221

33-
child.stdout.on('data', function(data) {
34-
stdout += data;
35-
});
22+
/**
23+
* Spawns a process to execute a git command in the GitCommander instances
24+
* working directory.
25+
*
26+
* @param {array|string} args - arguments to call `git` with on the command line
27+
* @param {function} callback - node callback for error and command output
28+
*/
29+
exec: function(args, callback) {
30+
if (!_.isArray(args) || !_.isFunction(callback)) {
31+
return;
32+
}
3633

37-
child.stderr.on('data', function(data) {
38-
stderr += data;
39-
});
34+
var stdout = '';
35+
var stderr = '';
36+
var child = child_process.spawn('git', args, {cwd: this.workingDirectory});
37+
var processError;
4038

41-
child.on('error', function(error) {
42-
processError = error;
43-
});
39+
child.stdout.on('data', function(data) {
40+
stdout += data;
41+
});
4442

45-
child.on('close', function(errorCode) {
46-
if (processError) {
47-
return callback(processError);
48-
}
43+
child.stderr.on('data', function(data) {
44+
stderr += data;
45+
});
4946

50-
if (errorCode) {
51-
var error = new Error(stderr);
52-
error.code = errorCode;
53-
return callback(error);
54-
}
47+
child.on('error', function(error) {
48+
processError = error;
49+
});
5550

56-
return callback(null, stdout.trimRight());
57-
});
58-
}
51+
child.on('close', function(errorCode) {
52+
if (processError) {
53+
return callback(processError);
54+
}
5955

60-
/**
61-
* Executes git blame on the input file in the instances working directory
62-
*
63-
* @param {string} fileName - name of file to blame, relative to the repos
64-
* working directory
65-
* @param {function} callback - callback funtion to call with results or error
66-
*/
67-
GitCommander.prototype.blame = function(fileName, callback) {
68-
var args = ['blame', '--line-porcelain', fileName];
56+
if (errorCode) {
57+
var error = new Error(stderr);
58+
error.code = errorCode;
59+
return callback(error);
60+
}
6961

70-
// Execute blame command and parse
71-
this.exec(args, function(err, blame) {
72-
if (err) {
73-
return callback(err, blame);
74-
}
62+
return callback(null, stdout.trimRight());
63+
});
64+
},
7565

76-
return callback(null, blameFormatter.parseBlame(blame));
77-
});
78-
};
66+
/**
67+
* Executes git blame on the input file in the instances working directory
68+
*
69+
* @param {string} fileName - name of file to blame, relative to the repos
70+
* working directory
71+
* @param {function} callback - callback funtion to call with results or error
72+
*/
73+
blame: function(fileName, callback) {
74+
var args = ['blame', '--line-porcelain', fileName];
7975

76+
// Execute blame command and parse
77+
this.exec(args, function(err, blame) {
78+
if (err) {
79+
return callback(err, blame);
80+
}
81+
82+
return callback(null, blameFormatter.parseBlame(blame));
83+
});
84+
}
85+
});
86+
87+
// ================
8088
// Exports
89+
// ================
90+
8191
module.exports = GitCommander;

0 commit comments

Comments
 (0)