-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-cc
More file actions
executable file
·71 lines (61 loc) · 2.29 KB
/
git-cc
File metadata and controls
executable file
·71 lines (61 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
// Checkout or create a branch
const { execSync, exec } = require('child_process');
try {
const lines = execSync("git branch --all --format '%(refname:short) %(refname)'").toString().trim().split('\n')
const localBranches = lines.flatMap(line => {
const [short, ref] = line.split(' ');
if (ref.startsWith('refs/remotes/')) {
return []
} else {
return [short]
}
})
const remoteBranches = lines.flatMap(line => {
const [short, ref] = line.split(' ');
if (ref.startsWith('refs/remotes/') && !ref.endsWith('/HEAD')) {
const [, local] = short.split(/\/(.*)/)
if (localBranches.includes(local)) {
return []
} else {
return short
}
} else {
return []
}
})
const branches = localBranches.concat(remoteBranches)
exec(`echo "${branches.join('\n')}" | fzf --print-query`, (err, stdout, stderr) => {
if (stdout.length > 0) {
let [query, selectedBranch] = stdout.toString().split('\n').map(x => x.trim())
if (selectedBranch != null && selectedBranch !== '') {
if (localBranches.includes(selectedBranch)) {
execSync(`git checkout ${selectedBranch}`)
} else if (remoteBranches.includes(selectedBranch)) {
const [remote, local] = selectedBranch.split(/\/(.*)/)
execSync(`git checkout -b ${local} ${remote}/${local}`)
}
} else {
const prompt = `# Output branch name
- Output the appropriate branch name for the following changes.
- If the change content can be used as is for the branch name, simply convert it according to the general branch naming rules.
- Make branch names concise, descriptive, and distinctive.
- If the branch name is too generic and may conflict with future work, add a timestamp (YYYYMMDDhhmm) to the end of the name.
- Only respond with the message and no affirmation
## Changes
${query.replace('ai:', '')}
`
const branch = execSync(`claude --model sonnet -p "${prompt}"`).toString().trim()
execSync(`git checkout -b "${branch.trim()}"`)
}
}
})
} catch(e) {
if ('status' in e && 'stdout' in e && 'stderr' in e) {
console.error('status:', e.status)
console.error('stdout:' + e.stdout.toString() + 'stderr:', e.stderr.toString())
} else {
console.error(e)
}
process.exit(1)
}