Last active
June 12, 2020 17:51
-
-
Save b2ox/2cec7e3c5a72ff7e8f9ee16aad1e4d58 to your computer and use it in GitHub Desktop.
git呼び出しライブラリ for deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: deno run --allow-run git-mgft.ts from to | |
// fromをtoにマージする | |
import * as git from './git.ts' | |
async function main(): Promise<number> { | |
if (Deno.args.length < 2) { | |
console.error('Usage: git-mgft from to'); | |
return -1 | |
} | |
const [branchFrom, branchTo] = Deno.args | |
const current = await git.currentBranch() | |
if (current === null) { | |
console.error('not a git repository') | |
return -1 | |
} | |
const info = await git.branches() | |
if (info.indexOf(branchFrom) < 0 || info.indexOf(branchTo) < 0) { | |
console.error('no branch: ' + branchTo) | |
return -1 | |
} | |
console.log(`git merge: ${branchFrom} -> ${branchTo}`) | |
await git.checkout(branchTo) | |
await git.merge(branchFrom) | |
await git.checkout(current) | |
return 0 | |
} | |
Deno.exit(await main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: deno run --allow-run git-wip.ts | |
// 現在のブランチがworkなら wip/YYYYMMDD_hh ブランチを作成し移動 | |
// wip/~ ブランチならworkブランチにマージ | |
// | |
// Script install: deno install --allow-run ~/denoscripts/git-wip.ts | |
// 上記でインストールすると ~/.deno/bin に ~/denoscripts/git-wip.ts を呼び出すスクリプトが作られる | |
// ~/denoscripts/git-wip.ts自体は~/.deno/binにコピーされない | |
import * as git from './git.ts' | |
async function main(): Promise<number> { | |
const work = 'work' | |
const twoDigits = (n: number): string => (n > 9 ? '' : '0') + n | |
const current = await git.currentBranch() | |
if (current === null) { | |
console.error('not a git repository') | |
return -1 | |
} else if (current === work) { | |
const now = new Date() | |
const wip = `wip/${now.getFullYear()}${twoDigits(now.getMonth() + 1)}${twoDigits(now.getDate())}_${twoDigits(now.getHours())}` | |
if (await git.checkoutBranch(wip)) { | |
console.log(`branching "${wip}"`) | |
} else { | |
console.error(`do not branching "${wip}"`) | |
return -1 | |
} | |
} else if (current.startsWith('wip/')) { | |
if (!(await git.branches()).includes(work)) { | |
console.error('no exists "work" branch') | |
return -1 | |
} else { | |
console.log(`${current} is merging to work`) | |
await git.checkout(work) | |
await git.merge(current) | |
await git.deleteBranch(current) | |
} | |
} else { | |
console.error('not a work or wip branch') | |
return -1 | |
} | |
return 0 | |
} | |
Deno.exit(await main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function branches(): Promise<string[]> { | |
const ret = await runCmd(['git', 'rev-parse', '--abbrev-ref', '--branches']) | |
if (ret === null) return [] | |
return ret.split(/\r\n|\n/) | |
} | |
export const currentBranch = () => runCmd(['git', 'rev-parse', '--abbrev-ref', 'HEAD']) | |
export const checkoutBranch = async (branch: string) => null !== await runCmd(['git', 'checkout', '-b', branch]) | |
export const checkout = async (branch: string) => null !== await runCmd(['git', 'checkout', branch]) | |
export const merge = async (branch: string) => null !== await runCmd(['git', 'merge', '--no-ff', branch]) | |
export const deleteBranch = async (branch: string) => null !== await runCmd(['git', 'branch', '-D', branch]) | |
async function runCmd(cmds: string[]): Promise<string | null> { | |
const p = Deno.run({ cmd: cmds, stdout: 'piped', stderr: 'piped' }) | |
const { code } = await p.status() | |
return code !== 0 ? null : (new TextDecoder().decode(await p.output())).trim() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
git-*.tsはgit.tsの呼び出しサンプル