Introduction

Basic idea

Based on the definition proposed by freecodecamp:

The .gitignore file is a text file that tells Git which files or folders to ignore in a project. A local .gitignore file is usually placed in the root directory of a project. You can also create a global .gitignore file and any entries in that file will be ignored in all of your Git repositories.

For any project, it is therefore important to have a .gitignore file that is complete and accurate. The package gitignore provides a simple R interface to the gitignore.io API. It can be used to fetch gitignore templates that can be included into the .gitignore file of you git repository. The gitignore R package can be used with R package, R Studio project or with any .gitignore file. Note that by default, the usethis package populates the .gitignore for the R language when you create a R project. However, it is common to use many different programming languages in a project such as LaTeX, python, matlab, julia and so on. This is where the gitignore package shines as it can be used to programmatically modify the .gitignore file of your project.

gitignore is a simple R package that provide an interface to query gitignore.io to fetch gitignore templates that can be included in the .gitignore file. More than 450 templates are currently available. There are actually two functions in the package:

Examples

library(gitignore)

Get the list of all available templates from gitignore.io:

head(gi_available_templates(), 50)
#>  [1] "1c"                   "1c-bitrix"            "a-frame"             
#>  [4] "al"                   "aspnetcore"           "ate"                 
#>  [7] "ats"                  "awr"                  "actionscript"        
#> [10] "ada"                  "adobe"                "advancedinstaller"   
#> [13] "adventuregamestudio"  "agda"                 "alteraquartusii"     
#> [16] "altium"               "amplify"              "android"             
#> [19] "androidstudio"        "angular"              "anjuta"              
#> [22] "ansible"              "ansibletower"         "apachecordova"       
#> [25] "apachehadoop"         "appbuilder"           "appcode"             
#> [28] "appcode+all"          "appcode+iml"          "appengine"           
#> [31] "appceleratortitanium" "aptanastudio"         "arcanist"            
#> [34] "archlinuxpackages"    "archive"              "archives"            
#> [37] "assembler"            "atmelstudio"          "audio"               
#> [40] "automationstudio"     "autotools"            "autotools+strict"    
#> [43] "azurefunctions"       "azurite"              "backup"              
#> [46] "ballerina"            "basic"                "batch"               
#> [49] "bazaar"               "bazel"

The function gi_fetch_templates() can be used to fetch particular template(s). For example, one could want to get the java and c++ templates as follow:

gi_fetch_templates(c("java", "c++"))
#> 
#> # Created by https://www.toptal.com/developers/gitignore/api/java,c++
#> # Edit at https://www.toptal.com/developers/gitignore?templates=java,c++
#> 
#> ### C++ ###
#> # Prerequisites
#> *.d
#> 
#> # Compiled Object files
#> *.slo
#> *.lo
#> *.o
#> *.obj
#> 
#> # Precompiled Headers
#> *.gch
#> *.pch
#> 
#> # Compiled Dynamic libraries
#> *.so
#> *.dylib
#> *.dll
#> 
#> # Fortran module files
#> *.mod
#> *.smod
#> 
#> # Compiled Static libraries
#> *.lai
#> *.la
#> *.a
#> *.lib
#> 
#> # Executables
#> *.exe
#> *.out
#> *.app
#> 
#> ### Java ###
#> # Compiled class file
#> *.class
#> 
#> # Log file
#> *.log
#> 
#> # BlueJ files
#> *.ctxt
#> 
#> # Mobile Tools for Java (J2ME)
#> .mtj.tmp/
#> 
#> # Package Files #
#> *.jar
#> *.war
#> *.nar
#> *.ear
#> *.zip
#> *.tar.gz
#> *.rar
#> 
#> # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
#> hs_err_pid*
#> 
#> # End of https://www.toptal.com/developers/gitignore/api/java,c++

By default, the template(s) are not copied into the clipboard, this can be changed by setting copy_to_clipboard = TRUE:

gi_fetch_templates(c("java", "c++"), copy_to_clipboard = TRUE)

Additionally, you can tell gi_fetch_templates() to append automatically the .gitignore file in your project by setting append_gitignore = TRUE:

gi_fetch_templates(c("R"), append_gitignore = TRUE)

It is also possible to specify the .gitignore file to be modified by specifying the gitignore_file argument.

f <- file.path(tempdir(), ".gitignore")

file.create(f)
#> [1] TRUE

gi_fetch_templates("R", gitignore_file = f, append_gitignore = TRUE)
#> 
#> # Created by https://www.toptal.com/developers/gitignore/api/r
#> # Edit at https://www.toptal.com/developers/gitignore?templates=r
#> 
#> ### R ###
#> # History files
#> .Rhistory
#> .Rapp.history
#> 
#> # Session Data files
#> .RData
#> 
#> # User-specific files
#> .Ruserdata
#> 
#> # Example code in package build process
#> *-Ex.R
#> 
#> # Output files from R CMD build
#> /*.tar.gz
#> 
#> # Output files from R CMD check
#> /*.Rcheck/
#> 
#> # RStudio files
#> .Rproj.user/
#> 
#> # produced vignettes
#> vignettes/*.html
#> vignettes/*.pdf
#> 
#> # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
#> .httr-oauth
#> 
#> # knitr and R markdown default cache directories
#> *_cache/
#> /cache/
#> 
#> # Temporary files created by R markdown
#> *.utf8.md
#> *.knit.md
#> 
#> # R Environment Variables
#> .Renviron
#> 
#> # pkgdown site
#> docs/
#> 
#> # translation temp files
#> po/*~
#> 
#> ### R.Bookdown Stack ###
#> # R package: bookdown caching files
#> /*_files/
#> 
#> # End of https://www.toptal.com/developers/gitignore/api/r
#> ● .gitignore file successfully modified.
  
readLines(f)
#>  [1] ""                                                                    
#>  [2] "# Created by https://www.toptal.com/developers/gitignore/api/r"      
#>  [3] "# Edit at https://www.toptal.com/developers/gitignore?templates=r"   
#>  [4] "### R ###"                                                           
#>  [5] "# History files"                                                     
#>  [6] ".Rhistory"                                                           
#>  [7] ".Rapp.history"                                                       
#>  [8] "# Session Data files"                                                
#>  [9] ".RData"                                                              
#> [10] "# User-specific files"                                               
#> [11] ".Ruserdata"                                                          
#> [12] "# Example code in package build process"                             
#> [13] "*-Ex.R"                                                              
#> [14] "# Output files from R CMD build"                                     
#> [15] "/*.tar.gz"                                                           
#> [16] "# Output files from R CMD check"                                     
#> [17] "/*.Rcheck/"                                                          
#> [18] "# RStudio files"                                                     
#> [19] ".Rproj.user/"                                                        
#> [20] "# produced vignettes"                                                
#> [21] "vignettes/*.html"                                                    
#> [22] "vignettes/*.pdf"                                                     
#> [23] "# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3"
#> [24] ".httr-oauth"                                                         
#> [25] "# knitr and R markdown default cache directories"                    
#> [26] "*_cache/"                                                            
#> [27] "/cache/"                                                             
#> [28] "# Temporary files created by R markdown"                             
#> [29] "*.utf8.md"                                                           
#> [30] "*.knit.md"                                                           
#> [31] "# R Environment Variables"                                           
#> [32] ".Renviron"                                                           
#> [33] "# pkgdown site"                                                      
#> [34] "docs/"                                                               
#> [35] "# translation temp files"                                            
#> [36] "po/*~"                                                               
#> [37] "### R.Bookdown Stack ###"                                            
#> [38] "# R package: bookdown caching files"                                 
#> [39] "/*_files/"                                                           
#> [40] "# End of https://www.toptal.com/developers/gitignore/api/r"