Skip to content

Commit

Permalink
fix(clone): print help with just repo name (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: 2nthony <[email protected]>
  • Loading branch information
2nthony and 2nthony authored Mar 26, 2023
1 parent 2280773 commit c385998
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 })

Expand Down
20 changes: 14 additions & 6 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c385998

Please sign in to comment.