github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/docs/Add-git-hooks.md (about)

     1  Summary
     2  -------
     3  `./godelw git-hooks` installs a [Git commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) that ensures
     4  that all of the files in a project are formatted using `./godelw format` before they are committed (requires the project
     5  to be a Git repository).
     6  
     7  Tutorial start state
     8  --------------------
     9  
    10  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
    11  * Project contains `godel` and `godelw`
    12  
    13  ([Link](https://github.com/nmiyake/echgo/tree/6a73370d5b9c8c32ce1a5218938c922f1218db30))
    14  
    15  Create Git commit hook
    16  ----------------------
    17  
    18  Install the Git hooks for gödel in the current project by running the following:
    19  
    20  ```
    21  ➜ ./godelw git-hooks
    22  ```
    23  
    24  With the repository initialized and hooks installed, we start writing code. Run the following to generate the initial
    25  version of a `main.go` file echoes the arguments provided by the user:
    26  
    27  ```
    28  ➜ echo 'package main
    29  import "fmt"
    30  import "strings"
    31  import "os"
    32  func main() {
    33  	fmt.Println(strings.Join(os.Args[1:], " "))
    34  }' > main.go
    35  ```
    36  
    37  This is valid Go that compiles and runs properly:
    38  
    39  ```
    40  ➜ go run main.go foo
    41  foo
    42  ```
    43  
    44  However, if we attempt to add and commit this file to the repository, it will fail:
    45  
    46  ```
    47  ➜ git add main.go
    48  ➜ git commit -m "Add main.go"
    49  Unformatted files exist -- run ./godelw format to format these files:
    50    /Volumes/git/go/src/github.com/nmiyake/echgo/main.go
    51  ```
    52  
    53  This is because the commit hook has determined that `main.go` is not formatted properly. We can run `./godelw format`
    54  (this is covered in more detail in the [Format](https://github.com/palantir/godel/wiki/Format) section of the tutorial)
    55  to format the file and then verify that adding and committing the file works:
    56  
    57  ```
    58  ➜ ./godelw format
    59  ➜ git add main.go
    60  ➜ git commit -m "Add main.go"
    61  [master 9e77ec4] Add main.go
    62   1 file changed, 11 insertions(+)
    63   create mode 100644 main.go
    64  ➜ git status
    65  On branch master
    66  nothing to commit, working directory clean
    67  ```
    68  
    69  We now have a repository that contains the first version of our `echgo` program and have a commit hook that ensures that
    70  all of the code we check in for our program will be properly formatted.
    71  
    72  Tutorial end state
    73  ------------------
    74  
    75  * `$GOPATH/src/github.com/nmiyake/echgo` exists and is the working directory
    76  * Project contains `godel` and `godelw`
    77  * Project contains `main.go`
    78  
    79  ([Link](https://github.com/nmiyake/echgo/tree/9e77ec4885591f5a4fd95b550da729a004e7a04a))
    80  
    81  Tutorial next step
    82  ------------------
    83  
    84  [Generate IDE project for Gogland](https://github.com/palantir/godel/wiki/Generate-IDE-project)
    85  
    86  More
    87  ----
    88  
    89  ### Hook installation
    90  
    91  Running `./godelw git-hooks` will overwrite the `.git/hooks/pre-commit` file (including any previous customizations).
    92  
    93  ### Uninstalling the hook
    94  
    95  The commit hook can be uninstalled by removing the generated commit hook file at `.git/hooks/pre-commit`.