@drsimonj here to help you embark on git repo analyses!
Ever wondered who contributes to git repos? How their contributions have changed over time? What sort of conventions different authors use in their commit messages? Maybe you were inspired by Mara Averick to contribute to tidyverse packages and wonder how you fit in?
This post – intended for intermediate R users – will help you answer these sorts of questions using tidy R tools.
Install and load these packages to follow along:
# Parts 1 and 2
library(tidyverse)
library(glue)
library(stringr)
library(forcats)
# Part 3
library(tidygraph)
library(ggraph)
library(tidytext)
 Part 1: Git repo to a tidy data frame
 Get a git repo
We’ll explore the open-source ggplot2 repo by copying it to our local machine with git clone, typically run on a command-line like:
git clone Find the for github.com projects by clicking “Clone or download”.
is optional but useful for us to clone into a specific location.
The R code below clones the ggplot2 git repo into a temporary directory called &quot;git_repo&quot; (let Alexander Matrunich teach you more about temp directories and files here). system() invokes these commands from R (instead of using a command-line directly) and the glue package beautifully handles the strings for us.
# Remote repository URL
repo_url [7] &quot;data-raw&quot; &quot;DESCRIPTION&quot; &quot;ggplot2.Rproj&quot; #__ [10] &quot;icons&quot; &quot;inst&quot; &quot;ISSUE_TEMPLATE.md&quot;
#__ [13] &quot;LICENSE&quot; &quot;man&quot; &quot;NAMESPACE&quot; #__ [16] &quot;NEWS&quot; &quot;NEWS.md&quot; &quot;R&quot; #__ [19] &quot;README.md&quot; &quot;README.Rmd&quot; &quot;revdep&quot; #__ [22] &quot;tests&quot; &quot;vignettes&quot;
 Get tidy git history
You can now access the git history of the local repo using git log (making sure to target the right directory with -C). Examine the last few commits via:
system(glue('git -C {clone_dir} log -3'))
#__ commit 3c9c504fdb2f2c6b70a98d5a02f6cdf2c8cc62c9
#__ Merge: cab8a54e 449bc039
#__ Author: Lionel Henry #__ Date: Thu Mar 22 18:42:25 2018 +0100
#__ #__ Merge pull request #2491 from tidyverse/tidyeval-facets
#__ #__ Port facets to tidy eval
#__ #__ commit 449bc039a75dd06e0ff52233c5d747f81b4d7c30
#__ Author: Lionel Henry #__ Date: Thu Mar 22 17:54:12 2018 +0100
#__ #__ Remove dependency on plyr::as.quoted()
#__ #__ commit dc3c9855fd18f78ba8ef14d7e485334ac47e3b16
#__ Author: Lionel Henry #__ Date: Thu Mar 22 16:39:12 2018 +0100
#__ #__ Call formula interface classic instead of historical
This default output is nice but difficult to parse. Fortunately, git log has the --pretty option, which the code below uses to create a command to return nicely formatted logs (learn more about log formatting here):
log_format_options 2018-03-22 17:55:23 dc3c9855 1182b9f3 Lionel Henry Call formula interface classic instead of historical
The R code below executes this for the entire repo, captures the output (via intern = TRUE), splits commit strings into vectors of values (thanks to stringr), and converts them to a named tibble.
history_logs % str_split_fixed(option_delim, length(log_format_options)) %__% as_tibble() %__% setNames(names(log_format_options))
history_logs
#__ # A tibble: 3,946 x 5
#__ datetime commit parents author subject #__ #__ 1 2018-03-22 18:42:25 3c9c504f cab8a54e 449bc039 Lionel Henry Merge pull…
#__ 2 2018-03-22 17:55:23 449bc039 dc3c9855 Lionel Henry Remove dep…
#__ 3 2018-03-22 17:55:23 dc3c9855 1182b9f3 Lionel Henry Call formu…
#__ 4 2018-03-22 17:55:23 1182b9f3 b2144a8e Lionel Henry Document t…
#__ 5 2018-03-22 17:55:23 b2144a8e 13db76d2 Lionel Henry Mention ti…
#__ 6 2018-03-22 17:55:23 13db76d2 95253c66 Lionel Henry Accept var…
#__ 7 2018-03-22 17:55:23 95253c66 2983aa0d Lionel Henry Accept var…
#__ 8 2018-03-22 17:55:23 2983aa0d 64a00c7d Lionel Henry Rename as_…
#__ 9 2018-03-22 17:55:23 64a00c7d f186615a Lionel Henry Use proper…
#__ 10 2018-03-22 17:55:23 f186615a cab8a54e Lionel Henry Support qu…
#__ # ... with 3,936 more rows
The entire git commit history is now a tidy data frame (tibble)! We’ll finish this section with two minor additions.
First, the parents column can contain space-separated strings when a commit was a merge of multiple, for example. The code below converts this to a list-column of character vectors (let Jenny Bryan teach you more about this here):
history_logs % mutate(parents = str_split(parents, &quot; &quot;))
history_logs
#__ # A tibble: 3,946 x 5
#__ datetime commit parents author subject #__ #__ 1 2018-03-22 18:42:25 3c9c504f Lionel Henry Merge pull request…
#__ 2 2018-03-22 17:55:23 449bc039 Lionel Henry Remove dependency …
#__ 3 2018-03-22 17:55:23 dc3c9855 Lionel Henry Call formula inter…
#__ 4 2018-03-22 17:55:23 1182b9f3 Lionel Henry Document tidy eval…
#__ 5 2018-03-22 17:55:23 b2144a8e Lionel Henry Mention tidy eval …
#__ 6 2018-03-22 17:55:23 13db76d2 Lionel Henry Accept vars() spec…
#__ 7 2018-03-22 17:55:23 95253c66 Lionel Henry Accept vars() spec…
#__ 8 2018-03-22 17:55:23 2983aa0d Lionel Henry Rename as_facets_s…
#__ 9 2018-03-22 17:55:23 64a00c7d Lionel Henry Use proper quosure…
#__ 10 2018-03-22 17:55:23 f186615a Lionel Henry Support quosures i…
#__ # ... with 3,936 more rows
Finally, be sure to assign branch numbers to commits. There’s surely a better way to do this, but here’s one (very untidy) method:
# Start with NA
history_logs % mutate(branch = NA_integer_)
# Create a boolean vector to represent free columns (1000 should be plenty!)
free_col 9 2018-03-22 17:55:23 64a00c7d Lionel Henry Use proper … 2
#__ 10 2018-03-22 17:55:23 f186615a Lionel Henry Support quo… 2
#__ # ... with 3,936 more rows
This rounds off the section on getting a git commit history into a tidy data frame. Remove the local git repo, which is no longer needed:
unlink(clone_dir, recursive = TRUE)
 Part 2: Tidy Analysis
You’re now ready to embark on a tidy git repo analysis! For example, which authors make the most commits?
history_logs %__% count(author, sort = TRUE)
#__ # A tibble: 165 x 2
#__ author n
#__ #__ 1 hadley 2285
#__ 2 Winston Chang 650
#__ 3 Hadley Wickham 104
#__ 4 Kohske Takahashi @ jurina 103
#__ 5 Kohske Takahashi at Haruna 70
#__ 6 hadley wickham 69
#__ 7 Jean-Olivier Irisson 63
#__ 8 Lionel Henry 50
#__ 9 Thomas Lin Pedersen 47
#__ 10 Kirill Müller 40
#__ # ... with 155 more rows
Some authors appear under different names, which we can quickly correct based on these top cases:
history_logs % mutate(author = case_when(
str_detect(tolower(author), &quot;hadley&quot;) ~ &quot;Hadley Wickham&quot;,
str_detect(tolower(author), &quot;kohske takahashi&quot;) ~ &quot;Kohske Takahashi&quot;,
TRUE ~ str_to_title(author)
))
Now, again, authors by commit frequency:
history_logs %__% count(author) %__% arrange(desc(n))
#__ # A tibble: 157 x 2
#__ author n
#__ #__ 1 Hadley Wickham 2458
#__ 2 Winston Chang 650
#__ 3 Kohske Takahashi 232
#__ 4 Jean-Olivier Irisson 63
#__ 5 Lionel Henry 50
#__ 6 Thomas Lin Pedersen 47
#__ 7 Kirill Müller 40
#__ 8 Kara Woo 36
#__ 9 Brian Diggs 27
#__ 10 Jake Russ 20
#__ # ... with 147 more rows
And top-ten visualized with ggplot2:
history_logs %__% count(author) %__% top_n(10, n) %__% mutate(author = fct_reorder(author, n)) %__% ggplot(aes(author, n)) +
geom_col(aes(fill = n), show.legend = FALSE) +
coord_flip() +
theme_minimal() +
ggtitle(&quot;ggplot2 authors with most commits&quot;) +
labs(x = NULL, y = &quot;Number of commits&quot;, caption = &quot;Post by @drsimonj&quot;)
Not surprising to see Hadley Wikham topping the charts. Otherwise, the analysis options are pretty endless from here!
 Part 3: Advanced Topics
I’d like to touch on two advanced topics before leaving you to embark on astounding git repo analyses.
 Git repo as a relational graph
A git history is a relational structure where commits are nodes and connections between them are directed edges (from parent to child).
The code below converts our tidy data frame into a tidy relational structure made up of two data frames (nodes and edges) thanks to tidygraph (learn more from the package creator, Thomas Lin Pedersen, in blog posts like this).
# Convert commit to a factor (for ordering nodes)
history_logs % mutate(commit = factor(commit))
# Nodes are the commits (keeping relevant info)
nodes % select(-parents) %__% arrange(commit)
# Edges are connections between commits and their parents
edges % select(commit, parents) %__% unnest(parents) %__% mutate(parents = factor(parents, levels = levels(commit))) %__% transmute(from = as.integer(parents), to = as.integer(commit)) %__% drop_na()
# Create tidy directed graph object
git_graph # A tbl_graph: 3946 nodes and 4371 edges
#__ #
#__ # A directed acyclic simple graph with 1 component
#__ #
#__ # Node Data: 3,946 x 5 (active)
#__ datetime commit author subject branch
#__ #__ 1 2011-07-19 22:40:33 002e510d Kohske Takahashi remove style param… 4
#__ 2 2008-07-05 20:00:50 0036ad7f Hadley Wickham HAcks to get tile … 1
#__ 3 2010-02-05 10:25:26 0072cf4c Hadley Wickham Fix bug with empty… 1
#__ 4 2013-12-02 13:51:05 008018cc Josef Fruehwald added stat_ellipse 9
#__ 5 2010-12-23 10:22:36 00821913 Hadley Wickham Fix doc name 1
#__ 6 2011-12-23 06:24:28 008312c0 Hadley Wickham Merge pull request… 1
#__ # ... with 3,940 more rows
#__ #
#__ # Edge Data: 4,371 x 2
#__ from to
#__ #__ 1 3070 968
#__ 2 1071 968
#__ 3 3370 1071
#__ # ... with 4,368 more rows
Using ggraph (another of Thomas’ awesome packages with more detailed info in posts like this) a default visualization could look something like this:
git_graph %__% ggraph() +
geom_edge_link(alpha = .1) +
geom_node_point(aes(color = factor(branch)), alpha = .3) +
theme_graph() +
theme(legend.position = &quot;none&quot;)
#__ Using `nicely` as default layout
This looks cool but not right! A &quot;manual&quot; layout is needed for a linear visualisation of the git history (see the dplyr network for example).
For convenience, this is a template pipeline that will take the tidy graph object, ensure the proper layout is used, and create the basic plot:
ggraph_git %
# Set node x,y coordinates
activate(nodes) %__% mutate(x = datetime, y = branch) %__% # Plot with correct layout
create_layout(layout = &quot;manual&quot;, node.positions = as_tibble(activate(., nodes))) %__% {ggraph(., layout = &quot;manual&quot;) + theme_graph() + labs(caption = &quot;Post by @drsimonj&quot;)}
Using this pipeline:
git_graph %__% ggraph_git() +
geom_edge_link(alpha = .1) +
geom_node_point(aes(color = factor(branch)), alpha = .3) +
theme(legend.position = &quot;none&quot;) +
ggtitle(&quot;Commit history of ggplot2&quot;)
Much better! You can go crazy with how you’d like to visualise the repo. For example, here we filter on a specific date range:
git_graph %__% activate(nodes) %__% filter(datetime __ &quot;2015-11-01&quot;, datetime __ &quot;2016-08-01&quot;) %__% ggraph_git() +
geom_edge_link(alpha = .1) +
geom_node_point(aes(color = factor(branch)), alpha = .3) +
theme(legend.position = &quot;none&quot;) +
ggtitle(&quot;Git history of ggplot2&quot;,
subtitle = &quot;2015-11 to 2016-08&quot;)
Here commits are highlighted for the top authors:
# 10 most-common authors
top_authors % activate(nodes) %__% as_tibble() %__% count(author, sort = TRUE) %__% top_n(10, n) %__% pull(author)
# Plot
git_graph %__% activate(nodes) %__%
filter(datetime __ &quot;2015-11-01&quot;, datetime __ &quot;2016-08-01&quot;) %__% mutate(author = factor(author, levels = top_authors),
author = fct_explicit_na(author, na_level = &quot;Other&quot;)) %__% ggraph_git() +
geom_edge_link(alpha = .1) +
geom_node_point(aes(color = author), alpha = .3) +
theme(legend.position = &quot;bottom&quot;) +
ggtitle(&quot;ggplot2 commits by author&quot;,
subtitle = &quot;2015-11 to 2016-08&quot;)
I hope this gives you enough to start having fun with these sorts of visualisations!
 Text Mining Commit Messages
Commit messages are simple but great material for tidy text-mining tools like the brilliant tidytext package, best learned from Text Mining with R: A Tidy Approach, by Julia Silge and David Robinson. Here are some examples using commit subjects to get you started.
Get commit subjects into a tidy format and remove stop words.
data(stop_words)
tidy_subjects %
unnest_tokens(word, subject) %__% anti_join(stop_words)
#__ Joining, by = &quot;word&quot;
tidy_subjects
#__ # A tibble: 16,477 x 6
#__ datetime commit parents author branch word #__ #__ 1 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 merge #__ 2 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 pull #__ 3 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 request #__ 4 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 2491 #__ 5 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 tidyverse #__ 6 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 tidyeval #__ 7 2018-03-22 18:42:25 3c9c504f Lionel Henry 1 facets #__ 8 2018-03-22 17:55:23 449bc039 Lionel Henry 2 remove #__ 9 2018-03-22 17:55:23 449bc039 Lionel Henry 2 dependency
#__ 10 2018-03-22 17:55:23 449bc039 Lionel Henry 2 plyr #__ # ... with 16,467 more rows
What are the ten most frequently used words?
tidy_subjects %__%
count(word) %__% top_n(10, n) %__% mutate(word = fct_reorder(word, n)) %__% ggplot(aes(word, n)) +
geom_col(aes(fill = n), show.legend = FALSE) +
coord_flip() +
theme_minimal() +
ggtitle(&quot;Most-used words in ggplot2 commit subjects&quot;) +
labs(x = NULL, y = &quot;Word frequency&quot;, caption = &quot;Post by @drsimonj&quot;)
Or how about the words that most-frequently follow “fix”, the most-used word:
history_logs %__% select(commit, author, subject) %__% unnest_tokens(bigram, subject, token = &quot;ngrams&quot;, n = 2) %__% separate(bigram, c(&quot;word1&quot;, &quot;word2&quot;), sep = &quot; &quot;) %__% filter(word1 == &quot;fix&quot;) %__%
anti_join(stop_words, by = c(&quot;word2&quot; = &quot;word&quot;)) %__% count(word2, sort = TRUE)
#__ # A tibble: 223 x 2
#__ word2 n
#__ #__ 1 bug 57
#__ 2 typo 37
#__ 3 geom 11
#__ 4 guides 11
#__ 5 bugs 7
#__ 6 doc 7
#__ 7 boxplot 6
#__ 8 axis 5
#__ 9 documentation 5
#__ 10 dumb 5
#__ # ... with 213 more rows
Unsurprisingly, it seems like many of the commits involve fixing bugs and typos, as well as challenges with geoms and guides.
We’ve now covered more than enough for you to explore and analyse git repos in a tidy R framework. Don’t forget to share your findings with the world and let me know about it!
 Sign off
Thanks for reading and I hope this was useful for you.
For updates of recent blog posts, follow @drsimonj on Twitter, or email me at drsimonjackson@gmail.com to get in touch.
If you’d like the code that produced this blog, check out the blogR GitHub repository.