Skip to content

Commit c385998

Browse files
2nthony2nthony
andauthored
fix(clone): print help with just repo name (#73)
Co-authored-by: 2nthony <[email protected]>
1 parent 2280773 commit c385998

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/commands/clone.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { clone } from '../git'
22
import { parseCliOptionsToGitArgs } from '../args'
33
import type { PluginApi } from '../types'
44
import { readConfig } from '../config'
5+
import { analyzeUrl } from '../url'
56

67
export const cloneCommand: PluginApi = {
78
extend(api) {
@@ -13,17 +14,28 @@ export const cloneCommand: PluginApi = {
1314
type: [Boolean],
1415
})
1516
.ignoreOptionDefaultValue()
17+
.example('ghq clone ghq (requires `.gitconfig` has `github.user` or `user.name`)')
1618
.example('ghq clone 2nthony/ghq')
1719
.example('ghq clone github.com/2nthony/ghq')
1820
.example('ghq clone https://github.com/2nthony/ghq')
1921
.example('ghq get 2nthony/ghq')
2022
.allowUnknownOptions()
21-
.action(async (repo, options) => {
23+
.action(async (repo: string, options) => {
2224
if (!repo) {
2325
api.cli.outputHelp()
2426
return
2527
}
2628

29+
// ghq clone [repo]
30+
// means clone my repos?
31+
if (!repo.includes('/')) {
32+
const { user } = analyzeUrl(repo)
33+
if (!user) {
34+
api.cli.outputHelp()
35+
return
36+
}
37+
}
38+
2739
const config = await readConfig()
2840
const args = parseCliOptionsToGitArgs({ ...config, ...options })
2941

src/git.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ export async function init(repoUrl: string, ...args: string[]) {
3939
}
4040

4141
function getUsername() {
42-
try {
43-
const stdout = execSync('git config --get user.name')
44-
return stdout.toString().trim()
45-
}
46-
catch {
47-
return ''
42+
let username = ''
43+
function get(keypath: string) {
44+
try {
45+
return execSync(`git config --get ${keypath}`).toString().trim()
46+
}
47+
catch {
48+
return ''
49+
}
4850
}
51+
52+
username = get('github.user')
53+
if (!username)
54+
username = get('user.name')
55+
56+
return username
4957
}

0 commit comments

Comments
 (0)