Skip to content

Commit 92b4557

Browse files
committed
fix: improve .git/config lookup, add getFromGitConfig method
1 parent 2abcdb9 commit 92b4557

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

src/finda.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,42 @@ export class Finda {
103103
}
104104

105105
/**
106-
* Finda git email address
107-
*
108-
* Try to find git email address via;
109-
* - git config --get user.email
106+
* Find info from .git/config file
110107
*
108+
* @param {string} path Path to property ('user.email')
111109
* @returns {string}
112110
* @memberof Finda
113111
*/
114-
gitEmail(): string {
115-
const cacheKey = 'gitEmail_' + process.cwd();
116-
let email = this.cache.get(cacheKey);
117-
if (email) {
118-
return email;
112+
getFromGitConfig(path: string): string {
113+
if (!get(path, 'length')) {
114+
return undefined;
115+
}
116+
const cacheKey = `git_config_${path}_${process.cwd()}`;
117+
let str = this.cache.get(cacheKey);
118+
if (str) {
119+
return str;
119120
}
120121
// let config = gitConfigSync();
121122
// if (typeof config === 'undefined') config = gitConfigSync({ path: '~/.gitconfig' });
122-
// email = (typeof config.user !== 'undefined') ? config.user.email : null;
123+
// str = get(config, path);
123124
if (shWhich('git')) {
124-
email = shExec('git config --get user.email', { silent: true }).stdout.toString().trim();
125-
this.cache.set(cacheKey, email);
125+
str = shExec(`git config --get ${path}`, { silent: true }).stdout.toString().trim();
126126
}
127-
return email;
127+
this.cache.set(cacheKey, str);
128+
return str;
129+
}
130+
131+
/**
132+
* Find git email address
133+
*
134+
* Try to find git email address via;
135+
* - git config --get user.email
136+
*
137+
* @returns {string}
138+
* @memberof Finda
139+
*/
140+
gitEmail(): string {
141+
return this.getFromGitConfig('user.email');
128142
}
129143

130144
/**
@@ -174,19 +188,7 @@ export class Finda {
174188
* @memberof Finda
175189
*/
176190
gitName(): string {
177-
const cacheKey = 'gitName_' + process.cwd();
178-
let name = this.cache.get(cacheKey);
179-
if (name) {
180-
return name;
181-
}
182-
// let config = gitConfigSync();
183-
// if (typeof config === 'undefined') config = gitConfigSync({ path: '~/.gitconfig' });
184-
// name = (typeof config.user !== 'undefined') ? config.user.name : null;
185-
if (shWhich('git')) {
186-
name = shExec('git config --get user.name', { silent: true }).stdout.toString().trim();
187-
this.cache.set(cacheKey, name);
188-
}
189-
return name;
191+
return this.getFromGitConfig('user.name');
190192
}
191193

192194
/**

0 commit comments

Comments
 (0)