Skip to content

Commit

Permalink
chore: create examples/git-clang-format (#131)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lumirlumir authored Jan 3, 2025
1 parent 8c651d4 commit 2359b90
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/git-clang-format/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
ColumnLimit: 90
132 changes: 132 additions & 0 deletions examples/git-clang-format/README.md
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>

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 <file>..." 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**.
13 changes: 13 additions & 0 deletions examples/git-clang-format/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 13 additions & 0 deletions examples/git-clang-format/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

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;
}
13 changes: 13 additions & 0 deletions examples/git-clang-format/src/main_overwrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

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;
}
10 changes: 7 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2359b90

Please sign in to comment.