From c3859989bfded6562ac32a4864acc15c895e26fe Mon Sep 17 00:00:00 2001 From: 2nthony Date: Sun, 26 Mar 2023 16:39:15 +0800 Subject: [PATCH] fix(clone): print help with just repo name (#73) Co-authored-by: 2nthony <2nthony@192.168.0.12> --- src/commands/clone.ts | 14 +++++++++++++- src/git.ts | 20 ++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/commands/clone.ts b/src/commands/clone.ts index f211487..cd07d49 100644 --- a/src/commands/clone.ts +++ b/src/commands/clone.ts @@ -2,6 +2,7 @@ import { clone } from '../git' import { parseCliOptionsToGitArgs } from '../args' import type { PluginApi } from '../types' import { readConfig } from '../config' +import { analyzeUrl } from '../url' export const cloneCommand: PluginApi = { extend(api) { @@ -13,17 +14,28 @@ export const cloneCommand: PluginApi = { type: [Boolean], }) .ignoreOptionDefaultValue() + .example('ghq clone ghq (requires `.gitconfig` has `github.user` or `user.name`)') .example('ghq clone 2nthony/ghq') .example('ghq clone github.com/2nthony/ghq') .example('ghq clone https://github.com/2nthony/ghq') .example('ghq get 2nthony/ghq') .allowUnknownOptions() - .action(async (repo, options) => { + .action(async (repo: string, options) => { if (!repo) { api.cli.outputHelp() return } + // ghq clone [repo] + // means clone my repos? + if (!repo.includes('/')) { + const { user } = analyzeUrl(repo) + if (!user) { + api.cli.outputHelp() + return + } + } + const config = await readConfig() const args = parseCliOptionsToGitArgs({ ...config, ...options }) diff --git a/src/git.ts b/src/git.ts index 99cc985..c33e0bf 100644 --- a/src/git.ts +++ b/src/git.ts @@ -39,11 +39,19 @@ export async function init(repoUrl: string, ...args: string[]) { } function getUsername() { - try { - const stdout = execSync('git config --get user.name') - return stdout.toString().trim() - } - catch { - return '' + let username = '' + function get(keypath: string) { + try { + return execSync(`git config --get ${keypath}`).toString().trim() + } + catch { + return '' + } } + + username = get('github.user') + if (!username) + username = get('user.name') + + return username }