├── .github └── workflows │ ├── package.yml │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── example.txt ├── gitink ├── __init__.py ├── cli.py ├── color.py ├── main.py ├── svg.py ├── test.py └── test │ ├── down.svg │ ├── down.txt │ ├── right.svg │ ├── right.txt │ ├── up.svg │ └── up.txt ├── img └── example.jpg ├── pyproject.toml └── requirements.txt /.github/workflows/package.yml: -------------------------------------------------------------------------------- 1 | name: Package 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | build: 9 | 10 | runs-on: ${{ matrix.os }} 11 | strategy: 12 | matrix: 13 | python-version: ["3.12"] 14 | os: [ubuntu-latest] 15 | 16 | steps: 17 | - name: Checkout 18 | uses: actions/checkout@v4 19 | - name: Set up Python ${{ matrix.python-version }} 20 | uses: actions/setup-python@v5 21 | with: 22 | python-version: ${{ matrix.python-version }} 23 | - name: Install Python dependencies 24 | run: | 25 | python -m pip install --upgrade pip 26 | pip install -r requirements.txt 27 | - name: Upload package 28 | run: 29 | flit publish 30 | env: 31 | FLIT_USERNAME: __token__ 32 | FLIT_PASSWORD: ${{ secrets.PYPI_TOKEN }} 33 | # FLIT_INDEX_URL: https://test.pypi.org/legacy/ 34 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ${{ matrix.os }} 13 | strategy: 14 | matrix: 15 | python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] 16 | os: [ubuntu-latest, macOS-latest, windows-latest] 17 | 18 | steps: 19 | - name: Checkout 20 | uses: actions/checkout@v4 21 | - name: Set up Python ${{ matrix.python-version }} 22 | uses: actions/setup-python@v5 23 | with: 24 | python-version: ${{ matrix.python-version }} 25 | - name: Install Python dependencies 26 | run: | 27 | python -m pip install --upgrade pip 28 | pip install -r requirements.txt 29 | - name: Run tests 30 | run: | 31 | pytest -vv -s gitink/test.py 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | venv/ 3 | .cache/ 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![image](https://github.com/bast/gitink/workflows/Test/badge.svg)](https://github.com/bast/gitink/actions) 2 | [![image](https://img.shields.io/badge/license-%20MPL--v2.0-blue.svg)](LICENSE) 3 | [![PyPI badge](https://badge.fury.io/py/gitink.svg)](https://badge.fury.io/py/gitink) 4 | 5 | 6 | # gitink 7 | 8 | ASCII to SVG Git log graph visualizer. Useful for teaching Git. 9 | Under the hood it uses https://github.com/bast/ascii2graph. 10 | 11 | 12 | ## Example 13 | 14 | ```console 15 | $ cat example.txt 16 | 17 | [feature] 18 | | 19 | v 20 | x1-----x2 21 | / 22 | c1----c2----m1----c3----c4 23 | \ / ^ 24 | b1----b2----b3 | 25 | ^ ^ [main,HEAD] 26 | | | 27 | [_branch] [branch] 28 | ``` 29 | 30 | ```bash 31 | $ gitink --in-file=example.txt | display 32 | ``` 33 | 34 | This produces (display command requires 35 | [imagemagick](https://www.imagemagick.org)): 36 | 37 | ![git log graph example](img/example.jpg) 38 | 39 | 40 | ## Available options 41 | 42 | ```console 43 | $ gitink --help 44 | 45 | Usage: gitink [OPTIONS] 46 | 47 | Options: 48 | --scale FLOAT Scale sizes by this factor. 49 | --in-file TEXT ASCII file to convert. 50 | --time-direction INTEGER Direction of the time arrow (0, 90, 180, or 270). 51 | Default: 90 (right). 52 | --help Show this message and exit. 53 | ``` 54 | 55 | 56 | ## Installation 57 | 58 | ```bash 59 | $ pip install gitink 60 | ``` 61 | 62 | 63 | ## How do the colors work? 64 | 65 | Coloring is done according to the first character of the commit hash. Other 66 | suggestions welcome. 67 | 68 | 69 | ## Other projects that are interesting 70 | 71 | - [Asciio](https://nkh.github.io/P5-App-Asciio/): this project has a [Git 72 | mode](https://nkh.github.io/P5-App-Asciio/modes/git.html) and can be 73 | scripted [using the 74 | API](https://nkh.github.io/P5-App-Asciio/for_developers/scripting.html) to 75 | generate graphs. 76 | -------------------------------------------------------------------------------- /example.txt: -------------------------------------------------------------------------------- 1 | [feature] 2 | | 3 | v 4 | x1-----x2 5 | / 6 | c1----c2----m1----c3----c4 7 | \ / ^ 8 | b1----b2----b3 | 9 | ^ ^ [main,HEAD] 10 | | | 11 | [_branch] [branch] 12 | -------------------------------------------------------------------------------- /gitink/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | gitink: ASCII to SVG Git log graph visualizer. 3 | """ 4 | 5 | 6 | __version__ = "0.2.2" 7 | 8 | 9 | from .main import main 10 | -------------------------------------------------------------------------------- /gitink/cli.py: -------------------------------------------------------------------------------- 1 | import click 2 | from .main import main 3 | 4 | 5 | @click.command() 6 | @click.option("--scale", default=1.0, help="Scale sizes by this factor.") 7 | @click.option("--in-file", help="ASCII file to convert.") 8 | @click.option( 9 | "--time-direction", 10 | default=90, 11 | help="Direction of the time arrow (0, 90, 180, or 270). Default: 90 (right).", 12 | ) 13 | def cli(scale, in_file, time_direction): 14 | svg = main(scale, in_file, time_direction) 15 | print(svg) 16 | -------------------------------------------------------------------------------- /gitink/color.py: -------------------------------------------------------------------------------- 1 | def whiter_shade_of_pale(hex_color): 2 | """ 3 | This function pales the color a bit for the interior 4 | of the boxes. 5 | """ 6 | pale_shift = 70 7 | 8 | # separate the red, green, blue parts 9 | r_hex = hex_color[1:3] 10 | g_hex = hex_color[3:5] 11 | b_hex = hex_color[5:7] 12 | 13 | # convert from hex to dec 14 | r_dec = int(r_hex, 16) 15 | g_dec = int(g_hex, 16) 16 | b_dec = int(b_hex, 16) 17 | 18 | # make the color paler but make sure we do not go 19 | # beyond 255 or ff 20 | r_dec = min(255, r_dec + pale_shift) 21 | g_dec = min(255, g_dec + pale_shift) 22 | b_dec = min(255, b_dec + pale_shift) 23 | 24 | # convert from dec to hex 25 | r_hex = format(r_dec, "02x") 26 | g_hex = format(g_dec, "02x") 27 | b_hex = format(b_dec, "02x") 28 | 29 | # stitch them again together 30 | return "#{0}{1}{2}".format(r_hex, g_hex, b_hex) 31 | 32 | 33 | def get_color(text): 34 | # this is the deep palette of https://seaborn.pydata.org/ 35 | palette = ["#4C72B0", "#55A868", "#C44E52", "#8172B2", "#CCB974", "#64B5CD"] 36 | 37 | position = ord(text[0]) % len(palette) 38 | color = palette[position] 39 | pale_color = whiter_shade_of_pale(color) 40 | 41 | return color, pale_color 42 | -------------------------------------------------------------------------------- /gitink/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from ascii2graph import graph 3 | from .color import get_color 4 | from .svg import draw_box, draw_line, draw_arrow, embed_svg, get_box_dims 5 | 6 | 7 | def get_box_center(scale, text, origin): 8 | (x, y) = origin 9 | width, height = get_box_dims(scale, text) 10 | return x + 0.5 * width, y + 0.5 * height 11 | 12 | 13 | def parse_graph(file_name): 14 | with open(file_name, "r") as f: 15 | text = f.read() 16 | return graph(text) 17 | 18 | 19 | def move_children_behind_current_commit( 20 | scale, time_direction, min_separation, commit, commits, placement 21 | ): 22 | (_, _, text) = commit 23 | width, height = get_box_dims(scale, text) 24 | 25 | if time_direction in [90, 270]: 26 | _time_dim = 0 27 | _sep_dim = 1 28 | else: 29 | _time_dim = 1 30 | _sep_dim = 0 31 | 32 | children = commits[commit] 33 | for child in children: 34 | (line_number, character_position, text, angle) = child 35 | k = (line_number, character_position, text) 36 | 37 | # in our definition the angle difference cannot be larger than 90 38 | # unless the time_direction is 0 39 | if abs(angle - time_direction) > 90: 40 | angle -= 360 41 | 42 | # time direction | smaller | larger 43 | # 0 | left | right 44 | # 90 | up | down 45 | # 180 | right | left 46 | # 270 | down | up 47 | if time_direction in [0, 90]: 48 | if (angle - time_direction) < 0: 49 | shift = "min" 50 | elif (angle - time_direction) > 0: 51 | shift = "max" 52 | else: 53 | shift = None 54 | else: 55 | if (angle - time_direction) < 0: 56 | shift = "max" 57 | elif (angle - time_direction) > 0: 58 | shift = "min" 59 | else: 60 | shift = None 61 | if shift == "min": 62 | placement[k][_sep_dim] = min( 63 | placement[k][_sep_dim], placement[commit][_sep_dim] - min_separation 64 | ) 65 | elif shift == "max": 66 | placement[k][_sep_dim] = max( 67 | placement[k][_sep_dim], placement[commit][_sep_dim] + min_separation 68 | ) 69 | else: 70 | placement[k][_sep_dim] = placement[commit][_sep_dim] 71 | 72 | # displace children in the direction of the time arrow 73 | # make sure children are placed behind parents 74 | if time_direction in [90, 180]: 75 | placement[k][_time_dim] = max( 76 | placement[k][_time_dim], placement[commit][_time_dim] + min_separation 77 | ) 78 | else: 79 | placement[k][_time_dim] = min( 80 | placement[k][_time_dim], placement[commit][_time_dim] - min_separation 81 | ) 82 | 83 | placement = move_children_behind_current_commit( 84 | scale, time_direction, min_separation, k, commits, placement 85 | ) 86 | return placement 87 | 88 | 89 | def point_inside_box(point_xy, box_center_xy, box_width, box_height): 90 | if point_xy[0] > box_center_xy[0] + 0.5 * box_width: 91 | return False 92 | if point_xy[0] < box_center_xy[0] - 0.5 * box_width: 93 | return False 94 | if point_xy[1] > box_center_xy[1] + 0.5 * box_height: 95 | return False 96 | if point_xy[1] < box_center_xy[1] - 0.5 * box_height: 97 | return False 98 | return True 99 | 100 | 101 | def arrow_head(arrow_origin_xy, box_center_xy, box_width, box_height): 102 | """ 103 | Locates the arrow head so that it is just barely outside the target box. 104 | """ 105 | vx = arrow_origin_xy[0] - box_center_xy[0] 106 | vy = arrow_origin_xy[1] - box_center_xy[1] 107 | 108 | # this is done in a silly way 109 | num_steps = 100 110 | for f in range(num_steps + 1): 111 | s = f / num_steps 112 | x = box_center_xy[0] + s * vx 113 | y = box_center_xy[1] + s * vy 114 | if not point_inside_box((x, y), box_center_xy, box_width, box_height): 115 | return x, y 116 | sys.stderr.write("ERROR: head not found in arrow_head\n") 117 | sys.exit(1) 118 | 119 | 120 | def main(scale, in_file, time_direction): 121 | assert time_direction in [0, 90, 180, 270] 122 | 123 | if time_direction == 0: 124 | forward_angles = [315, 0, 45] 125 | elif time_direction == 90: 126 | forward_angles = [45, 90, 135] 127 | elif time_direction == 180: 128 | forward_angles = [135, 180, 225] 129 | elif time_direction == 270: 130 | forward_angles = [225, 270, 315] 131 | 132 | file_name = in_file 133 | _graph = parse_graph(file_name) 134 | 135 | # filter out all branches and tags 136 | _commits = {node: _graph[node] for node in _graph if not node[2].startswith("[")} 137 | 138 | # only keep pointers (branches, tags) 139 | pointers = {node: _graph[node] for node in _graph if node[2].startswith("[")} 140 | 141 | # make sure edges point to children only 142 | # we do this by filtering out all angles that point "backwards" 143 | commits = {} 144 | for node in _commits: 145 | commits[node] = [] 146 | for node in _commits: 147 | for child in _commits[node]: 148 | angle = child[3] 149 | if angle in forward_angles: 150 | commits[node].append(child) 151 | 152 | parents = {} 153 | for node in commits: 154 | for child in commits[node]: 155 | (line_number, character_position, text, _) = child 156 | k = (line_number, character_position, text) 157 | if k not in parents: 158 | parents[k] = [] 159 | parents[k].append(node) 160 | 161 | # find root commit, this is the commit without any parents 162 | commits_without_parents = [commit for commit in commits if commit not in parents] 163 | assert len(commits_without_parents) == 1 164 | root_commit = commits_without_parents[0] 165 | 166 | placement = {} 167 | for line_number, character_position, name in commits: 168 | placement[(line_number, character_position, name)] = [0.0, 0.0] 169 | 170 | placement = move_children_behind_current_commit( 171 | scale=scale, 172 | time_direction=time_direction, 173 | min_separation=scale * 60.0, 174 | commit=root_commit, 175 | commits=commits, 176 | placement=placement, 177 | ) 178 | 179 | s_svg = "" 180 | 181 | # first we place the edges 182 | for node in commits: 183 | center_parent = get_box_center(scale, name, placement[node]) 184 | for child in commits[node]: 185 | (line_number, character_position, text, _) = child 186 | k = (line_number, character_position, text) 187 | center_child = get_box_center(scale, text, placement[k]) 188 | s_svg += draw_line( 189 | x1=center_parent[0], 190 | y1=center_parent[1], 191 | x2=center_child[0], 192 | y2=center_child[1], 193 | scale=scale, 194 | color="#000000", 195 | opacity=0.8, 196 | ) 197 | 198 | x_min = sys.float_info.max 199 | y_min = sys.float_info.max 200 | x_max = -sys.float_info.max 201 | y_max = -sys.float_info.max 202 | 203 | # later we place the nodes so that they show up "on top" 204 | for node in commits: 205 | (_, _, name) = node 206 | (x, y) = placement[node] 207 | width, height = get_box_dims(scale, name) 208 | x_min = min(x_min, x) 209 | y_min = min(y_min, y) 210 | x_max = max(x_max, x + width) 211 | y_max = max(y_max, y + height) 212 | stroke_color, fill_color = get_color(name) 213 | s_svg += draw_box( 214 | text=name, 215 | x=x, 216 | y=y, 217 | scale=scale, 218 | stroke_color=stroke_color, 219 | fill_color=fill_color, 220 | opacity=1.0, 221 | rounded=True, 222 | ) 223 | 224 | # finally place branches and tags 225 | for node in pointers: 226 | (line_number, character_position, text, angle) = pointers[node][0] 227 | (x, y) = placement[(line_number, character_position, text)] 228 | (x_target, y_target) = get_box_center(scale, text, (x, y)) 229 | (_, _, tag_text) = node 230 | target_width, target_height = get_box_dims(scale, text) 231 | 232 | # remove the starting [ and ending ] 233 | tag_text = tag_text[1:-1] 234 | 235 | # tags can be a comma separated list 236 | for tag in tag_text.split(","): 237 | tag_width, tag_height = get_box_dims(scale, tag) 238 | if angle == 0: 239 | x = x + 0.5 * target_width - 0.5 * tag_width 240 | y = y + target_height + scale * 35.0 241 | elif angle == 180: 242 | x = x + 0.5 * target_width - 0.5 * tag_width 243 | y = y - tag_height - scale * 35.0 244 | if angle == 90: 245 | x = x - tag_width - scale * 35.0 246 | elif angle == 270: 247 | x = x + target_width + scale * 35.0 248 | x_min = min(x_min, x) 249 | y_min = min(y_min, y) 250 | x_max = max(x_max, x + tag_width) 251 | y_max = max(y_max, y + tag_height) 252 | center_tag = get_box_center(scale, tag, (x, y)) 253 | x2, y2 = arrow_head( 254 | arrow_origin_xy=center_tag, 255 | box_center_xy=(x_target, y_target), 256 | box_width=target_width, 257 | box_height=target_height, 258 | ) 259 | # update target 260 | target_width, target_height = tag_width, tag_height 261 | (x_target, y_target) = center_tag 262 | if tag.startswith("_"): 263 | tag_opacity = 0.2 264 | fill_color = "#ffffff" 265 | _tag = tag[1:] 266 | ghost = True 267 | else: 268 | tag_opacity = 1.0 269 | fill_color = "#dddddd" 270 | _tag = tag 271 | if _tag == "HEAD": 272 | fill_color = "#ffe600" 273 | ghost = False 274 | s_svg += draw_arrow( 275 | x1=center_tag[0], 276 | y1=center_tag[1], 277 | x2=x2, 278 | y2=y2, 279 | scale=scale, 280 | color="#000000", 281 | ghost=ghost, 282 | ) 283 | s_svg += draw_box( 284 | text=_tag, 285 | x=x, 286 | y=y, 287 | scale=scale, 288 | stroke_color="#000000", 289 | fill_color=fill_color, 290 | opacity=tag_opacity, 291 | rounded=False, 292 | ) 293 | 294 | bbox = (x_min, y_min, x_max, y_max) 295 | s_svg = embed_svg(text=s_svg, bbox=(x_min, y_min, x_max, y_max)) 296 | return s_svg 297 | -------------------------------------------------------------------------------- /gitink/svg.py: -------------------------------------------------------------------------------- 1 | def get_box_dims(scale, text): 2 | width = scale * 8.0 * (len(text) + 2) 3 | height = scale * 30.0 4 | return width, height 5 | 6 | 7 | def embed_svg(text, bbox): 8 | (x_min, y_min, x_max, y_max) = bbox 9 | height = y_max - y_min 10 | width = x_max - x_min 11 | 12 | return """ 13 | 14 | 28 | 29 | 31 | 38 | 42 | 43 | 50 | 54 | 55 | 56 | 60 | {text} 61 | 62 | """.format( 63 | width=width, height=height, translate_x=-x_min, translate_y=-y_min, text=text 64 | ) 65 | 66 | 67 | def draw_line(x1, y1, x2, y2, scale, color, opacity): 68 | return """ 69 | 76 | """.format( 77 | x1=x1, y1=y1, x2=x2, y2=y2, color=color, width=scale * 2.5, opacity=opacity 78 | ) 79 | 80 | 81 | def draw_arrow(x1, y1, x2, y2, scale, color, ghost): 82 | if ghost: 83 | opacity = 0.2 84 | marker = "ghostmarker" 85 | else: 86 | opacity = 1.0 87 | marker = "fullmarker" 88 | 89 | return """ 90 | 101 | """.format( 102 | x1=x1, 103 | y1=y1, 104 | x2=x2, 105 | y2=y2, 106 | color=color, 107 | width=scale * 2.5, 108 | opacity=opacity, 109 | marker=marker, 110 | ) 111 | 112 | 113 | def draw_box(text, x, y, scale, stroke_color, fill_color, opacity, rounded): 114 | width, height = get_box_dims(scale, text) 115 | text_x = x + scale * 7.5 116 | text_y = y + scale * 12.5 + 8.0 117 | if rounded: 118 | rounding = scale * 5.0 119 | else: 120 | rounding = 0.0 121 | font_size = scale * 12.5 122 | stroke_width = scale * 1.25 123 | 124 | return """ 125 | 136 | {text} 148 | """.format( 149 | fill_color=fill_color, 150 | stroke_color=stroke_color, 151 | stroke_width=stroke_width, 152 | width=width, 153 | height=height, 154 | x=x, 155 | y=y, 156 | text_x=text_x, 157 | text_y=text_y, 158 | rounding=rounding, 159 | font_size=font_size, 160 | opacity=opacity, 161 | text=text, 162 | ) 163 | -------------------------------------------------------------------------------- /gitink/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | from .main import main 3 | 4 | 5 | def test_main(): 6 | generate_references = os.getenv("GENERATE_REFERENCES", False) 7 | _this_path = os.path.dirname(os.path.realpath(__file__)) 8 | 9 | for in_file, time_direction in [("right", 90), ("up", 0), ("down", 180)]: 10 | in_file_name = os.path.join(_this_path, "test", "{0}.txt".format(in_file)) 11 | ref_file_name = os.path.join(_this_path, "test", "{0}.svg".format(in_file)) 12 | svg = main(1.0, in_file_name, time_direction) 13 | if generate_references: 14 | with open(ref_file_name, "w") as f: 15 | f.write(svg) 16 | with open(ref_file_name, "r") as f: 17 | reference = f.read() 18 | assert svg == reference, "failed to test {0}".format(in_file) 19 | -------------------------------------------------------------------------------- /gitink/test/down.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 20 | 27 | 31 | 32 | 39 | 43 | 44 | 45 | 49 | 50 | 57 | 58 | 65 | 66 | 73 | 74 | 81 | 82 | 89 | 90 | 101 | x1 113 | 114 | 125 | x2 137 | 138 | 149 | c3 161 | 162 | 173 | c4 185 | 186 | 197 | c1 209 | 210 | 221 | c2 233 | 234 | 245 | 246 | 257 | foo 269 | 270 | 281 | 282 | 293 | main 305 | 306 | 307 | -------------------------------------------------------------------------------- /gitink/test/down.txt: -------------------------------------------------------------------------------- 1 | c1 2 | | 3 | | 4 | c2 5 | | 6 | | 7 | c3 8 | | \ 9 | | \ 10 | | \ 11 | | x1 12 | | | 13 | [main]--->c4 | 14 | x2<---[foo] 15 | -------------------------------------------------------------------------------- /gitink/test/right.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 20 | 27 | 31 | 32 | 39 | 43 | 44 | 45 | 49 | 50 | 57 | 58 | 65 | 66 | 73 | 74 | 81 | 82 | 89 | 90 | 97 | 98 | 105 | 106 | 113 | 114 | 121 | 122 | 129 | 130 | 141 | c1 153 | 154 | 165 | c2 177 | 178 | 189 | b2 201 | 202 | 213 | b3 225 | 226 | 237 | b1 249 | 250 | 261 | x1 273 | 274 | 285 | x2 297 | 298 | 309 | m1 321 | 322 | 333 | c3 345 | 346 | 357 | c4 369 | 370 | 381 | 382 | 393 | branch 405 | 406 | 417 | 418 | 429 | main 441 | 442 | 453 | 454 | 465 | HEAD 477 | 478 | 489 | 490 | 501 | branch 513 | 514 | 525 | 526 | 537 | feature 549 | 550 | 551 | -------------------------------------------------------------------------------- /gitink/test/right.txt: -------------------------------------------------------------------------------- 1 | [feature] 2 | | 3 | v 4 | x1-----x2 5 | / 6 | c1----c2----m1----c3----c4 7 | \ / ^ 8 | b1----b2----b3 | 9 | ^ ^ [main,HEAD] 10 | | | 11 | [_branch] [branch] 12 | -------------------------------------------------------------------------------- /gitink/test/up.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 17 | 18 | 20 | 27 | 31 | 32 | 39 | 43 | 44 | 45 | 49 | 50 | 57 | 58 | 65 | 66 | 73 | 74 | 81 | 82 | 93 | c3 105 | 106 | 117 | c2 129 | 130 | 141 | c1 153 | 154 | 165 | c4 177 | 178 | 189 | a1 201 | 202 | 213 | 214 | 225 | origin/main 237 | 238 | 249 | 250 | 261 | feature 273 | 274 | 285 | 286 | 297 | HEAD 309 | 310 | 311 | -------------------------------------------------------------------------------- /gitink/test/up.txt: -------------------------------------------------------------------------------- 1 | [origin/main]-->c4 a1<--[feature,HEAD] 2 | \ / 3 | \ / 4 | c3 5 | | 6 | | 7 | c2 8 | | 9 | | 10 | | 11 | c1 12 | -------------------------------------------------------------------------------- /img/example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bast/gitink/f022a3490f11e6e840b40eaee8ab02fc13c1c4a5/img/example.jpg -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["flit_core >=3.2,<4"] 3 | build-backend = "flit_core.buildapi" 4 | 5 | [project] 6 | name = "gitink" 7 | authors = [ 8 | {name = "Radovan Bast", email = "radovan.bast@uit.no"}, 9 | ] 10 | dynamic = ["version", "description"] 11 | classifiers = [ 12 | "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)", 13 | ] 14 | dependencies = [ 15 | "click", 16 | "ascii2graph", 17 | ] 18 | readme = "README.md" 19 | 20 | [project.urls] 21 | Home = "https://github.com/bast/gitink" 22 | 23 | [project.scripts] 24 | gitink = "gitink.cli:cli" 25 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | flit 3 | vulture 4 | ascii2graph 5 | --------------------------------------------------------------------------------