From 2359b904a77be1a9c27c728ee0a9df6558726cc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A3=A8=EB=B0=80LuMir?= Date: Fri, 3 Jan 2025 23:38:12 +0900 Subject: [PATCH] chore: create `examples/git-clang-format` (#131) This pull request introduces a new example project demonstrating the usage of the `git-clang-format` command. The changes include configuration files, documentation, and example source code. ### New example project for `git-clang-format`: * [`examples/git-clang-format/.clang-format`](diffhunk://#diff-3c15af9b07d3d1f0abe57fe105ed45cf8d43841bcd3f1c1cefd7f77b22c755d9R1-R2): Added configuration file based on Google style with a column limit of 90. * [`examples/git-clang-format/README.md`](diffhunk://#diff-5bd7f2d85a02a051e388d92fc7cf99b27213d473269402d48097a537570609adR1-R132): Added documentation explaining how to use `git-clang-format` with a step-by-step guide and example outputs. * [`examples/git-clang-format/package.json`](diffhunk://#diff-4b5616cba814f4c7ca4ea49c77e8456efcc7931852306a3d899597507ad137b6R1-R13): Added package configuration with scripts for modifying and formatting the example source code. * [`examples/git-clang-format/src/main.c`](diffhunk://#diff-6818c1c5b98f76188b7448927e9e54053967986c07f5b1596cf3b60499b6578bR1-R13): Added example source code file to demonstrate the formatting changes. * [`examples/git-clang-format/src/main_overwrite.txt`](diffhunk://#diff-9fd2872ab9b374d557d9ae0d2303c06f76fdf02502adeb21c238de284ee9e191R1-R13): Added helper file used by the script to modify the example source code. --- examples/git-clang-format/.clang-format | 2 + examples/git-clang-format/README.md | 132 ++++++++++++++++++ examples/git-clang-format/package.json | 13 ++ examples/git-clang-format/src/main.c | 13 ++ .../git-clang-format/src/main_overwrite.txt | 13 ++ package-lock.json | 10 +- 6 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 examples/git-clang-format/.clang-format create mode 100644 examples/git-clang-format/README.md create mode 100644 examples/git-clang-format/package.json create mode 100644 examples/git-clang-format/src/main.c create mode 100644 examples/git-clang-format/src/main_overwrite.txt diff --git a/examples/git-clang-format/.clang-format b/examples/git-clang-format/.clang-format new file mode 100644 index 0000000..1516991 --- /dev/null +++ b/examples/git-clang-format/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: Google +ColumnLimit: 90 diff --git a/examples/git-clang-format/README.md b/examples/git-clang-format/README.md new file mode 100644 index 0000000..b985818 --- /dev/null +++ b/examples/git-clang-format/README.md @@ -0,0 +1,132 @@ +# `examples-git-clang-format` + +This example demonstrates the features of `git-clang-format` command (same as `git clang-format` command). + +Follow the guidelines below to see how `git-clang-format` works. + +## Getting Started + +Take a look at the `src/main.c` file in the `examples/git-clang-format` directory. + +Note that this file is not formatted according to the coding style configured in the `.clang-format` file at the root. + +In this scenario, what would happen if I add a space to line 9 of the `src/main.c` file, run `git add src/main.c`, and then execute `npx git-clang-format` (same as running `git clang-format`)? + +```c +/* src/main.c */ + +#include + +int main(void) { + printf("Line 4"); + printf("Line 5"); + printf("Line 6"); + printf("Line 7"); + printf("Line 8"); + printf("Line 9"); + printf("Line 10"); + + return 0; +} +``` + +### Step 1 + +Run the following command to add a space to line 9 of the `src/main.c` file. + +#### Run from the Root + +```sh +npm run add-a-space-to-line-9-of-main-c-file -w examples/git-clang-format +``` + +#### Navigate to the Directory and Run + +```sh +cd examples/git-clang-format + +npm run add-a-space-to-line-9-of-main-c-file +``` + +--- + +Now, you can see that the `src/main.c` file has been modified as shown below. + +```diff +- printf("Line 9"); ++ printf("Line 9"); +``` + +### Step 2 + +Run the following command to add the modified `src/main.c` file to the staging area of Git. + +#### Run from the Root + +```sh +npm run git-add -w examples/git-clang-format +``` + +#### Navigate to the Directory and Run + +```sh +# cd examples/git-clang-format (You are already in the directory.) + +npm run git-add +``` + +--- + +Now, you can see that the `src/main.c` file has been added to the staging area of Git. + +```txt +Changes to be committed: + (use "git restore --staged ..." to unstage) + modified: src/main.c +``` + +### Step 3 + +Run the following command to run `git-clang-format` and check the result. + +#### Run from the Root + +```sh +npm run git-clang-format -w examples/git-clang-format +``` + +#### Navigate to the Directory and Run + +```sh +# cd examples/git-clang-format (You are already in the directory.) + +npm run git-clang-format +``` + +--- + +If you run the command, it should produce an **error message** like the one below, since the modified `src/main.c` file is not formatted according to the coding style configured in the `.clang-format` file. + +```txt +> examples-git-clang-format@1.2.3 git-clang-format +> npx git-clang-format + +changed files: + examples/git-clang-format/src/main.c +Process exited with code: 1 +``` + +Check the 'Changes not staged for commit' section in Git using the `git status` command in the CLI and review the `src/main.c` file. The `git-clang-format` command should have fixed the code formatting according to the coding style configured in the `.clang-format` file. + +So, it is now properly formatted according to the configured coding style. + +```diff +- printf("Line 9"); ++ printf("Line 9"); +``` + +## Notable Points + +If you run the `npx clang-format src/main.c` command, it will format the entire `src/main.c` file according to the coding style configured in the `.clang-format` file. + +However, if you run the `npx clang-format src/main.c` command on a modified file, it will only format the changed lines of code. This is known as **Surgical Formatting**. diff --git a/examples/git-clang-format/package.json b/examples/git-clang-format/package.json new file mode 100644 index 0000000..8005acd --- /dev/null +++ b/examples/git-clang-format/package.json @@ -0,0 +1,13 @@ +{ + "private": true, + "name": "examples-git-clang-format", + "version": "1.2.3", + "scripts": { + "add-a-space-to-line-9-of-main-c-file": "cat src/main_overwrite.txt > src/main.c", + "git-add": "git add src/main.c && git status", + "git-clang-format": "npx git-clang-format" + }, + "dependencies": { + "clang-format-git": "^1.2.3" + } +} diff --git a/examples/git-clang-format/src/main.c b/examples/git-clang-format/src/main.c new file mode 100644 index 0000000..4ab9b6e --- /dev/null +++ b/examples/git-clang-format/src/main.c @@ -0,0 +1,13 @@ +#include + +int main(void) { + printf("Line 4"); + printf("Line 5"); + printf("Line 6"); + printf("Line 7"); + printf("Line 8"); + printf("Line 9"); + printf("Line 10"); + + return 0; +} diff --git a/examples/git-clang-format/src/main_overwrite.txt b/examples/git-clang-format/src/main_overwrite.txt new file mode 100644 index 0000000..adec4dc --- /dev/null +++ b/examples/git-clang-format/src/main_overwrite.txt @@ -0,0 +1,13 @@ +#include + +int main(void) { + printf("Line 4"); + printf("Line 5"); + printf("Line 6"); + printf("Line 7"); + printf("Line 8"); + printf("Line 9"); + printf("Line 10"); + + return 0; +} diff --git a/package-lock.json b/package-lock.json index 02f9047..1095d54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,10 +39,10 @@ } }, "examples/git-clang-format": { - "extraneous": true, + "name": "examples-git-clang-format", + "version": "1.2.3", "dependencies": { - "clang-format-git": "^1.2.1", - "clang-format-git-python": "^1.2.1" + "clang-format-git": "^1.2.3" } }, "node_modules/@ampproject/remapping": { @@ -7058,6 +7058,10 @@ "resolved": "examples/clang-format", "link": true }, + "node_modules/examples-git-clang-format": { + "resolved": "examples/git-clang-format", + "link": true + }, "node_modules/execa": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/execa/-/execa-8.0.1.tgz",