Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the 'git-info' command #755

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,26 @@ If you wish to omit the config section, you may use `--no-config`:
$ git info --no-config
```

By default, the output is colored to make it easier on the eyes. When this is not desired (if piping the output of `git info` to another command, for example), the `--no-color` option may be used:

```bash
$ git info --no-color
```

You can select the number of previous commits to show by using the `-c <N>` option (where `<N>` is any integer). It is important to note, however, that `-c` **must be the FIRST option** you pass to `git info`. For example:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use a while loop to get rid of this limitation, like: https://github.com/tj/git-extras/blob/master/bin/git-release#L31


```bash
$ git info -c 5 --no-config
```

will display the 5 most recent commits as apart of its output (and omit the configuration section), but

```bash
$ git info --no-config -c 5
```

will NOT recognize that the `-c` option was used. Consequently, the output will contain only the single most recent commit (the default action).

## git create-branch

Create local branch `name`:
Expand Down
77 changes: 47 additions & 30 deletions bin/git-info
Original file line number Diff line number Diff line change
@@ -1,47 +1,64 @@
#!/usr/bin/env bash

get_config() {
git config --list
}
clear

most_recent_commit() {
git log --max-count=1 --pretty=short
}

local_branches() {
git branch
}
max_count=1
if [[ "$1" == "-c" ]]; then
shift
max_count="$1"; shift
fi

remote_branches() {
git branch -r
}
color="--color=always"
no_color="--no-color"
if [[ "$1" == "${no_color}" ]]; then
color="$1"; shift
else
if [[ "$2" == "${no_color}" ]]; then
color="$2"
fi
fi

remote_urls() {
git remote -v

header() {
if [[ "${color}" != "${no_color}" ]]; then
tput setaf 242
fi

printf "### $1\n"

if [[ "${color}" != "${no_color}" ]]; then
tput sgr0
fi
}

echon() {
echo "$@"
echo
git_cmd() {
eval "$1"
printf "\n"
}


# Show info similar to svn

echo
echon "## Remote URLs:"
echon "$(remote_urls)"
header "Remote URLs:"
git_cmd "git remote -v"

header "Remote Branches:"
git_cmd "git branch -r ${color}"

echon "## Remote Branches:"
echon "$(remote_branches)"
header "Local Branches:"
git_cmd "git branch ${color}"

echon "## Local Branches:"
echon "$(local_branches)"
if [[ "${max_count}" -ne 1 ]]; then
header="Top ${max_count} Commits:"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, Top N Commits: is vague. Actually we show the last N commits in the current branch.

else
header="Most Recent Commit:"
fi

echon "## Most Recent Commit:"
echon "$(most_recent_commit)"
echon "Type 'git log' for more commits, or 'git show <commit id>' for full commit details."
header "${header}"
git_cmd "git --no-pager log --max-count=${max_count} --pretty=short ${color}"

if test "$1" != "--no-config"; then
echon "## Configuration (.git/config):"
echon "$(get_config)"
if [[ "$1" != "--no-config" ]]; then
header "Configuration (.git/config):"
git --no-pager config --list
fi