@@ -19,96 +19,108 @@ var Blamer = function(repo) {
19
19
this . initialize ( ) ;
20
20
}
21
21
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
+ }
34
41
}
35
- }
36
- }
42
+ } ,
37
43
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 ) ;
48
54
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 ) ;
52
58
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
+ }
56
62
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
+ } ,
62
68
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
+ }
84
91
}
85
92
}
86
- }
87
93
88
- return repoUtil ;
89
- }
94
+ return repoUtil ;
95
+ } ,
90
96
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
+ }
105
112
}
106
113
}
114
+
115
+ // remove leading '/' if there is one before returning
116
+ filePath = filePath . replace ( / ^ \/ / , '' ) ;
117
+ return filePath ;
107
118
}
108
119
109
- // remove leading '/' if there is one before returning
110
- filePath = filePath . replace ( / ^ \/ / , '' ) ;
111
- return filePath ;
112
- }
120
+ } ) ;
121
+
122
+ // ================
123
+ // Exports
124
+ // ================
113
125
114
126
module . exports = Blamer ;
0 commit comments