Git 中文文档 Git 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 起步

    • 1.1 关于版本控制
    • 1.2 git 简史
    • 1.3 git 是什么
    • 1.4 命令行
    • 1.5 安装 git
    • 1.6 初次运行git前的配置
    • 1.7 获取帮助
    • 1.8 起步 - 总结
  • git 基础

  • git 分支

  • 服务器上的 git

  • 分布式 git

  • github

  • git 工具

  • 自定义 git

  • git 与其他系统

  • git 内部原理

Hercules


Fast, insightful and highly customizable Git history analysis.

Overview •   How To Use •   Installation •   Contributions •   License

Table of Contents


Overview
Installation
Build from source
GitHub Action

Contributions
License
Usage
Caching
GitHub Action
Docker image
Built-in analyses
Project burndown
Files
People
Churn matrix
Code ownership
Couples
Structural hotness
Aligned commit series
Added vs changed lines through time
Efforts through time
Sentiment (positive and negative comments)
Everything in a single pass

Plugins
Merging
Bad unicode errors
Plotting
Custom plotting backend
Caveats
Burndown Out-Of-Memory

Roadmap

Overview


Hercules is an amazingly fast and highly customizable Git repository analysis engine written in Go. Batteries are included. Powered by go-git.

Notice (November 2020): the main author is back from the limbo and is gradually resuming the development. See the roadmap.

There are two command-line tools: hercules and labours. The first is a program written in Go which takes a Git repository and executes a Directed Acyclic Graph (DAG) of analysis tasks over the full commit history. The second is a Python script which shows some predefined plots over the collected data. These two tools are normally used together through a pipe. It is possible to write custom analyses using the plugin system. It is also possible to merge several analysis results together - relevant for organizations. The analyzed commit history includes branches, merges, etc.

Hercules has been successfully used for several internal projects at source{d}. There are blog posts: 1, 2 and a presentation. Please contribute by testing, fixing bugs, adding new analyses, or coding swagger!

Hercules DAG of Burndown analysis

The DAG of burndown and couples analyses with UAST diff refining. Generated with hercules --burndown --burndown-people --couples --feature=uast --dry-run --dump-dag doc/dag.dot https://github.com/src-d/hercules

git/git image

torvalds/linux line burndown (granularity 30, sampling 30, resampled by year). Generated with hercules --burndown --first-parent --pb https://github.com/torvalds/linux | labours -f pb -m burndown-project in 1h 40min.

Installation


Grab hercules binary from the Releases page. labours is installable from PyPi :

  1. ``` sh
  2. pip3 install labours

  3. ```

pip3 is the Python package manager.

Numpy and Scipy can be installed on Windows using http://www.lfd.uci.edu/~gohlke/pythonlibs/

Build from source


You are going to need Go (>= v1.11) and protoc.

  1. ``` sh
  2. git clone https://github.com/src-d/hercules && cd hercules
  3. make
  4. pip3 install -e ./python

  5. ```

GitHub Action


It is possible to run Hercules as a GitHub Action : Hercules on GitHub Marketplace. Please refer to the sample workflow which demonstrates how to setup.

Contributions


...are welcome! See CONTRIBUTING and code of conduct.

License


Apache 2.0

Usage


The most useful and reliably up-to-date command line reference:

  1. ``` sh
  2. hercules --help

  3. ```

Some examples:

  1. ``` sh
  2. # Use "memory" go-git backend and display the burndown plot. "memory" is the fastest but the repository's git data must fit into RAM.
  3. hercules --burndown https://github.com/go-git/go-git | labours -m burndown-project --resample month
  4. # Use "file system" go-git backend and print some basic information about the repository.
  5. hercules /path/to/cloned/go-git
  6. # Use "file system" go-git backend, cache the cloned repository to /tmp/repo-cache, use Protocol Buffers and display the burndown plot without resampling.
  7. hercules --burndown --pb https://github.com/git/git /tmp/repo-cache | labours -m burndown-project -f pb --resample raw

  8. # Now something fun
  9. # Get the linear history from git rev-list, reverse it
  10. # Pipe to hercules, produce burndown snapshots for every 30 days grouped by 30 days
  11. # Save the raw data to cache.yaml, so that later is possible to labours -i cache.yaml
  12. # Pipe the raw data to labours, set text font size to 16pt, use Agg matplotlib backend and save the plot to output.png
  13. git rev-list HEAD | tac | hercules --commits - --burndown https://github.com/git/git | tee cache.yaml | labours -m burndown-project --font-size 16 --backend Agg --output git.png

  14. ```

labours -i /path/to/yaml allows to read the output from hercules which was saved on disk.

Caching


It is possible to store the cloned repository on disk. The subsequent analysis can run on the corresponding directory instead of cloning from scratch:

  1. ``` sh
  2. # First time - cache
  3. hercules https://github.com/git/git /tmp/repo-cache

  4. # Second time - use the cache
  5. hercules --some-analysis /tmp/repo-cache

  6. ```

GitHub Action


The action produces the artifact named hercules_charts. Since it is currently impossible to pack several files in one artifact, all the charts and Tensorflow Projector files are packed in the inner tar archive. In order to view the embeddings, go to projector.tensorflow.org, click "Load" and choose the two TSVs. Then use UMAP or T-SNE.

Docker image


  1. ``` sh
  2. docker run --rm srcd/hercules hercules --burndown --pb https://github.com/git/git | docker run --rm -i -v $(pwd):/io srcd/hercules labours -f pb -m burndown-project -o /io/git_git.png

  3. ```

Built-in analyses


Project burndown


  1. ``` sh
  2. hercules --burndown
  3. labours -m burndown-project

  4. ```

Line burndown statistics for the whole repository. Exactly the same what git-of-theseus does but much faster. Blaming is performed efficiently and incrementally using a custom RB tree tracking algorithm, and only the last modification date is recorded while running the analysis.

All burndown analyses depend on the values of granularityand sampling. Granularity is the number of days each band in the stack consists of. Sampling is the frequency with which the burnout state is snapshotted. The smaller the value, the more smooth is the plot but the more work is done.

There is an option to resample the bands inside labours, so that you can define a very precise distribution and visualize it different ways. Besides, resampling aligns the bands across periodic boundaries, e.g. months or years. Unresampled bands are apparently not aligned and start from the project's birth date.

Files


  1. ``` sh
  2. hercules --burndown --burndown-files
  3. labours -m burndown-file

  4. ```

Burndown statistics for every file in the repository which is alive in the latest revision.

Note: it will generate separate graph for every file. You don't want to run it on repository with many files.

People


  1. ``` sh
  2. hercules --burndown --burndown-people [--people-dict=/path/to/identities]
  3. labours -m burndown-person

  4. ```

Burndown statistics for the repository's contributors. If --people-dict is not specified, the identities are discovered by the following algorithm:

We start from the root commit towards the HEAD. Emails and names are converted to lower case.
If we process an unknown email and name, record them as a new developer.
If we process a known email but unknown name, match to the developer with the matching email, and add the unknown name to the list of that developer's names.
If we process an unknown email but known name, match to the developer with the matching name, and add the unknown email to the list of that developer's emails.

If --people-dict is specified, it should point to a text file with the custom identities. The format is: every line is a single developer, it contains all the matching emails and names separated by |. The case is ignored.

Overwrites matrix


Wireshark top 20 overwrites matrix

Wireshark top 20 devs - overwrites matrix

  1. ``` sh
  2. hercules --burndown --burndown-people [--people-dict=/path/to/identities]
  3. labours -m overwrites-matrix

  4. ```

Beside the burndown information, --burndown-people collects the added and deleted line statistics per developer. Thus it can be visualized how many lines written by developer A are removed by developer B. This indicates collaboration between people and defines expertise teams.

The format is the matrix with N rows and (N+2) columns, where N is the number of developers.

First column is the number of lines the developer wrote.
Second column is how many lines were written by the developer and deleted by unidentified developers (if --people-dict is not specified, it is always 0).
The rest of the columns show how many lines were written by the developer and deleted by identified developers.

The sequence of developers is stored in people_sequence YAML node.

Code ownership


Ember.js top 20 code ownership

Ember.js top 20 devs - code ownership

  1. ``` sh
  2. hercules --burndown --burndown-people [--people-dict=/path/to/identities]
  3. labours -m ownership

  4. ```

--burndown-people also allows to draw the code share through time stacked area plot. That is, how many lines are alive at the sampled moments in time for each identified developer.

Couples


Linux kernel file couples

torvalds/linux files' coupling in Tensorflow Projector

  1. ``` sh
  2. hercules --couples [--people-dict=/path/to/identities]
  3. labours -m couples -o <name> [--couples-tmp-dir=/tmp]

  4. ```

Important: it requires Tensorflow to be installed, please follow official instructions.

The files are coupled if they are changed in the same commit. The developers are coupled if they change the same file. hercules records the number of couples throughout the whole commit history and outputs the two corresponding co-occurrence matrices. labours then trains Swivel embeddings - dense vectors which reflect the co-occurrence probability through the Euclidean distance. The training requires a working Tensorflow installation. The intermediate files are stored in the system temporary directory or --couples-tmp-dir if it is specified. The trained embeddings are written to the current working directory with the name depending on -o. The output format is TSV and matches Tensorflow Projector so that the files and people can be visualized with t-SNE implemented in TF Projector.

Structural hotness


  1. ``` sh
  2.       46  jinja2/compiler.py:visit_Template [FunctionDef]
  3.       42  jinja2/compiler.py:visit_For [FunctionDef]
  4.       34  jinja2/compiler.py:visit_Output [FunctionDef]
  5.       29  jinja2/environment.py:compile [FunctionDef]
  6.       27  jinja2/compiler.py:visit_Include [FunctionDef]
  7.       22  jinja2/compiler.py:visit_Macro [FunctionDef]
  8.       22  jinja2/compiler.py:visit_FromImport [FunctionDef]
  9.       21  jinja2/compiler.py:visit_Filter [FunctionDef]
  10.       21  jinja2/runtime.py:__call__ [FunctionDef]
  11.       20  jinja2/compiler.py:visit_Block [FunctionDef]

  12. ```

Thanks to Babelfish, hercules is able to measure how many times each structural unit has been modified. By default, it looks at functions; refer to Semantic UAST XPath manual to switch to something else.

  1. ``` sh
  2. hercules --shotness [--shotness-xpath-*]
  3. labours -m shotness

  4. ```

Couples analysis automatically loads "shotness" data if available.

Jinja2 functions grouped by structural hotness

hercules --shotness --pb https://github.com/pallets/jinja | labours -m couples -f pb

Aligned commit series


tensorflow/tensorflow

tensorflow/tensorflow aligned commit series of top 50 developers by commit number.

  1. ``` sh
  2. hercules --devs [--people-dict=/path/to/identities]
  3. labours -m devs -o <name>

  4. ```

We record how many commits made, as well as lines added, removed and changed per day for each developer. We plot the resulting commit time series using a few tricks to show the temporal grouping. In other words, two adjacent commit series should look similar after normalization.

We compute the distance matrix of the commit series. Our distance metric is Dynamic Time Warping. We use FastDTW algorithm which has linear complexity proportional to the length of time series. Thus the overall complexity of computing the matrix is quadratic.
We compile the linear list of commit series with Seriation technique. Particularly, we solve the Travelling Salesman Problem which is NP-complete. However, given the typical number of developers which is less than 1,000, there is a good chance that the solution does not take much time. We use Google or-tools solver.
We find 1-dimensional clusters in the resulting path with HDBSCAN algorithm and assign colors accordingly.
Time series are smoothed by convolving with the Slepian window.

This plot allows to discover how the development team evolved through time. It also shows "commit flashmobs" such as Hacktoberfest. For example, here are the revealed insights from the tensorflow/tensorflow plot above:

"Tensorflow Gardener" is classified as the only outlier.
The "blue" group of developers covers the global maintainers and a few people who left (at the top).
The "red" group shows how core developers join the project or become less active.

Added vs changed lines through time


tensorflow/tensorflow

tensorflow/tensorflow added and changed lines through time.

  1. ``` sh
  2. hercules --devs [--people-dict=/path/to/identities]
  3. labours -m old-vs-new -o <name>

  4. ```

--devs from the previous section allows to plot how many lines were added and how many existing changed (deleted or replaced) through time. This plot is smoothed.

Efforts through time


kubernetes/kubernetes

kubernetes/kubernetes efforts through time.

  1. ``` sh
  2. hercules --devs [--people-dict=/path/to/identities]
  3. labours -m devs-efforts -o <name>

  4. ```

Besides, --devs allows to plot how many lines have been changed (added or removed) by each developer. The upper part of the plot is an accumulated (integrated) lower part. It is impossible to have the same scale for both parts, so the lower values are scaled, and hence there are no lower Y axis ticks. There is a difference between the efforts plot and the ownership plot, although changing lines correlate with owning lines.

Sentiment (positive and negative comments)


Django sentiment

It can be clearly seen that Django comments were positive/optimistic in the beginning, but later became negative/pessimistic.`hercules --sentiment --
Last Updated: 2023-09-03 19:17:54