Skip to content

Commit 53fef62

Browse files
committed
feat(cli): add "gas log" to display commit history
1 parent 13ed0cd commit 53fef62

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tensorbay/cli/cli.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,5 +345,28 @@ def tag(obj: Dict[str, str], tbrn: str) -> None:
345345
_implement_tag(obj, tbrn)
346346

347347

348+
@cli.command()
349+
@click.argument("tbrn", type=str)
350+
@click.option("-n", "--max-count", type=int, help="Limit the max number of commits to be showed")
351+
@click.option("--oneline", is_flag=True, help="Limit commit message to oneline")
352+
@click.pass_obj
353+
def log( # pylint: disable=too-many-arguments
354+
obj: Dict[str, str], tbrn: str, max_count: int, oneline: bool,
355+
) -> None:
356+
"""Display the log.\f
357+
358+
Arguments:
359+
obj: A dict contains config information.
360+
tbrn: The dataset to be showed.
361+
max_count: Max number of commits.
362+
is_all: Whether to show all commits of all branches.
363+
oneline: Whether to show a commit message in oneline.
364+
graph: Whether to plot the text-based graphical commits
365+
""" # noqa: D301,D415
366+
from .log import _implement_log
367+
368+
_implement_log(obj, tbrn, max_count, oneline)
369+
370+
348371
if __name__ == "__main__":
349372
cli() # pylint: disable=no-value-for-parameter

tensorbay/cli/log.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright 2021 Graviti. Licensed under MIT License.
4+
#
5+
6+
"""Implementation of gas log."""
7+
8+
import sys
9+
from datetime import datetime
10+
from typing import Dict
11+
12+
import click
13+
14+
from ..client.struct import Commit
15+
from .tbrn import TBRN
16+
from .utility import get_dataset_client, get_gas
17+
18+
19+
def _convert_to_commit_log(commit: Commit, oneline: bool) -> str:
20+
commit_id = commit.commit_id
21+
author = commit.committer.name
22+
date = datetime.fromtimestamp(commit.committer.date)
23+
message = commit.message
24+
25+
if oneline:
26+
commit_log = f"{commit_id[:7]} {message}"
27+
else:
28+
commit_log = (
29+
f"commit {commit_id}\n"
30+
f"Author: {author}\n"
31+
f"Date: {date.strftime('%a %b %d %H:%M:%S %Y')}\n\n {message}\n"
32+
)
33+
34+
return commit_log
35+
36+
37+
def _implement_log( # pylint: disable=too-many-arguments
38+
obj: Dict[str, str], tbrn: str, max_count: int, oneline: bool,
39+
) -> None:
40+
gas = get_gas(**obj)
41+
42+
if not tbrn:
43+
click.echo("Must specify the dataset to be showed")
44+
sys.exit(1)
45+
46+
info = TBRN(tbrn=tbrn)
47+
dataset_client = get_dataset_client(gas, info)
48+
49+
commit_list = dataset_client.list_commits(info.revision)[:max_count]
50+
51+
click.echo_via_pager([_convert_to_commit_log(commit, oneline) for commit in commit_list])

0 commit comments

Comments
 (0)