Pro Yearly is on sale from $80 to $50! »

YAPC::Asia 2014: Writing tools with Git

9b741203feda475cbeae8b384de9f415?s=47 motemen
August 29, 2014

YAPC::Asia 2014: Writing tools with Git

9b741203feda475cbeae8b384de9f415?s=128

motemen

August 29, 2014
Tweet

Transcript

  1. Git ʹΑΔπʔϧ։ൃ YAPC::Asia 2014 motemen 1

  2. motemen • GitHub: motemen • Twitter: motemen • Go/Vim/Git/Perl 2

  3. ஫ҙ Git ʹΑΔʮπʔϧ։ൃʯͰ͸ͳ͘
 ʮGit ʹΑΔπʔϧʯ։ൃͰ͢ 3

  4. ͭͬͨ͘΋ͷ • ghq • git-browse-remote • git-pr-release • github-commit-status-mark •

    git-unify 4
  5. ghq • Go • ͍͍ײ͡ʹ git clone / hg clone

    • ghq get <URL> • ghq look <repo> ghqΛ࢖ͬͨϩʔΧϧϦϙδτϦͷ౷Ұతɾޮ཰తͳ؅ཧʹ͍ͭͯ ghq + peco/percol 5
  6. git-browse-remote • Ruby • ͍͍ײ͡ʹϒϥ΢βͰ։͘ • hub browse ΑΓͪΐͬͱݡ͍ 6

  7. git-pr-release • Ruby • GitHub ͷ Pull Request Λ·ͱΊΔ 7

  8. github-commit-status-mark • Go • CI ௨͔ͬͨͲ͏͔ͻͱ໨ͰΘ͔Δ 8

  9. git-unify • sh • ෳ਺ͷϩʔΧϧΫϩʔϯͷ .git ҎԼ Λڞ༗ git-hsm: git

    submodule ͰαϒϞδϡʔϧ؅ཧͯ͠ΔͱσΟ εΫ༰ྔΛѹഭͯ͠ٽ͖ͨ͘ͳΓ·ͤΜ͔ 9
  10. એ఻ऴΘΓ 10

  11. ڞ௨఺ 11

  12. ࢖ͬͯͳ͍ • .git/** • libgit2 12

  13. git-* • git help -a • > 150 commands •

    ͜ΕΒΛར༻͢Δ 13
  14. Detailed Topics 14

  15. ҉໧ͷҾ਺ • Ϣʔβ͕Ծఆͯ͠Δ΋ͷ • ϦϙδτϦ • HEADʢϒϥϯνʣ • ex. git

    push (push.default=simple) 15
  16. Ҿ਺ͷղऍ • git browse-remote [<rev>] • git browse-remote feature/cool •

    git browse-remote b08d29 • git browse-remote v0.2-pre1~1 • git browse-remote @{3} 16
  17. Git <rev> • object (= tag/commit/tree/blob) Λࢦ͢ͳʹ͔ • SHA1 •

    ໊લ • ΄͔ • ૿͑Δ 17
  18. <rev> ͋Ε͜Ε • SHA1: 3b3ab428ec05924e9a96d16a05959150485f51a0, … • refname: “A symbolic

    ref name”, master, v2.0, HEAD… • λά/ϒϥϯν/ϦϞʔτ • “master” → “refs/heads/master” • “v2.0” → “refs/tags/v2.0” • “origin” → “refs/remotes/origin/HEAD” • ࣮ଶ͸ .git/ ҎԼ 18
  19. <rev> ͋Ε͜Ε • <rev>^{n} • <rev>~{n} • [<refname>]@{yesterday}, @{3} •

    describeOutput: v0.5-3-g0829bd • :/<text> • …and more! git-rev-parse: SPECIFYING REVISIONS 19
  20. DWIM!! • Ϣʔβ͕ԿΛݴ͍͍ͨͷ͔ • ϒϥϯνɾλάɾίϛοτ etc… • git symbolic-ref, git

    rev-parse, 
 git name-rev 20
  21. git-browse-remote • ϒϥϯν΍λά • → github.com/.../tree/some-branch • ͦ͏Ͱͳ͍ίϛοτ • →

    github.com/.../commit/ bd08d29 21
  22. gbr: Ҿ਺ • Ҿ਺ͳ͠ • ϒϥϯν΍λά • ίϛοτ • ͦͷଞʢϑΝΠϧͱ͔ʣ

    22
  23. gbr: Ҿ਺ͳ͠ • ҉໧ͷ HEAD • ϒϥϯν or λά or

    ίϛοτ • git symbolic-ref -q HEAD 23
  24. git symbolic-ref • ݩ࢝ɺ.git/HEAD ͸ symlink • → .git/refs/heads/** •

    ࠓ: “ref: <refname>” ͱ͔ • or SHA1 24
  25. gbr: Ҿ਺͋Γ • git name-rev —name-only • rev ʹγϯϘϦοΫͳ໊લΛ༩͑Δ •

    “5d7d3957” → “remotes/origin/master” • “HEAD~3” → “tags/v0.4-pre" • git rev-parse —symbolic-full-name • refname Λ׬શ໊ʹ • “v0.4” → “refs/tags/v0.4” 25
  26. “git foo-bar” • git-foo-bar Ͱఏڙ • git --exec-path ʹύε͕௨Δ 26

  27. git --exec-path • ex: /usr/local/Cellar/git/2.1.0/libexec/git-core • Git ͷαϒίϚϯυͷๅݿ • ࣮ߦϏοτཱ͕ͬͯͳ͍΋ͷ΋


    → γΣϧεΫϦϓτ༻ 27
  28. #!/bin/sh • “. git-sh-setup” ͔Βελʔτ • ศརؔ਺ͨͪ • Α͠ͳʹηοτΞοϓ 28

  29. git-sh-setup • NONGIT_OK • SUBDIRECTORY_OK • OPTIONS_SPEC 29

  30. OPTIONS_SPEC OPTS_SPEC="\! some-command [options] <args>...! ! some-command does foo and

    bar!! --! h,help show the help! ! foo some nifty option --foo! bar= some cool option --bar with an argument! baz=arg another cool option --baz with a named argument! qux?path qux may take a path argument but has meaning by itself! ! An option group Header! C? option C with an optional argument"! 30 git-rev-parse: PARSEOPT
  31. OPTIONS_SPEC OPTS_SPEC="\! some-command [options] <args>...! ! some-command does foo and

    bar!! --! h,help show the help! ! foo some nifty option --foo! bar= some cool option --bar with an argument! baz=arg another cool option --baz with a named argument! qux?path qux may take a path argument but has meaning by itself! ! An option group Header! C? option C with an optional argument"! 31 git-rev-parse: PARSEOPT
  32. ύε • ϑΝΠϧͷϧʔτ͔Βͷύε • .git σΟϨΫτϦͷҐஔ • ࢥͬͨ৔ॴʹͳ͍͜ͱ΋ (cf. submodule)

    32
  33. git rev-parse • git rev-parse --show-toplevel • τοϓσΟϨΫτϦͷઈରύε • git

    rev-parse --git-dir • git rev-parse --show-prefix • cwd ͔Βͷ૬ରύε 33
  34. git rev-parse ͍͢͝ 34

  35. GitHub • API • octokit / go-github • GitHub:Enterprise ରԠ

    • → ϗετ͕͋Β͔͡Ί෼͔Βͳ͍ 35
  36. GH:E ϦϞʔτΛऔಘ 1. remote.origin.url 2. ݱࡏͷϒϥϯνʹରԠ͢ΔϦϞʔ τͷ URLʢݫີʹ΍Γ͍ͨਓʣ 36

  37. Get ϦϞʔτ • $branch = `git symbolic-ref -q HEAD` •

    $remote = `git config branch.$branch.remote` • $url = `git config remote.$remote.url` 37
  38. ͞Βʹ • git config url.<newURL>.insteadOf <oldURL> • oldURL Λ push/fetch

    ͢Δ୅ΘΓʹ newURL ʹ 38
  39. url.<url>.insteadOf • git config url.git@github.com:motemen/.insteadOf https://github.com/motemen/ • https://github.com/motemen/* ΁ͷΞΫηεΛ git@github.com:motemen/*

    ʹॻ͖׵͑ 39
  40. Get ϦϞʔτ: ݁࿦ • git remote show -n <remote> ͯ͠

    ਖ਼نදݱͰͳΜͱ͔͢Δ 40
  41. ϦϞʔτ URL • user@host.xz:path/to/repo.git/ • “SCP-like URL” • proto://host.xz[:port]/path/to/repo.git/ docs/git-fetch.html#URLS

    • git fetch-pack --diag-url 41
  42. ઃఆ • YAML/JSON • git config 42

  43. git config • Human readable/writable • KVS • ͋͘·Ͱݸਓઃఆ •

    .git/config • ~/.gitconfig 43
  44. git config -f • git config -f <file> • ϓϩδΣΫτݻ༗ͷઃఆΛڞ༗

    • cf. .git-pr-release 44
  45. git config --get-urlmatch • URL ͝ͱʹઃఆΛมߋͰ͖Δ • git config --get-urlmatch


    ghq.vcs https://example.com/git/ # => “git” 45 [ghq]! vcs = unknown! [ghq "https://example.com/git/"]! vcs = git! [ghq "https://example.com/hg/"]! vcs = hg
  46. ͪ͜Β͔Β͸Ҏ্Ͱ͢ 46

  47. ͓ΘΓ • ศརͳ Git αϒίϚϯυୡ • symbolic-ref / name-rev /

    rev-parse / git-sh-setup / config / remote • …and more • ׆༻͠·͠ΐ͏ • --help -w 47