├── .gitignore ├── LICENSE ├── README.md ├── config ├── __init__.py ├── common.py ├── private.py └── util.py ├── env.py ├── gitstat.py ├── main.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Python template 3 | # Byte-compiled / optimized / DLL files 4 | __pycache__/ 5 | *.py[cod] 6 | *$py.class 7 | 8 | # C extensions 9 | *.so 10 | 11 | # Distribution / packaging 12 | .Python 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | .hypothesis/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | .static_storage/ 58 | .media/ 59 | local_settings.py 60 | 61 | # Flask stuff: 62 | instance/ 63 | .webassets-cache 64 | 65 | # Scrapy stuff: 66 | .scrapy 67 | 68 | # Sphinx documentation 69 | docs/_build/ 70 | 71 | # PyBuilder 72 | target/ 73 | 74 | # Jupyter Notebook 75 | .ipynb_checkpoints 76 | 77 | # pyenv 78 | .python-version 79 | 80 | # celery beat schedule file 81 | celerybeat-schedule 82 | 83 | # SageMath parsed files 84 | *.sage.py 85 | 86 | # Environments 87 | .env 88 | .venv 89 | env/ 90 | venv/ 91 | ENV/ 92 | env.bak/ 93 | venv.bak/ 94 | 95 | # Spyder project settings 96 | .spyderproject 97 | .spyproject 98 | 99 | # Rope project settings 100 | .ropeproject 101 | 102 | # mkdocs documentation 103 | /site 104 | 105 | # mypy 106 | .mypy_cache/ 107 | 108 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Henry Zhu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Git Repo代码贡献量分析脚本(Git Repository Mining) 2 | 3 | ### 说明 4 | 代码贡献量 != 项目真实贡献 5 | 但能不能从无意思的数据中, 去**挖掘**一些有趣有用的信息. 也就是说, 能不能用程序, 代替人去评估程序员. 6 | 7 | 比如: 8 | 9 | - insertion/deletion 10 | - 某个人写的代码被删的概率. 11 | - 被修改频率最高的文件 12 | - 等等 13 | 14 | (如果你想到好的指标或算法, 请直接留言issue, 谢谢!) 15 | 16 | --- 17 | 18 | ### Demo: 19 | ![](http://opetwnn9x.bkt.clouddn.com/git_contribution/Jietu20171022-210926.jpg) 20 | 21 | --- 22 | 23 | ### Features: 24 | 25 | 1. 快 (2500个commits的项目用时, 用时1.1s, 和gitinspector相比快了20倍.) 26 | 2. 统计一个用户所有的commits, insertion, deletion, 改动总比重. 27 | 3. 合并多个用户(不同用户名)--> 同一个用户名. 28 | 4. 支持按列排序. 29 | 30 | --- 31 | 32 | ### 使用方法: 33 | 34 | - **安装python依赖:** 35 | ``` 36 | git clone git@github.com:daya0576/git-code-contribution-analysis.git; cd git-code-contribution-analysis 37 | pip3 install -r requirements.txt 38 | ``` 39 | - **配置:**(可选) 40 | 在`env.py`中配置选项, e.g. 排序的列, 合并重复的author, .. 41 | - **运行:** 42 | ``` 43 | python3 main.py 44 | ``` 45 | 46 | --- 47 | 48 | ### TODO 49 | 50 | 1. 导出其他格式: html, cvs, excel, json 51 | 2. 多个repo, 合并分析 52 | 3. 兼容Python2 53 | 4. **其他指标, 算法, 更好地分析项目贡献, 而不仅仅是代码量的贡献.** 54 | 5. 程序的进度条 55 | 6. 将配置(env.py)放到参数中 56 | 7. **git log 中limitation能做到的都能加上, e.g. 时间范围等等** 57 | 8. ... 58 | 59 | --- 60 | (割) 61 | --- 62 | 63 | # A Script for **Code Contribution** Analysis   64 | 65 | ### Installation 66 | `tested in python3.6` 67 | ``` sh 68 | pip install -r requirements.txt 69 | ``` 70 | 71 | ### Usage 72 | 73 | 1. config repo path, [order column, ] in `env.py ` 74 | 2. `python ` 75 | 76 | -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | __author__ = "daya0576" 4 | __date__ = "2017-10-20 14:39" -------------------------------------------------------------------------------- /config/common.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | from env import ORDER_BY as _ORDER_BY 3 | 4 | COL_INSERTION = ' Insertion' 5 | COL_DELETION = ' Deletion' 6 | COL_COMMITS = ' Commits' 7 | COL_TOTAL = ' % of changes' 8 | 9 | 10 | order_d = dict( 11 | commits=COL_COMMITS, 12 | insertion=COL_INSERTION, 13 | deletion=COL_DELETION, 14 | total=COL_TOTAL 15 | ) 16 | ORDER_BY = order_d.get(_ORDER_BY, COL_COMMITS) 17 | 18 | COMMIT_FLAG = '%%' 19 | -------------------------------------------------------------------------------- /config/private.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import functools 3 | 4 | __author__ = "daya0576" 5 | __date__ = "2017-10-20 14:39" 6 | 7 | from line_profiler import LineProfiler 8 | 9 | class Line_Profiler(object): 10 | def __init__(self, follow=None): 11 | self.follow = follow or [] 12 | 13 | def __call__(self, func): 14 | def profiled_func(*args, **kwargs): 15 | line_profiler = LineProfiler() 16 | line_profiler.add_function(func) 17 | map(lambda x: line_profiler.add_function(x), self.follow) 18 | line_profiler.enable_by_count() 19 | result = func(*args, **kwargs) 20 | line_profiler.disable_by_count() 21 | line_profiler.print_stats(stripzeros=True) 22 | return result 23 | 24 | return functools.wraps(func)(profiled_func) 25 | 26 | 27 | profile = Line_Profiler() 28 | 29 | -------------------------------------------------------------------------------- /config/util.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import subprocess 3 | import sys 4 | PY3 = sys.version_info[0] >= 3 5 | 6 | if PY3: 7 | unicode = str 8 | else: 9 | unicode = unicode 10 | 11 | __author__ = "daya0576" 12 | __date__ = "2017-10-20 16:20" 13 | 14 | defenc = sys.getdefaultencoding() 15 | 16 | 17 | def safe_decode(s): 18 | """Safely decodes a binary string to unicode""" 19 | if isinstance(s, unicode): 20 | return s 21 | elif isinstance(s, bytes): 22 | return s.decode(defenc, 'surrogateescape') 23 | elif s is not None: 24 | raise TypeError('Expected bytes or text, but got %r' % (s,)) 25 | 26 | 27 | def execute(command, repo): 28 | if repo.path: 29 | command = ['git', '-C', repo.path] + command[1:] 30 | ls_output = subprocess.check_output(command) 31 | return ls_output -------------------------------------------------------------------------------- /env.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | REPO = '' 4 | 5 | AUTHOR_NAME_MAPPING = { 6 | # example: 7 | # 'daya0576': ['Daya', 'Henry'] --> 'Daya', 'Henry' will be replaced by daya0576 8 | } 9 | 10 | # CHOICES: commits, insertion, deletion, total 11 | ORDER_BY = 'commits' 12 | -------------------------------------------------------------------------------- /gitstat.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import print_function, unicode_literals 4 | 5 | import getopt 6 | import re 7 | import pandas as pd 8 | import sys 9 | 10 | from config.common import COL_TOTAL, COL_DELETION, COL_INSERTION, COL_COMMITS, ORDER_BY, COMMIT_FLAG 11 | from config.util import safe_decode, execute 12 | from env import AUTHOR_NAME_MAPPING, REPO 13 | 14 | __author__ = "daya0576" 15 | __date__ = "2017-10-20 11:25" 16 | 17 | branch = 'develop' 18 | 19 | 20 | class Repo(object): 21 | def __init__(self, path=''): 22 | self.path = path 23 | 24 | 25 | def get_authors(repo): 26 | """ fetch all authors""" 27 | command = ['git', 'log', '--format=%aN'] 28 | result = execute(command, repo) 29 | authors = set(result.splitlines()) 30 | authors = [safe_decode(x) for x in authors] 31 | 32 | return authors 33 | 34 | 35 | def get_stat_by(author, repo): 36 | """ sum of insertion/deletion/commits of author """ 37 | author_filter = '--committer=^{} '.format(author) 38 | 39 | command = ['git', 'log', author_filter, '--oneline', '--shortstat', '--pretty=%{}'.format(COMMIT_FLAG)] 40 | # print(' '.join(command)) 41 | # command = 'git log --oneline --shortstat --pretty='.split() 42 | result = execute(command, repo=repo) 43 | result = safe_decode(result) 44 | insertion = re.findall(r'(\d+) insertions', result) 45 | deletion = re.findall(r'(\d+) deletions', result) 46 | 47 | insertion_sum, deletion_sum = sum([int(x) for x in insertion]), sum([int(x) for x in deletion]) 48 | 49 | d = { 50 | COL_COMMITS: result.count(COMMIT_FLAG), 51 | COL_INSERTION: insertion_sum, 52 | COL_DELETION: deletion_sum 53 | } 54 | return {author: d} 55 | 56 | 57 | def get_dataframe(author_d): 58 | df = pd.DataFrame.from_dict(author_d, orient='index').reset_index() 59 | 60 | if AUTHOR_NAME_MAPPING: 61 | name_d = {x: k for k, v in AUTHOR_NAME_MAPPING.items() for x in v} 62 | df = df.replace({"index": name_d}) 63 | df = df.groupby('index', as_index=False).sum() 64 | 65 | return df 66 | 67 | 68 | def after(df): 69 | df.sort_values(COL_COMMITS, ascending=0) 70 | 71 | df_total = df.sum(numeric_only=True) 72 | df_total[COL_TOTAL] = '100%' 73 | total_changes = df_total[COL_INSERTION] + df_total[COL_DELETION] 74 | 75 | df[COL_TOTAL] = (df[COL_INSERTION] + df[COL_DELETION]) / total_changes 76 | df[COL_TOTAL] = df[COL_TOTAL].map('{:.2%}'.format) 77 | 78 | # Sort 79 | df = df.sort_values(ORDER_BY, ascending=0) 80 | 81 | # df = df.set_index('index') 82 | 83 | return df, df_total 84 | 85 | 86 | def pretty_print(df, df_total): 87 | # TODO: pretty print && export to other format 88 | # Add the row of total to the end 89 | df = df.append(df_total, ignore_index=True) 90 | df['index'].fillna('TOTAL', inplace=True) 91 | 92 | df.set_index('index', inplace=True) 93 | df.index.name = None 94 | 95 | print('\n\n{}'.format(df)) 96 | 97 | 98 | def main(): 99 | repo_path = sys.argv[-1] if len(sys.argv) > 2 else REPO 100 | if not repo_path: 101 | print('python3 main.py ') 102 | repo = Repo(path=repo_path) 103 | author_d = {} 104 | # print('Analyzing ') 105 | for author in get_authors(repo): 106 | # print(author, end=' ') 107 | author_d.update(get_stat_by(author, repo)) 108 | 109 | df = get_dataframe(author_d) 110 | df, df_total = after(df) 111 | 112 | pretty_print(df, df_total) 113 | 114 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | # -*- coding: utf-8 -*- 3 | 4 | from gitstat import main 5 | 6 | if __name__ == '__main__': 7 | main() 8 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pandas==0.20.3 2 | --------------------------------------------------------------------------------