---
title: "Create a gitbook with git_down"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ab-create-git_down}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

<script type="text/javascript">
$(document).ready(function () {
  window.initializeCodeFolding("show" === "show");
});
</script>


```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup, include=FALSE}
library(gitdown)
library(git2r)
library(dplyr)
```


## Create a reproducible example of a versioned directory

Create a versioned directory with some commits and a NEWS.md in a temporary directory

- Some commits mention an issue with `#`
- Some commits mention a ticket with `ticket`
- A commit is associated with a tag

```{r}
repo <- fake_repo()
```

## Build book of commits messages

The main function of {gitdown} is to build this gitbook with all commit messages ordered according to a pattern. Each commit message associated with an issue will be recorded in the section of this issue. A commit message can thus appears multiple times if it is associated with multiple issues.  
If you have your own referencing system for tickets in an external software, you can also create the gitbook associated like using `ticket` as in the example below.

```{r, eval = FALSE}
git_down(repo, pattern = c("Tickets" = "ticket[[:digit:]]+", "Issues" = "#[[:digit:]]+"))
```


## Read list of commits and extract information

As a side effect of {gitdown}, you can get some intermediate information used to build the book with some exported functions.

### Get commits and find pattern

- Find all commits of a branch and associate with tags recursively

```{r}
get_commits_tags(repo, ref = "main")
```

- Find all commits of a branch and detect a specific pattern
    + Here we find commits mentioning an issue with `#123`
    
```{r}
get_commits_pattern(repo, pattern = "#[[:digit:]]+", ref = "main")
```

## Create a vignette that lists all files with date of modification

```{r, eval=FALSE}
repo_pkg <- fake_repo(as.package = TRUE)
# List only files in R/ directory
create_vignette_last_modif(repo_pkg)
# List all files of the git repository
create_vignette_last_modif(repo_pkg, path = "")
```

With this example, the vignette will show this content:  

```{r, echo=FALSE, results='asis'}
repo_pkg <- fake_repo(as.package = TRUE)
cat(present_files(repo_pkg, path = ""))
```
