Skip to content

Commit f3102d5

Browse files
authored
Merge pull request #115 from nunocoracao/fix/issue-81-subdirectory-git
Fix #81: Detect existing git repo in parent directories
2 parents 4e4142e + 8db078b commit f3102d5

File tree

2 files changed

+81
-8
lines changed

2 files changed

+81
-8
lines changed

src/flow.js

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,38 @@ export default class flow {
150150
hugospinner.succeed('Hugo site created');
151151

152152
var precommand = 'cd ' + response.directory + ' && ';
153-
const gitspinner = ora('Initializing Git').start();
154-
await utils.run(precommand + 'git init', false)
155-
gitspinner.succeed('Git initialized');
153+
154+
// Check if there's an existing git repo in parent directories
155+
const existingGitRepo = utils.findGitRepoInParents(process.cwd());
156+
let skipGitInit = false;
157+
158+
if (existingGitRepo) {
159+
console.log(chalk.yellow('\nFound existing Git repository at: ' + existingGitRepo));
160+
const gitChoice = await safePrompt({
161+
type: 'select',
162+
name: 'option',
163+
message: 'How would you like to handle Git?',
164+
choices: [
165+
'Use existing repository (recommended for subdirectories)',
166+
'Create new repository in ' + response.directory
167+
]
168+
});
169+
170+
if (!gitChoice) {
171+
flow.showMain();
172+
return;
173+
}
174+
175+
skipGitInit = gitChoice.option.includes('Use existing');
176+
}
177+
178+
if (!skipGitInit) {
179+
const gitspinner = ora('Initializing Git').start();
180+
await utils.run(precommand + 'git init', false)
181+
gitspinner.succeed('Git initialized');
182+
} else {
183+
console.log(chalk.green('✔ Using existing Git repository'));
184+
}
156185

157186
const blowfishspinner = ora('Installing Blowfish').start();
158187
const submoduleExitCode = await utils.run(precommand + 'git submodule add --depth 1 -b main https://github.com/nunocoracao/blowfish.git themes/blowfish', false, true);
@@ -241,9 +270,38 @@ export default class flow {
241270
hugospinner.succeed('Template cloned');
242271

243272
var precommand = 'cd ' + response.directory + ' && ';
244-
const gitspinner = ora('Initializing Git').start();
245-
await utils.run(precommand + 'git init', false)
246-
gitspinner.succeed('Git initialized');
273+
274+
// Check if there's an existing git repo in parent directories
275+
const existingGitRepo = utils.findGitRepoInParents(process.cwd());
276+
let skipGitInit = false;
277+
278+
if (existingGitRepo) {
279+
console.log(chalk.yellow('\nFound existing Git repository at: ' + existingGitRepo));
280+
const gitChoice = await safePrompt({
281+
type: 'select',
282+
name: 'option',
283+
message: 'How would you like to handle Git?',
284+
choices: [
285+
'Use existing repository (recommended for subdirectories)',
286+
'Create new repository in ' + response.directory
287+
]
288+
});
289+
290+
if (!gitChoice) {
291+
flow.showMain();
292+
return;
293+
}
294+
295+
skipGitInit = gitChoice.option.includes('Use existing');
296+
}
297+
298+
if (!skipGitInit) {
299+
const gitspinner = ora('Initializing Git').start();
300+
await utils.run(precommand + 'git init', false)
301+
gitspinner.succeed('Git initialized');
302+
} else {
303+
console.log(chalk.green('✔ Using existing Git repository'));
304+
}
247305

248306
const blowfishspinner = ora('Installing Blowfish').start();
249307
await utils.directoryDelete(response.directory + '/themes/blowfish')

src/utils.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,30 @@ export default class utils {
134134
fs.writeFileSync(path, result, 'utf8')
135135
}
136136

137-
static directoryExists(path) {
137+
static directoryExists(dirPath) {
138138
try {
139-
return fs.existsSync(path);
139+
return fs.existsSync(dirPath);
140140
} catch (err) {
141141
console.log(err)
142142
return false;
143143
}
144144
}
145145

146+
static findGitRepoInParents(startPath) {
147+
// Check if there's a .git folder in any parent directory
148+
let currentPath = path.resolve(startPath);
149+
const root = path.parse(currentPath).root;
150+
151+
while (currentPath !== root) {
152+
const gitPath = path.join(currentPath, '.git');
153+
if (fs.existsSync(gitPath)) {
154+
return currentPath;
155+
}
156+
currentPath = path.dirname(currentPath);
157+
}
158+
return null;
159+
}
160+
146161
static directoryIsEmpty(path) {
147162
try {
148163
const files = fs.readdirSync(path);

0 commit comments

Comments
 (0)