Skip to content

Commit

Permalink
feat: support generate json output file (#60)
Browse files Browse the repository at this point in the history
* feat: support generate json output file

* feat: update ci to generate json file

* fix: rename varible name to aviod same with build-in

* fix: rename varible name to aviod same with build-in
  • Loading branch information
shenxianpeng authored Jan 21, 2025
1 parent b51156c commit 4c19dda
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
.\venv\Scripts\activate.bat
# for testing
pip install .
gitstats . gitstats-report-windows
gitstats . gitstats-report-windows -f json
- name: Save GitStats Report
if: ${{ matrix.python-version == '3.9' }}
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
source venv/bin/activate
# for testing
pip install .
gitstats . gitstats-report-macos
gitstats . gitstats-report-macos -f json
- name: Save GitStats Report
if: ${{ matrix.python-version == '3.9' }}
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
source venv/bin/activate
# for testing
pip install .
gitstats . gitstats-report
gitstats . gitstats-report -f json
- name: Save GitStats Report
if: ${{ matrix.python-version == '3.9' }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gitstory.egg-info
build/
gitstats-report
test-report
test-report.json
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ docker run ghcr.io/shenxianpeng/gitstats:latest --help
## Usage

```bash
usage: gitstats [-h] [-v] [-c key=value] <gitpath> [<gitpath> ...] <outputpath>
gitstats --help
usage: gitstats [-h] [-v] [-c key=value] [-f {json}] <gitpath> [<gitpath> ...] <outputpath>

Generate statistics for a Git repository.

Expand All @@ -70,7 +71,11 @@ options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-c key=value, --config key=value
Override configuration value. Can be specified multiple times. Default configuration: {'max_domains': 10, 'max_ext_length': 10, 'style': 'gitstats.css', 'max_authors': 20, 'authors_top': 5, 'commit_begin': '', 'commit_end': 'HEAD', 'linear_linestats': 1, 'project_name': '', 'processes': 8, 'start_date': ''}.
Override configuration value. Can be specified multiple times. Default configuration: {'max_domains':
10, 'max_ext_length': 10, 'style': 'gitstats.css', 'max_authors': 20, 'authors_top': 5, 'commit_begin':
'', 'commit_end': 'HEAD', 'linear_linestats': 1, 'project_name': '', 'processes': 8, 'start_date': ''}.
-f {json}, --format {json}
The extra format of the output file.
```
> [!TIP]
Expand Down
33 changes: 31 additions & 2 deletions gitstats/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,15 @@ def rev_to_date(self, rev):
return datetime.datetime.fromtimestamp(stamp).strftime("%Y-%m-%d")


def run(gitpath, outputpath) -> int:
def run(gitpath, outputpath, extra_fmt=None) -> int:
"""Run the gitstats program.
Args:
gitpath: path to the git repository
outputpath: path to the output directory
extra_fmt: extra format
Returns:
0 on success, 1 on failure
"""
rundir = os.getcwd()

try:
Expand Down Expand Up @@ -709,6 +717,18 @@ def run(gitpath, outputpath) -> int:
html_report = HTMLReportCreator()
html_report.create(data, outputpath)

if extra_fmt:
output_file = os.path.join(gitpath, f"{outputpath}.{extra_fmt}")
if extra_fmt == "json":
import json

print(f'Generating JSON file: "{output_file}"')
with open(output_file, "w") as file:
json.dump(data.__dict__, file, default=str)
else:
print(f"Error: Unsupported format '{extra_fmt}'")
return 1

time_end = time.time()
exectime_internal = time_end - time_start
print(
Expand Down Expand Up @@ -761,6 +781,14 @@ def get_parser() -> argparse.ArgumentParser:
help="Path to the directory where the output will be stored.",
)

parser.add_argument(
"-f",
"--format",
choices=["json"],
required=False,
help="The extra format of the output file.",
)

return parser


Expand All @@ -769,6 +797,7 @@ def main() -> int:
args = parser.parse_args()
gitpath = args.gitpath
outputpath = os.path.abspath(args.outputpath)
extra_fmt = args.format

for item in args.config:
try:
Expand All @@ -779,7 +808,7 @@ def main() -> int:
except ValueError:
parser.error("Config must be in the form key=value")

run(gitpath, outputpath)
run(gitpath, outputpath, extra_fmt=extra_fmt)

return 0

Expand Down

0 comments on commit 4c19dda

Please sign in to comment.