Skip to content

Commit

Permalink
A new hope
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrián Quintás committed Apr 25, 2018
0 parents commit 499457b
Show file tree
Hide file tree
Showing 19 changed files with 851 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs: [
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
name: "default",
#
# These are the files included in the analysis:
files: %{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
included: ["lib/", "src/", "web/", "apps/"],
excluded: [~r"/_build/", ~r"/deps/"]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
requires: [],
#
# Credo automatically checks for updates, like e.g. Hex does.
# You can disable this behaviour below:
check_for_updates: true,
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
strict: true,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
color: true,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks: [
{Credo.Check.Readability.ModuleDoc},
{Credo.Check.Consistency.ExceptionNames},
{Credo.Check.Consistency.LineEndings},
{Credo.Check.Consistency.ParameterPatternMatching},
{Credo.Check.Consistency.SpaceAroundOperators},
{Credo.Check.Consistency.SpaceInParentheses},
{Credo.Check.Consistency.TabsOrSpaces},

# For some checks, like AliasUsage, you can only customize the priority
# Priority values are: `low, normal, high, higher`
{Credo.Check.Design.AliasUsage, priority: :low},

# For others you can set parameters

# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
{Credo.Check.Design.DuplicatedCode, excluded_macros: []},

# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
{Credo.Check.Design.TagTODO, exit_status: 2},
{Credo.Check.Design.TagFIXME},

{Credo.Check.Readability.FunctionNames},
{Credo.Check.Readability.LargeNumbers},
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 150},
{Credo.Check.Readability.ModuleAttributeNames},
{Credo.Check.Readability.ModuleNames},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs},
{Credo.Check.Readability.ParenthesesInCondition},
{Credo.Check.Readability.PredicateFunctionNames},
{Credo.Check.Readability.PreferImplicitTry},
{Credo.Check.Readability.RedundantBlankLines},
{Credo.Check.Readability.StringSigils},
{Credo.Check.Readability.TrailingBlankLine},
{Credo.Check.Readability.TrailingWhiteSpace},
{Credo.Check.Readability.VariableNames},
{Credo.Check.Readability.Semicolons},
{Credo.Check.Readability.SpaceAfterCommas},

{Credo.Check.Refactor.DoubleBooleanNegation},
{Credo.Check.Refactor.CondStatements},
{Credo.Check.Refactor.CyclomaticComplexity},
{Credo.Check.Refactor.FunctionArity},
{Credo.Check.Refactor.MatchInCondition},
{Credo.Check.Refactor.NegatedConditionsInUnless},
{Credo.Check.Refactor.NegatedConditionsWithElse},
{Credo.Check.Refactor.Nesting, max_nesting: 3},
{Credo.Check.Refactor.PipeChainStart},
{Credo.Check.Refactor.UnlessWithElse},

{Credo.Check.Warning.BoolOperationOnSameValues},
{Credo.Check.Warning.IExPry},
{Credo.Check.Warning.IoInspect},
{Credo.Check.Warning.LazyLogging},
{Credo.Check.Warning.OperationOnSameValues},
{Credo.Check.Warning.OperationWithConstantResult},
{Credo.Check.Warning.UnusedEnumOperation},
{Credo.Check.Warning.UnusedFileOperation},
{Credo.Check.Warning.UnusedKeywordOperation},
{Credo.Check.Warning.UnusedListOperation},
{Credo.Check.Warning.UnusedPathOperation},
{Credo.Check.Warning.UnusedRegexOperation},
{Credo.Check.Warning.UnusedStringOperation},
{Credo.Check.Warning.UnusedTupleOperation},

# Controversial and experimental checks (opt-in, just remove `, false`)
#
{Credo.Check.Refactor.ABCSize, false},
{Credo.Check.Refactor.AppendSingleItem, false},
{Credo.Check.Refactor.VariableRebinding, false},
{Credo.Check.Warning.MapGetUnsafePass, false},
{Credo.Check.Consistency.MultiAliasImportRequireUse, false},

# Deprecated checks (these will be deleted after a grace period)
{Credo.Check.Readability.Specs, false},
{Credo.Check.Warning.NameRedeclarationByAssignment, false},
{Credo.Check.Warning.NameRedeclarationByCase, false},
{Credo.Check.Warning.NameRedeclarationByDef, false},
{Credo.Check.Warning.NameRedeclarationByFn, false},

# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
git_hooks-*.tar

10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: elixir
elixir:
- 1.6
env:
- MIX_ENV=test
script: mix coveralls.travis
notifications:
email:
on_success: change
on_failure: always
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Adrián Quintás

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[![Coverage Status](https://coveralls.io/repos/github/qgadrian/elixir_git_hooks/badge.svg?branch=master)](https://coveralls.io/github/qgadrian/elixir_git_hooks?branch=master)
[![Hex version](https://img.shields.io/hexpm/v/sippet.svg "Hex version")](https://hex.pm/packages/git_hooks)
[![Hex Docs](https://img.shields.io/badge/hex-docs-9768d1.svg)](https://hexdocs.pm/elixir_git_hooks)
[![Build Status](https://travis-ci.org/qgadrian/metadata_plugs.svg?branch=master)](https://travis-ci.org/qgadrian/elixir_git_hooks.svg?branch=master)
[![Deps Status](https://beta.hexfaktor.org/badge/all/github/qgadrian/elixir_git_hooks.svg)](https://beta.hexfaktor.org/github/qgadrian/elixir_git_hooks)

# GitHooks

Installs [git hooks](https://git-scm.com/docs/githooks) that will run in Elixir project.

## Table of Contents

- [Installation](#installation)
- [Backup](#backup-current-hooks)
- [Automatic](#automatic-installation)
- [Manual](#manual-installation)
- [Configuration](#configuration)
- [Execution](#execution)
- [Automatic](#automatic-execution)
- [Manual](#manual-execution)

## Installation

Add to dependencies:

```elixir
def deps do
[{:git_hooks, "~> 0.1.0"}]
end
```

The install the dependencies:

```bash
mix deps.get
```

### Backup current hooks

This project will backup your current git hooks files copying the files and adding the extension `.pre_git_hooks_backup`.

### Automatic installation

This library will install automatically the configured git hooks in your file.

### Manual installation

You can install manually the configured git hooks by running:

```bash
mix git_hooks.install
```

## Configuration

One or more git hooks can be configured, those git hooks will be the ones [installed](#installation) for your project.

Currently there are supported two configuration options:
* **mix_tasks**: A list of the mix tasks that will run for the git hook
* **verbose**: The output of the mix tasks will be visible. This can be configured globally or per git hook.

```elixir
config :git_hooks,
verbose: true,
git_hooks: [
pre_commit: [
mix_tasks: [
"format"
]
],
pre_push: [
verbose: false,
mix_tasks: [
"dialyzer",
"test"
]
]
]
```

## Execution

### Automatic installation

The git hooks will run automatically for each [git step](https://git-scm.com/docs/githooks#_hooks).

### Manual installation

You can run manually any configured git hook as well.

For example, to run the pre_commit configuration:

```bash
mix git_hooks.run pre_commit
```
49 changes: 49 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure your application as:
#
# config :git_hooks, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:git_hooks, :key)
#
# You can also configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"

### Example configurations

config :git_hooks,
git_hooks: [
pre_commit: [
verbose: true,
mix_tasks: [
"format --check-formatted --dry-run",
"credo"
]
],
pre_push: [
verbose: true,
mix_tasks: [
"dialyzer"
]
]
]
50 changes: 50 additions & 0 deletions lib/config/config.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule GitHooks.Config do
@moduledoc false

@supported_hooks [
:pre_commit,
:pre_push,
:pre_rebase,
:pre_receive,
:pre_applypatch,
:post_update
]

@spec supported_hooks() :: list(atom())
def supported_hooks, do: @supported_hooks

@spec git_hooks() :: list(atom())
def git_hooks do
:git_hooks
|> Application.get_env(:git_hooks, [])
|> Keyword.take(@supported_hooks)
|> Keyword.keys()
end

@spec mix_tasks(atom()) :: list(String.t())
def mix_tasks(git_hook_type) do
:git_hooks
|> Application.get_env(:git_hooks, [])
|> Keyword.get(git_hook_type, [])
|> Keyword.get(:mix_tasks, [])
end

@spec verbose?(atom()) :: boolean()
def verbose?(git_hook_type) do
:git_hooks
|> Application.get_env(:git_hooks, [])
|> Keyword.get(git_hook_type, [])
|> Keyword.get(:verbose, Application.get_env(:git_hooks, :verbose, false))
end

@spec io_stream(atom()) :: any()
def io_stream(git_hook_type) do
case verbose?(git_hook_type) do
true ->
IO.stream(:stdio, :line)

_ ->
""
end
end
end
Loading

0 comments on commit 499457b

Please sign in to comment.