From ba344d0b3c05236f8b45f964245afbd4c471a46d Mon Sep 17 00:00:00 2001 From: vanpipy Date: Fri, 3 Nov 2023 15:29:23 +0800 Subject: [PATCH] refactor(git-authors): add unit test for option `--output` * doc(git-authors): update option `--output` usage --- bin/git-authors | 83 +++++++++++++++++++++++++------------------ man/git-authors.1 | 29 ++++++++++++++- man/git-authors.html | 29 ++++++++++++++- man/git-authors.md | 26 ++++++++++++++ tests/test_authors.py | 33 +++++++++++++++++ 5 files changed, 164 insertions(+), 36 deletions(-) diff --git a/bin/git-authors b/bin/git-authors index baa95ee7c..38b7967c0 100755 --- a/bin/git-authors +++ b/bin/git-authors @@ -1,40 +1,20 @@ #!/usr/bin/env bash -LIST=false -NO_EMAIL=false -FILE="" +list=0 +no_email=0 +output="" +file="" -while [[ $# -gt 0 ]]; do - case $1 in - -l|--list ) - LIST=true - shift - ;; - --no-email ) - NO_EMAIL=true - shift - ;; - * ) - break - esac -done - -if ! $LIST; then - FILE=$1 - if [ -z "$FILE" ]; then - FILE=$(find . -mindepth 1 -maxdepth 1 \( -iname '*authors*' -o -iname '*contributors*' \) | head -n1) - if [ -z "$FILE" ]; then - FILE='AUTHORS' - fi - fi +if [[ $# = 1 ]] && [[ "$1" = "--no-email" ]]; then + echo >&2 "--no-email option only can be used with --list | -l | --output" + exit 1 fi # # list authors sorted by number of commits (descending). # - authors() { - if $NO_EMAIL; then + if [[ $no_email = 1 ]]; then # email will be used to uniq authors. git shortlog HEAD -sne | awk '{$1=""; sub(" ", ""); print}' | awk -F'<' '!x[$1]++' | awk -F'<' '!x[$2]++' \ | awk -F'<' '{gsub(/ +$/, "", $1); print $1}' @@ -43,12 +23,47 @@ authors() { fi } -# -# authors. -# +if [[ -z "$file" ]]; then + file=$(find . -mindepth 1 -maxdepth 1 -iregex '.*\(authors\|contributors\).*' | head -n1) + if [[ -z "$file" ]]; then + file="AUTHORS" + fi +fi + +if [[ $# = 0 ]]; then + authors >> "$file" + exit 0 +fi + +while [[ $# -gt 0 ]]; do + case $1 in + -l|--list ) + list=1 + ;; + --no-email ) + no_email=1 + ;; + --output ) + if [[ -n $2 ]] && [[ $2 =~ ^[a-zA-Z] ]]; then + output=$2 + shift + else + echo >&2 "option $1 requires a letter prefix value" + exit 1 + fi + ;; + * ) + break + esac + shift +done + +if [[ -n "$output" ]]; then + authors >> "$output" + exit 0 +fi -if $LIST; then +if [[ $list = 1 ]]; then authors -else - authors >> "$FILE" + exit 0 fi diff --git a/man/git-authors.1 b/man/git-authors.1 index 3169dfa5c..b29a1424c 100644 --- a/man/git-authors.1 +++ b/man/git-authors.1 @@ -4,7 +4,7 @@ .SH "NAME" \fBgit\-authors\fR \- Generate authors report .SH "SYNOPSIS" -\fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email] +\fBgit\-authors\fR \fBgit\-authors\fR [\-l, \-\-list] [\-\-no\-email] \fBgit\-authors\fR [\-\-output] [\-\-no\-email] .SH "DESCRIPTION" .TS allbox; @@ -20,6 +20,14 @@ Show authors\. \-\-no\-email .P Don\'t show authors\' email\. +.P +\-\-output +.P +output the authors +.P + +.P +the filename which is letter prefix to save the authors .SH "EXAMPLES" Updating AUTHORS file: .IP "" 4 @@ -51,6 +59,25 @@ nickl\- Leila Muhtasib .fi .IP "" 0 +.P +Output authors +.IP "" 4 +.nf +$ git authors \-\-output authors\-history || cat authors\-history +TJ Holowaychuk +hemanth\.hm +Jonhnny Weslley +nickl\- +Leila Muhtasib + +$ git authors \-\-output authors\-history \-\-no\-email || cat authors\-history +TJ Holowaychuk +hemanth\.hm +Jonhnny Weslley +nickl\- +Leila Muhtasib +.fi +.IP "" 0 .SH "AUTHOR" Written by Titus Wormer <\fItituswormer@gmail\.com\fR> .SH "REPORTING BUGS" diff --git a/man/git-authors.html b/man/git-authors.html index 697fef566..70fd8ba6b 100644 --- a/man/git-authors.html +++ b/man/git-authors.html @@ -77,7 +77,9 @@

NAME

SYNOPSIS

-

git-authors [-l, --list] [--no-email]

+

git-authors +git-authors [-l, --list] [--no-email] +git-authors [--output] <filename> [--no-email]

DESCRIPTION

@@ -102,6 +104,14 @@

OPTIONS

Don't show authors' email.

+

--output

+ +

output the authors

+ +

<filename>

+ +

the filename which is letter prefix to save the authors

+

EXAMPLES

Updating AUTHORS file:

@@ -129,6 +139,23 @@

EXAMPLES

Leila Muhtasib +

Output authors

+ +
$ git authors --output authors-history || cat authors-history
+TJ Holowaychuk <tj@vision-media.ca>
+hemanth.hm <hemanth.hm@gmail.com>
+Jonhnny Weslley <jw@jonhnnyweslley.net>
+nickl- <github@jigsoft.co.za>
+Leila Muhtasib <muhtasib@gmail.com>
+
+$ git authors --output authors-history --no-email || cat authors-history
+TJ Holowaychuk
+hemanth.hm
+Jonhnny Weslley
+nickl-
+Leila Muhtasib
+
+

AUTHOR

Written by Titus Wormer <tituswormer@gmail.com>

diff --git a/man/git-authors.md b/man/git-authors.md index 50bcd3599..e43edf04b 100644 --- a/man/git-authors.md +++ b/man/git-authors.md @@ -3,7 +3,9 @@ git-authors(1) -- Generate authors report ## SYNOPSIS +`git-authors` `git-authors` [-l, --list] [--no-email] +`git-authors` [--output] <filename> [--no-email] ## DESCRIPTION @@ -21,6 +23,14 @@ git-authors(1) -- Generate authors report Don't show authors' email. + --output + + output the authors + + <filename> + + the filename which is letter prefix to save the authors + ## EXAMPLES Updating AUTHORS file: @@ -45,6 +55,22 @@ git-authors(1) -- Generate authors report nickl- Leila Muhtasib + Output authors + + $ git authors --output authors-history || cat authors-history + TJ Holowaychuk + hemanth.hm + Jonhnny Weslley + nickl- + Leila Muhtasib + + $ git authors --output authors-history --no-email || cat authors-history + TJ Holowaychuk + hemanth.hm + Jonhnny Weslley + nickl- + Leila Muhtasib + ## AUTHOR Written by Titus Wormer <> diff --git a/tests/test_authors.py b/tests/test_authors.py index 8eb3cf9ac..0c3d63fb0 100644 --- a/tests/test_authors.py +++ b/tests/test_authors.py @@ -43,3 +43,36 @@ def test_list_authors_has_not_email(self, temp_repo): actual = temp_repo.invoke_extras_command("authors", "-l", "--no-email") actual = actual.stdout.decode() assert actual == expected_authors_list_without_email + + def test_output_authors_has_email_into_AUTHORS(self, temp_repo): + git = temp_repo.get_repo_git() + temp_repo.invoke_extras_command("authors", "--output") + with open(authors_file) as f: + content = f.read() + assert content == expected_authors_list + + def test_output_authors_has_email_into_target_file(self, temp_repo): + git = temp_repo.get_repo_git() + temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_A") + with open("AUTHORS_TARGET_FILE_A") as f: + content = f.read() + assert content == expected_authors_list + + def test_output_authors_has_not_email_into_target_file(self, temp_repo): + git = temp_repo.get_repo_git() + rs = temp_repo.invoke_extras_command("authors", "--output", "AUTHORS_TARGET_FILE_B", "--no-email") + with open("AUTHORS_TARGET_FILE_B") as f: + content = f.read() + assert content == expected_authors_list_without_email + + def test_fail_to_output_authors_when_an_option_like_follow_output_parameter(self, temp_repo): + git = temp_repo.get_repo_git() + actual = temp_repo.invoke_extras_command("authors", "--output", "--no-email") + actual = actual.stderr.decode() + assert actual == "option --output requires a value\n" + + def test_fail_to_output_authors_when_only_no_email_option(self, temp_repo): + git = temp_repo.get_repo_git() + actual = temp_repo.invoke_extras_command("authors", "--no-email") + actual = actual.stderr.decode() + assert actual == "--no-email option only can be used with --list | -l | --output\n"