Standardise code formating for cmake projects with git and clang-format

Overview

git-cmake-format

This project aims to provide a quick and easy way to integrate clang-format into your CMake project hosted in a git repository, it consists of three elements.

  • CMakeLists.txt provides the custom format target
  • git-pre-commit-hook blocks commits of unformatted C/C++ files
  • git-cmake-format.py is called by the format target or the pre-commit hook, it queries git for edited files, then block the commit or formats the sources

Dependencies

There are three dependencies:

  • git
  • python 2.7+
  • clang-format

Usage

To make use of this project you can either add it as a submodule to your existing project, or copy the files into your repository. Now add the following to your CMakeLists.txt.

add_subdirectory(/path/to/git-cmake-format)

Next you can generate your build system, assuming the required dependencies are available on your path.

cd build
cmake ..

The installation of the pre-commit hook is done at CMake time during the generation of the build system, if you have followed these steps it is already installed at /path/to/your/project/.git/hooks/pre-commit.

Options

It is possible to specify the path to any of the executables this project depends upon using the following CMake variables.

  • GCF_GIT_PATH:STRING=/path/to/git
  • GCF_PYTHON_PATH:STRING=/path/to/python
  • GCF_CLANGFORMAT_PATH:STRING=/path/to/clang-format defaults to clang-format

It is also possible to set the -style= command line argument for clang-format with the following options, the default is file

  • GCF_CLANGFORMAT_STYLE:STRING=WebKit
Comments
  • Git root finding + python compatibility

    Git root finding + python compatibility

    Both the CMake script and the python script find out the git root directory per git rev-parse call to adapt to various repository layouts To make the scripts usable on systems with /usr/bin/python linking to python3, explicityly require python2 in the shebang and CMake find_program call

    opened by dfunke 4
  • Use find_package(...) to locate clang-format, git, and python

    Use find_package(...) to locate clang-format, git, and python

    • Use standard FindGit.cmake and FindPythonInterp.cmake
    • Locate clang-format executable with find_package command
    • Unset all non-cache v ariables when not required anymore
    • Tag cache variables as "advanced"
    opened by tristan0x 3
  • Exclude some files?

    Exclude some files?

    Is it possible to exclude some files from the format checking / reformating? I typically use a vendor directory, which includes sources coming from 3rd parties, which should stay the way I received them.

    What is the best way to implement such a feature? Do you have an idea?

    opened by jbreitbart 3
  • Name confusion

    Name confusion

    There now exists a program called cmake-format for auto-formatting CMake listfiles (written in Python: pip install cmake-format and then run cmake-format from the command line). The name of git-cmake-format is a bit confusing under the circumstances since AFAICT it doesn't use cmake-format :)

    opened by lassik 2
  • Fix duplicate else() block in CMakeLists, and runtime error when using Python 3

    Fix duplicate else() block in CMakeLists, and runtime error when using Python 3

    Just a small fix of the duplicate else() statement in CMakeLists.txt which crashes the configure phase, and a runtime error due to string vs bytes inconsistency in Python 3.

    opened by gflegar 0
  • Only reject staged unformatted files

    Only reject staged unformatted files

    Right now git-cmake-format rejects commits where any tracked C/C++ file is improperly formatted. Here's an example:

    > git status
    
    Changes not staged for commit:
        modified:     foo.cpp
        modified:     bar.cpp
    
    > git add foo.cpp
    > git commit -m "Updated foo"
    
    'bar.cpp' must be formatted, run the cmake target 'format'
    

    I think it would be an easier work flow if I could still commit foo.cpp even if bar.cpp wasn't formatted.

    Glancing at it, you could try adding --cached to the flags passed to git in git-cmake-format.py/getEditedFiles()

    opened by frasercrmck 0
  • Set clang-format style from CMakeLists.txt

    Set clang-format style from CMakeLists.txt

    Not all users will require the use of a .clang-format file as they will be using one of the built in styles. Add a CMake option to allow setting this value, default to file.

    enhancement 
    opened by kbenzie 0
  • git commit --amend fails

    git commit --amend fails

    When trying to amend a commit, I see the following error:

    Traceback (most recent call last):
      File "/Users/gegles/Workspaces/fasp.io/cmake/git-cmake-format/git-cmake-format.py", line 114, in <module>
        if not isFormattable(FileName):
      File "/Users/gegles/Workspaces/fasp.io/cmake/git-cmake-format/git-cmake-format.py", line 43, in isFormattable
        if '' != Dir and '' != os.path.commonprefix([os.path.relpath(File), os.path.relpath(Dir)]):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 428, in relpath
        raise ValueError("no path specified")
    ValueError: no path specified
    

    Probably need to support the script running with no actual files changed..

    question 
    opened by gegles 1
Owner
Kenneth Benzie
Kenneth Benzie
The OpenEXR project provides the specification and reference implementation of the EXR file format, the professional-grade image storage format of the motion picture industry.

OpenEXR OpenEXR provides the specification and reference implementation of the EXR file format, the professional-grade image storage format of the mot

Academy Software Foundation 1.3k Sep 26, 2022
(Simple String Format) is an syntax of format and a library for parse this.

SSFMT (Simple String Format) is an syntax of format and a library for parse this. SSFMT != {fmt} SSFMT is NOT an API/library for parse {fmt} syntax !

null 2 Jan 30, 2022
An implementation of memcpy for amd64 with clang/gcc

memcpy-amd64 A memcpy library for amd64 platforms. Parameters void memcpy_<set/get>_erms_threshold(size_t) (default 2048): threshold to use rep movsb

Schrodinger ZHU Yifan 13 Feb 5, 2022
Cmake check - Static analysis for CMake files.

cmake_check Cmake_check is a linter for the CMake language. It takes a set of user-defined rules and reports violations for CMakeLists.txt files and C

Dael 20 Mar 22, 2020
A fast Perforce to Git conversion tool written in C++ using Perforce Helix Core C++ API and Libgit2

P4 Fusion A fast Perforce depot to Git repository converter using the Helix Core C/C++ API as an attempt to mitigate the performance bottlenecks in gi

Salesforce 52 Sep 20, 2022
Open source courseware for Git and GitHub

GitHub Training Kit: Cheatsheets We ❤️ Contributors Like You! We’re eager to work with you, our user community, to improve these materials and develop

GitHub 3.7k Sep 30, 2022
Not a big fan of git. May create a nicer repo in the future.

os My x86-64 hobby operating system. Cooperative multitasking system with no user-mode support, everything runs on ring 0 (for now). Packed with a rea

tiagoporsch 13 Sep 9, 2022
This is the git repository for the FFTW library for computing Fourier transforms (version 3.x), maintained by the FFTW authors.

This is the git repository for the FFTW library for computing Fourier transforms (version 3.x), maintained by the FFTW authors.

FFTW 2.2k Oct 1, 2022
ncurses Git mirror

------------------------------------------------------------------------------- -- Copyright 2020,2021 Thomas E. Dickey

Repo mirrors 294 Oct 4, 2022
The official Allegro 5 git repository. Pull requests welcome!

Welcome to Allegro! Allegro is a cross-platform library mainly aimed at video game and multimedia programming. It handles common, low-level tasks such

Allegro 1.5k Oct 3, 2022
File path converter for Windows & Git Bash

windows-git-bash-path-converter Motivation Made this because it was so mad to convert path between Windows and Git Bash How to use Windows file path t

Jooho Lee 3 Mar 15, 2022
Flight rules for git

Flight rules for Git ?? English ∙ Español ∙ Русский ∙ 简体中文∙ 한국어 ∙ Tiếng Việt ∙ Français ∙ 日本語 What are "flight rules"? A guide for astronauts (now, pr

Kate Hudson 39.3k Oct 1, 2022
A code generator for C++ projects.

Cmple A code preprocessor/generator for C++ cross-platform projects. This utility transforms simplified C++ source code files into fully-functional co

Anton Dospekhov 22 Sep 17, 2022
format lua code

EmmyLuaCodeStyle 项目介绍 该项目是基于C++的lua代码格式化算法库 经过长期实践,发现人们对格式化算法的预期是尽可能少的改动代码的行布局,而列布局符合基本审美就可以了。 基于这样的想法我设计并实现了lua格式化算法 格式化行为介绍 基本语句 该算法的主要特点是分析并对当前代码做出

null 46 Sep 21, 2022
PLP Project Programming Language | Programming for projects and computer science and research on computer and programming.

PLPv2b PLP Project Programming Language Programming Language for projects and computer science and research on computer and programming. What is PLP L

PLP Language 5 Aug 20, 2022
Library of useful C++ snippets and reusable classes I've created as I build out Arduino Uno and ESP32 projects.

Arduino Snippets Library of useful C++ snippets and reusable classes I've created as I build out Arduino Uno and ESP32 projects. Button A simple butto

Max Lynch 7 Feb 5, 2022
Several Proteus projects using Arduino Boards and sensors, these were parts of IoT course homework #1.

1. Print Print your name on virtual terminal using one of the Arduino family microcontrollers. 2. Highway Based on the position of the car in highway,

Parsa 1 Nov 3, 2021
A collection of academic and personal projects demonstrating my proficiency in programming.

Best-README-Template An awesome README template to jumpstart your projects! Explore the docs » View Demo · Report Bug · Request Feature Table of Conte

Kevyn Higbee 1 Nov 18, 2021
A cool and transposable Makefile for C++ projects.

Makefile A cool and transposable Makefile for C++ projects. Features Cool and colorfull display. A fancy progress bar with the (up to date) number of

Romain T-BIGOT 5 Feb 21, 2022