Upgrade to Pro — share decks privately, control downloads, hide ads and more …

YAPC::Asia 2014: Writing tools with Git

motemen
August 29, 2014

YAPC::Asia 2014: Writing tools with Git

motemen

August 29, 2014
Tweet

More Decks by motemen

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    ʮGit ʹΑΔπʔϧʯ։ൃͰ͢
    3

    View Slide

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

    View Slide

  5. ghq
    • Go
    • ͍͍ײ͡ʹ git clone / hg clone
    • ghq get
    • ghq look
    ghqΛ࢖ͬͨϩʔΧϧϦϙδτϦͷ౷Ұతɾޮ཰తͳ؅ཧʹ͍ͭͯ
    ghq + peco/percol
    5

    View Slide

  6. git-browse-remote
    • Ruby
    • ͍͍ײ͡ʹϒϥ΢βͰ։͘
    • hub browse ΑΓͪΐͬͱݡ͍
    6

    View Slide

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

    View Slide

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

    View Slide

  9. git-unify
    • sh
    • ෳ਺ͷϩʔΧϧΫϩʔϯͷ .git ҎԼ
    Λڞ༗
    git-hsm: git submodule ͰαϒϞδϡʔϧ؅ཧͯ͠ΔͱσΟ
    εΫ༰ྔΛѹഭͯ͠ٽ͖ͨ͘ͳΓ·ͤΜ͔
    9

    View Slide

  10. એ఻ऴΘΓ
    10

    View Slide

  11. ڞ௨఺
    11

    View Slide

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

    View Slide

  13. git-*
    • git help -a
    • > 150 commands
    • ͜ΕΒΛར༻͢Δ
    13

    View Slide

  14. Detailed Topics
    14

    View Slide

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

    View Slide

  16. Ҿ਺ͷղऍ
    • git browse-remote []
    • git browse-remote feature/cool
    • git browse-remote b08d29
    • git browse-remote v0.2-pre1~1
    • git browse-remote @{3}
    16

    View Slide

  17. Git
    • object (= tag/commit/tree/blob) Λࢦ͢ͳʹ͔
    • SHA1
    • ໊લ
    • ΄͔
    • ૿͑Δ
    17

    View Slide

  18. ͋Ε͜Ε
    • 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

    View Slide

  19. ͋Ε͜Ε
    • ^{n}
    • ~{n}
    • []@{yesterday}, @{3}
    • describeOutput: v0.5-3-g0829bd
    • :/
    • …and more!
    git-rev-parse: SPECIFYING REVISIONS
    19

    View Slide

  20. DWIM!!
    • Ϣʔβ͕ԿΛݴ͍͍ͨͷ͔
    • ϒϥϯνɾλάɾίϛοτ etc…
    • git symbolic-ref, git rev-parse, 

    git name-rev
    20

    View Slide

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

    View Slide

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

    View Slide

  23. gbr: Ҿ਺ͳ͠
    • ҉໧ͷ HEAD
    • ϒϥϯν or λά or ίϛοτ
    • git symbolic-ref -q HEAD
    23

    View Slide

  24. git symbolic-ref
    • ݩ࢝ɺ.git/HEAD ͸ symlink
    • → .git/refs/heads/**
    • ࠓ: “ref: ” ͱ͔
    • or SHA1
    24

    View Slide

  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

    View Slide

  26. “git foo-bar”
    • git-foo-bar Ͱఏڙ
    • git --exec-path ʹύε͕௨Δ
    26

    View Slide

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

    → γΣϧεΫϦϓτ༻
    27

    View Slide

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

    View Slide

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

    View Slide

  30. OPTIONS_SPEC
    OPTS_SPEC="\!
    some-command [options] ...!
    !
    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

    View Slide

  31. OPTIONS_SPEC
    OPTS_SPEC="\!
    some-command [options] ...!
    !
    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

    View Slide

  32. ύε
    • ϑΝΠϧͷϧʔτ͔Βͷύε
    • .git σΟϨΫτϦͷҐஔ
    • ࢥͬͨ৔ॴʹͳ͍͜ͱ΋ (cf.
    submodule)
    32

    View Slide

  33. git rev-parse
    • git rev-parse --show-toplevel
    • τοϓσΟϨΫτϦͷઈରύε
    • git rev-parse --git-dir
    • git rev-parse --show-prefix
    • cwd ͔Βͷ૬ରύε
    33

    View Slide

  34. git rev-parse ͍͢͝
    34

    View Slide

  35. GitHub
    • API
    • octokit / go-github
    • GitHub:Enterprise ରԠ
    • → ϗετ͕͋Β͔͡Ί෼͔Βͳ͍
    35

    View Slide

  36. GH:E ϦϞʔτΛऔಘ
    1. remote.origin.url
    2. ݱࡏͷϒϥϯνʹରԠ͢ΔϦϞʔ
    τͷ URLʢݫີʹ΍Γ͍ͨਓʣ
    36

    View Slide

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

    View Slide

  38. ͞Βʹ
    • git config url..insteadOf
    • oldURL Λ push/fetch ͢Δ୅ΘΓʹ
    newURL ʹ
    38

    View Slide

  39. url..insteadOf
    • git config
    [email protected]:motemen/.insteadOf
    https://github.com/motemen/
    • https://github.com/motemen/* ΁ͷΞΫηεΛ
    [email protected]:motemen/* ʹॻ͖׵͑
    39

    View Slide

  40. Get ϦϞʔτ: ݁࿦
    • git remote show -n ͯ͠
    ਖ਼نදݱͰͳΜͱ͔͢Δ
    40

    View Slide

  41. ϦϞʔτ URL
    [email protected]: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

    View Slide

  42. ઃఆ
    • YAML/JSON
    • git config
    42

    View Slide

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

    View Slide

  44. git config -f
    • git config -f <file>
    • ϓϩδΣΫτݻ༗ͷઃఆΛڞ༗
    • cf. .git-pr-release
    44

    View Slide

  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

    View Slide

  46. ͪ͜Β͔Β͸Ҏ্Ͱ͢
    46

    View Slide

  47. ͓ΘΓ
    • ศརͳ Git αϒίϚϯυୡ
    • symbolic-ref / name-rev / rev-parse / git-sh-setup / config
    / remote
    • …and more
    • ׆༻͠·͠ΐ͏
    • --help -w
    47

    View Slide