Skip to content

lorenzwalthert/precommit

Repository files navigation

<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>General • precommit</title><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="bootstrap-toc.css"><script src="bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="pkgdown.css" rel="stylesheet"><script src="pkgdown.js"></script><!-- docsearch --><script src="docsearch.js"></script><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.3/docsearch.min.css" integrity="sha256-QOSRU/ra9ActyXkIBbiIB144aDBdtvXBcNc3OTNuX/Q=" crossorigin="anonymous"><link href="docsearch.css" rel="stylesheet"><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.min.js" integrity="sha256-4HLtjeVgH0eIB3aZ9mLYF6E8oU5chNdjU6p6rrXpl9U=" crossorigin="anonymous"></script><meta property="og:title" content="General"><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--></head><body data-spy="scroll" data-target="#toc">


    <div class="container template-title-body">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="index.html">precommit</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.4.3</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav"><li>
  <a href="articles/precommit.html">Get started</a>
</li>
<li>
  <a href="reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Articles

    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu"><li>
      <a href="articles/why-use-hooks.html">Why is this useful?</a>
    </li>
    <li>
      <a href="articles/available-hooks.html">Available Hooks</a>
    </li>
    <li>
      <a href="articles/ci.html">Continuous Integration</a>
    </li>
    <li>
      <a href="articles/FAQ.html">FAQ</a>
    </li>
    <li class="divider">
    <li>
      <a href="articles/index.html">More articles...</a>
    </li>
  </ul></li>
<li>
  <a href="news/index.html">Changelog</a>
</li>
      </ul><ul class="nav navbar-nav navbar-right"><li>
  <a href="https://github.com/lorenzwalthert/precommit/" class="external-link">
    <span class="fab fa-github fa-lg"></span>

  </a>
</li>
      </ul><form class="navbar-form navbar-right hidden-xs hidden-sm" role="search">
        <div class="form-group">
          <input type="search" class="form-control" name="search-input" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off"></div>
      </form>

    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->



      </header><div class="row">
  <div class="contents col-md-9">
    <div class="page-header">
      <h1>General</h1>
    </div>


<div id="general" class="section level1">

<p>Before making a pull request, discuss your ideas in an issue. # Adding new hooks</p>
<p>To create a new hook, have a look at the <a href="https://pre-commit.com/#new-hooks" class="external-link">official documentation</a> on creating new hooks, then have a look at existing hooks in this repo. The actual executables are defined in <a href="https://github.com/lorenzwalthert/precommit/tree/main/inst/hooks" class="external-link"><code>inst/hooks/</code></a>. In the script, you can expect the passed command line arguments to be all options, finally the files that should be processed with the hook.</p>
<p>For the scripts to become a hook, they need to be <em>registered</em> in <a href="https://github.com/lorenzwalthert/precommit/blob/main/.pre-commit-hooks.yaml" class="external-link"><code>.pre-commit-hooks.yaml</code></a>. As of pre-commit 2.11, R is a <a href="https://pre-commit.com/#r" class="external-link">supported language of pre-commit</a>. Hence, it should have <code>language: r</code> in <code>.pre-commit-hooks.yaml</code> and then (for compatibility) a shebang in the <code>entrypoint</code> script.</p>
</div>
<div class="section level1">
<h1 id="testing-hooks">Testing hooks<a class="anchor" aria-label="anchor" href="#testing-hooks"></a></h1>
<p>Hooks should be tested by checking both the positive outcome (hook passes) and the negative outcome (hook fails) by adding two <code><a href="reference/run_test.html">run_test()</a></code> statements to <a href="https://github.com/lorenzwalthert/precommit/blob/main/tests/testthat/test-hooks.R" class="external-link"><code>./tests/testthat/test-hooks.R</code></a>. Look at existing examples and <a href="https://lorenzwalthert.github.io/precommit/reference/run_test.html">the documentation of <code>run_test()</code></a>. Note that this won’t interact with pre-commit. It will simply run <code>Rscript path/to/script.R</code> (whereas with pre-commit, a {renv} will be activated before running the script).</p>
<p>Also, there are <a href="https://github.com/lorenzwalthert/precommit/blob/main/.github/workflows/end-to-end.yml" class="external-link">tests</a> to ensure that hooks are correctly registered in <code>.pre-commit-hooks.yaml</code>, which you have to adapt if you add a hook.</p>
<p>You can also test them with <code>pre-commit try-repo</code> as described in the <a href="https://pre-commit.com/#pre-commit-try-repo" class="external-link">documentation</a>.</p>
</div>
<div class="section level1">
<h1 id="summary">Summary<a class="anchor" aria-label="anchor" href="#summary"></a></h1>
<ul><li><p>add your R (with extension) script in <code>inst/hooks/exported</code> and make it executable. See other scripts in <code>inst/hooks/exported</code> for a starting point for setting up your script.</p></li>
<li><p>register hook in <code>.pre-commit-hooks.yaml</code>.</p></li>
<li><p>add two unit tests, test manually with <code>pre-commit try-repo</code> and adapt the <a href="https://github.com/lorenzwalthert/precommit/blob/main/.github/workflows/end-to-end.yml" class="external-link">end-to-end test</a>.</p></li>
<li><p>add a description of the new hook to <code>vignettes/available-hooks.Rmd</code>. Both description and <code>yaml</code> example code.</p></li>
</ul></div>


  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top"><h2 data-toc-skip>Contents</h2>
    </nav></div>

</div>



      <footer><div class="copyright">
  <p></p><p>Developed by <a href="https://lorenzwalthert.com" class="external-link">Lorenz Walthert</a>.</p>
</div>

<div class="pkgdown">
  <p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.1.0.</p>
</div>

      </footer></div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.1/docsearch.min.js" integrity="sha256-GKvGqXDznoRYHCwKXGnuchvKSwmx9SRMrZOTh2g4Sb0=" crossorigin="anonymous"></script><script>
  docsearch({


    apiKey: 'ae5363c86f48f4117b4f2b9beaefa28f',
    indexName: 'lorenzwalthert_precommit',
    inputSelector: 'input#search-input.form-control',
    transformData: function(hits) {
      return hits.map(function (hit) {
        hit.url = updateHitURL(hit);
        return hit;
      });
    }
  });
</script></body></html>

About

pre-commit hooks for R projects

Topics

License

Contributing

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 35

Languages