-
Notifications
You must be signed in to change notification settings - Fork 15
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
Fix multi remote error、add git hist
and update the install.sh
#4
base: master
Are you sure you want to change the base?
Conversation
…rent remote name Signed-off-by: limingshuang <[email protected]>
Signed-off-by: limingshuang <[email protected]>
Signed-off-by: limingshuang <[email protected]>
Signed-off-by: limingshuang <[email protected]>
Signed-off-by: limingshuang <[email protected]>
WalkthroughThe pull request introduces enhancements to a Git utility toolkit, focusing on improving Git navigation and configuration. The changes include updating multiple shell scripts ( Changes
Sequence DiagramsequenceDiagram
participant User
participant GitAlias
participant GitRepo
User->>GitAlias: Execute alias command
GitAlias->>GitRepo: Retrieve remote info
GitAlias->>GitRepo: Fetch commit/branch details
GitRepo-->>GitAlias: Return commit/branch info
GitAlias-->>User: Display/Switch to requested commit/branch
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 11
♻️ Duplicate comments (1)
git-first (1)
4-5
:⚠️ Potential issueAdd error handling for multiple remote repositories.
Similar to git-last, this script needs proper handling of multiple remotes.
Apply the same fix as suggested for git-last.
🧹 Nitpick comments (4)
git-hist (1)
10-12
: Improve format string readability and add shellcheck directives.The format string is hard to read and maintain. Also, adding shellcheck directives would help catch shell scripting issues.
Add shellcheck directive at the top of the file:
#!/bin/sh +# shellcheck disable=SC2086 # Allow word splitting for $params
Split the format string for better readability:
- git log --color --graph --abbrev-commit \ - --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \ - $params + format_string='%Cred%h%Creset' # commit hash + format_string+=' -%C(yellow)%d%Creset' # decorations + format_string+=' %s' # commit message + format_string+=' %Cgreen(%cr)%Creset' # relative time + format_string+=' %C(bold blue)<author:%an == commitor:%cn>%Creset' # authors + git log --color --graph --abbrev-commit \ + --pretty=format:"$format_string" \ + $paramsREADME.md (2)
24-27
: Consider translating the Chinese section to English.For better accessibility and consistency, consider translating the Chinese section to English since the rest of the documentation is in English.
8-14
: Improve command formatting and clarity.The installation instructions could be clearer:
- Use consistent command formatting
- Add example paths
- Add verification steps
Example improvement:
1. Clone this repository -2. Execute command : `./install.sh` +2. Run the installation script: + ```bash + ./install.sh + ``` or 1. Download the `git-paging-alias.txt` -2. Execute `git config --global --add include.path PATH/git-paging-ali.txt` +2. Add it to your git config: + ```bash + git config --global --add include.path /path/to/git-paging-alias.txt + ``` +3. Verify installation: + ```bash + git hist --help + ```🧰 Tools
🪛 LanguageTool
[grammar] ~9-~9: The verb ‘Execute’ is plural. Did you mean: “executes”? Did you use a verb instead of a noun?
Context: ...o install? 1. Clone this repository 2. Execute command :./install.sh
or 1. Downlo...(PLURAL_VERB_AFTER_THIS)
git-paging-alias.txt (1)
1-35
: Consider handling multiple remotes more robustly.When multiple remotes exist, the current implementation uses the first remote returned by
git remote
. This might not be the desired behavior.Consider:
- Preferring 'origin' if it exists
- Adding a configuration option for default remote
- Adding error handling for multiple remotes
I can help implement these improvements. Would you like me to generate the enhanced implementation?
🧰 Tools
🪛 LanguageTool
[style] ~2-
2: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 1090 characters long)
Context: [alias] hist = !"hist() { \ if test -z$1;then \ REMOTE="$ (git remote)"; \ params="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ else \ params="$@"; \ fi; \ git log \ --color \ --graph \ --abbrev-commit \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \$params; \ };hist" swc-first = !"first() { \ REMOTE="$ (git remote)"; \ branch="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ git switch --detach $(git log --reverse --pretty=%H "$branch" | head -1); \ };first" swc-last = !"last(){ \ REMOTE="$(git remote)"; \ branch="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ git switch --detach $(git log -n 1 --pretty=%H "$branch"); \ };last" swc-prev = !"prev(){ \ n="${1:-1}"; \ git checkout HEAD$n; \ };prev" swc-next = !"next(){ \ REMOTE="$ (git remote...(EN_EXCESSIVE_EXCLAMATION)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (8)
-
README.md
(1 hunks) -
git-first
(1 hunks) -
git-hist
(1 hunks) -
git-last
(1 hunks) -
git-next
(1 hunks) -
git-paging-alias.txt
(1 hunks) -
git-prev
(0 hunks) -
install.sh
(1 hunks)
💤 Files with no reviewable changes (1)
- git-prev
🧰 Additional context used
🪛 Shellcheck (0.10.0)
install.sh
[warning] 3-3: Quote this to prevent word splitting.
(SC2046)
🪛 LanguageTool
README.md
[grammar] ~9-~9: The verb ‘Execute’ is plural. Did you mean: “executes”? Did you use a verb instead of a noun?
Context: ...o install? 1. Clone this repository 2. Execute command : ./install.sh
or 1. Downlo...
(PLURAL_VERB_AFTER_THIS)
git-paging-alias.txt
[style] ~2-2: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 1090 characters long)
Context: [alias] hist = !"hist() { \ if test -z
(EN_EXCESSIVE_EXCLAMATION)
REMOTE=$(git remote) | ||
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for multiple remote repositories.
The git remote
command will return all configured remotes. When multiple remotes exist, this could lead to incorrect branch references.
Apply this diff to handle multiple remotes:
- REMOTE=$(git remote)
+ REMOTE=$(git remote | head -n1)
+ if [ $(git remote | wc -l) -gt 1 ]; then
+ echo "Warning: Multiple remotes found, using '$REMOTE'"
+ fi
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
REMOTE=$(git remote) | |
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} | |
REMOTE=$(git remote | head -n1) | |
if [ $(git remote | wc -l) -gt 1 ]; then | |
echo "Warning: Multiple remotes found, using '$REMOTE'" | |
fi | |
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} |
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} | ||
git log -n 1 --pretty=%H "$branch" | git checkout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling and proper quoting.
The script needs proper error handling for git commands and variable quoting.
Apply this diff to improve robustness:
- branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
- git log -n 1 --pretty=%H "$branch" | git checkout
+ if ! branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null)}; then
+ echo "Error: Failed to get default branch for remote '$REMOTE'" >&2
+ exit 1
+ fi
+ commit=$(git log -n 1 --pretty=%H "$branch") || exit 1
+ git checkout "$commit" || exit 1
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} | |
git log -n 1 --pretty=%H "$branch" | git checkout | |
if ! branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null)}; then | |
echo "Error: Failed to get default branch for remote '$REMOTE'" >&2 | |
exit 1 | |
fi | |
commit=$(git log -n 1 --pretty=%H "$branch") || exit 1 | |
git checkout "$commit" || exit 1 |
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} | ||
git log --reverse --pretty=%H "$branch" | head -1 | xargs git checkout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve error handling and command consistency.
The script needs proper error handling and consistent command usage.
Apply this diff to improve robustness:
- branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
- git log --reverse --pretty=%H "$branch" | head -1 | xargs git checkout
+ if ! branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null)}; then
+ echo "Error: Failed to get default branch for remote '$REMOTE'" >&2
+ exit 1
+ fi
+ commit=$(git log --reverse --pretty=%H -n1 "$branch") || exit 1
+ git checkout "$commit" || exit 1
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")} | |
git log --reverse --pretty=%H "$branch" | head -1 | xargs git checkout | |
if ! branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null)}; then | |
echo "Error: Failed to get default branch for remote '$REMOTE'" >&2 | |
exit 1 | |
fi | |
commit=$(git log --reverse --pretty=%H -n1 "$branch") || exit 1 | |
git checkout "$commit" || exit 1 |
if [ -z "$1" ]; then | ||
n=1 | ||
else | ||
n=$1 | ||
fi | ||
git log --reverse --pretty=%H $branch | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout | ||
git log --reverse --pretty=%H "$branch" | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for the commit lookup chain.
The complex pipe chain needs proper error handling.
Apply this diff:
- git log --reverse --pretty=%H "$branch" | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout
+ current_commit=$(git rev-parse HEAD) || exit 1
+ next_commit=$(git log --reverse --pretty=%H "$branch" | grep -A "$n" "$current_commit" | tail -1) || exit 1
+ if [ -z "$next_commit" ]; then
+ echo "Error: No next commit found" >&2
+ exit 1
+ fi
+ git checkout "$next_commit" || exit 1
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
git log --reverse --pretty=%H "$branch" | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout | |
current_commit=$(git rev-parse HEAD) || exit 1 | |
next_commit=$(git log --reverse --pretty=%H "$branch" | grep -A "$n" "$current_commit" | tail -1) || exit 1 | |
if [ -z "$next_commit" ]; then | |
echo "Error: No next commit found" >&2 | |
exit 1 | |
fi | |
git checkout "$next_commit" || exit 1 |
REMOTE=$(git remote) | ||
branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation and add error handling for multiple remotes.
The script has inconsistent indentation and needs proper handling of multiple remotes.
Apply this diff:
- REMOTE=$(git remote)
- branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")
+ REMOTE=$(git remote | head -n1)
+ if [ $(git remote | wc -l) -gt 1 ]; then
+ echo "Warning: Multiple remotes found, using '$REMOTE'" >&2
+ fi
+ if ! branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null); then
+ echo "Error: Failed to get default branch for remote '$REMOTE'" >&2
+ exit 1
+ fi
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
REMOTE=$(git remote) | |
branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD") | |
REMOTE=$(git remote | head -n1) | |
if [ $(git remote | wc -l) -gt 1 ]; then | |
echo "Warning: Multiple remotes found, using '$REMOTE'" >&2 | |
fi | |
if ! branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD" 2>/dev/null); then | |
echo "Error: Failed to get default branch for remote '$REMOTE'" >&2 | |
exit 1 | |
fi |
if test -f git-paging-alias.txt;then | ||
git config --global --add include.path $(readlink -f git-paging-alias.txt) | ||
else | ||
install -m 755 git-* /usr/local/bin | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling and fix security issues.
The installation script has several issues that need to be addressed:
- The
readlink
command output should be quoted to prevent word splitting - Installation to
/usr/local/bin
requires sudo privileges but there's no check - Missing error handling for the
git config
command
Apply this diff to fix the issues:
#!/bin/sh
-if test -f git-paging-alias.txt;then
- git config --global --add include.path $(readlink -f git-paging-alias.txt)
+if test -f git-paging-alias.txt; then
+ if ! git config --global --add include.path "$(readlink -f git-paging-alias.txt)"; then
+ echo "Error: Failed to add git-paging-alias.txt to git config"
+ exit 1
+ fi
else
- install -m 755 git-* /usr/local/bin
+ if [ "$(id -u)" -ne 0 ]; then
+ echo "Error: Please run with sudo when installing to /usr/local/bin"
+ exit 1
+ fi
+ if ! install -m 755 git-* /usr/local/bin; then
+ echo "Error: Failed to install git-* files"
+ exit 1
+ fi
fi
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if test -f git-paging-alias.txt;then | |
git config --global --add include.path $(readlink -f git-paging-alias.txt) | |
else | |
install -m 755 git-* /usr/local/bin | |
fi | |
#!/bin/sh | |
if test -f git-paging-alias.txt; then | |
if ! git config --global --add include.path "$(readlink -f git-paging-alias.txt)"; then | |
echo "Error: Failed to add git-paging-alias.txt to git config" | |
exit 1 | |
fi | |
else | |
if [ "$(id -u)" -ne 0 ]; then | |
echo "Error: Please run with sudo when installing to /usr/local/bin" | |
exit 1 | |
fi | |
if ! install -m 755 git-* /usr/local/bin; then | |
echo "Error: Failed to install git-* files" | |
exit 1 | |
fi | |
fi |
🧰 Tools
🪛 Shellcheck (0.10.0)
[warning] 3-3: Quote this to prevent word splitting.
(SC2046)
1. Download the `git-paging-alias.txt` | ||
2. Execute `git config --global --add include.path PATH/git-paging-ali.txt` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix incorrect file name in the installation instructions.
The file name in the git config command is incorrect:
- Current:
git-paging-ali.txt
- Should be:
git-paging-alias.txt
swc-first = !"first() { \ | ||
REMOTE=\"$(git remote)\"; \ | ||
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | ||
git switch --detach $(git log --reverse --pretty=%H \"$branch\" | head -1); \ | ||
};first" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add consistent error handling to all navigation functions.
The swc-first
, swc-last
, and swc-prev
functions should also include error handling for Git commands.
I can help implement error handling for these functions. Would you like me to generate the improved implementations?
Also applies to: 21-25, 26-29
git-paging-alias.txt
Outdated
swc-next = !"next(){ \ | ||
REMOTE=\"$(git remote)\"; \ | ||
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | ||
n=\"${1:-1}\"; \ | ||
git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ | ||
};next" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix parameter handling in swc-next function.
The swc-next
function has conflicting usage of $1
:
- Line 32:
$1
is used for branch parameter - Line 33: Same
$1
is used for the number of commits
Apply this diff to fix the parameter handling:
swc-next = !"next(){ \
REMOTE=\"$(git remote)\"; \
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \
- n=\"${1:-1}\"; \
+ n=\"${2:-1}\"; \
git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \
};next"
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
swc-next = !"next(){ \ | |
REMOTE=\"$(git remote)\"; \ | |
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | |
n=\"${1:-1}\"; \ | |
git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ | |
};next" | |
swc-next = !"next(){ \ | |
REMOTE=\"$(git remote)\"; \ | |
branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | |
n=\"${2:-1}\"; \ | |
git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ | |
};next" |
hist = !"hist() { \ | ||
if test -z $1;then \ | ||
REMOTE=\"$(git remote)\"; \ | ||
params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | ||
else \ | ||
params=\"$@\"; \ | ||
fi; \ | ||
git log \ | ||
--color \ | ||
--graph \ | ||
--abbrev-commit \ | ||
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \ | ||
$params; \ | ||
};hist" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling to hist function.
The hist
function should handle errors from git commands and validate the remote existence.
Apply this diff to add error handling:
hist = !"hist() { \
if test -z $1;then \
- REMOTE=\"$(git remote)\"; \
+ REMOTE=\"$(git remote 2>/dev/null)\"; \
+ if [ -z \"$REMOTE\" ]; then \
+ echo \"Error: No remote found\"; \
+ return 1; \
+ fi; \
params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \
+ if [ $? -ne 0 ]; then \
+ echo \"Error: Failed to get remote HEAD reference\"; \
+ return 1; \
+ fi; \
else \
params=\"$@\"; \
fi; \
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
hist = !"hist() { \ | |
if test -z $1;then \ | |
REMOTE=\"$(git remote)\"; \ | |
params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | |
else \ | |
params=\"$@\"; \ | |
fi; \ | |
git log \ | |
--color \ | |
--graph \ | |
--abbrev-commit \ | |
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \ | |
$params; \ | |
};hist" | |
hist = !"hist() { \ | |
if test -z $1;then \ | |
REMOTE=\"$(git remote 2>/dev/null)\"; \ | |
if [ -z \"$REMOTE\" ]; then \ | |
echo \"Error: No remote found\"; \ | |
return 1; \ | |
fi; \ | |
params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ | |
if [ $? -ne 0 ]; then \ | |
echo \"Error: Failed to get remote HEAD reference\"; \ | |
return 1; \ | |
fi; \ | |
else \ | |
params=\"$@\"; \ | |
fi; \ | |
git log \ | |
--color \ | |
--graph \ | |
--abbrev-commit \ | |
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \ | |
$params; \ | |
};hist" |
🧰 Tools
🪛 LanguageTool
[style] ~2-2: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 1090 characters long)
Context: [alias] hist = !"hist() { \ if test -z
(EN_EXCESSIVE_EXCLAMATION)
Signed-off-by: MinsonLee <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (4)
git-paging-alias.txt (4)
2-15
:⚠️ Potential issueAdd error handling and fix parameter inconsistency.
- The
$1
parameter is used inconsistently:
- Line 3 checks if
$1
is empty- Line 5 uses
$1
as a fallback in parameter expansion- There's a typo in the format string: "commitor" should be "committer"
Apply this diff to fix the issues:
hist = !"hist() { \ if test -z $1;then \ - REMOTE=\"$(git remote)\"; \ + REMOTE=\"$(git remote 2>/dev/null)\"; \ + if [ -z \"$REMOTE\" ]; then \ + echo \"Error: No remote found\" >&2; \ + return 1; \ + fi; \ params=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + if [ $? -ne 0 ]; then \ + echo \"Error: Failed to get remote HEAD reference\" >&2; \ + return 1; \ + fi; \ else \ params=\"$@\"; \ fi; \ git log \ --color \ --graph \ --abbrev-commit \ - --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \ + --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == committer:%cn>%Creset' \ $params; \ };hist"🧰 Tools
🪛 LanguageTool
[style] ~2-
2: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 1084 characters long)
Context: [alias] hist = !"hist() { \ if test -z$1;then \ REMOTE="$ (git remote)"; \ params="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ else \ params="$@"; \ fi; \ git log \ --color \ --graph \ --abbrev-commit \ --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<author:%an == commitor:%cn>%Creset' \$params; \ };hist" swc-first = !"first() { \ REMOTE="$ (git remote)"; \ branch="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ git switch --detach $(git log --reverse --pretty=%H "$branch" | head -1); \ };first" swc-last = !"last(){ \ REMOTE="$(git remote)"; \ branch="${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}"; \ git switch --detach $(git log -n 1 --pretty=%H "$branch"); \ };last" swc-prev = !"prev(){ \ n="${1:-1}"; \ git checkout HEAD$n; \ };prev" swc-next = !"next(){ \ REMOTE="$ (git remote...(EN_EXCESSIVE_EXCLAMATION)
16-20
:⚠️ Potential issueAdd error handling for Git commands.
The function should handle errors from git commands and validate the remote existence.
Apply this diff to add error handling:
swc-first = !"first() { \ - REMOTE=\"$(git remote)\"; \ + REMOTE=\"$(git remote 2>/dev/null)\"; \ + if [ -z \"$REMOTE\" ]; then \ + echo \"Error: No remote found\" >&2; \ + return 1; \ + fi; \ branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + if [ $? -ne 0 ]; then \ + echo \"Error: Failed to get remote HEAD reference\" >&2; \ + return 1; \ + fi; \ + commit_hash=$(git log --reverse --pretty=%H \"$branch\" | head -1); \ + if [ -z \"$commit_hash\" ]; then \ + echo \"Error: Failed to get first commit hash\" >&2; \ + return 1; \ + fi; \ - git switch --detach $(git log --reverse --pretty=%H \"$branch\" | head -1); \ + git switch --detach \"$commit_hash\"; \ };first"
21-25
:⚠️ Potential issueAdd error handling for Git commands.
The function should handle errors from git commands and validate the remote existence.
Apply this diff to add error handling:
swc-last = !"last(){ \ - REMOTE=\"$(git remote)\"; \ + REMOTE=\"$(git remote 2>/dev/null)\"; \ + if [ -z \"$REMOTE\" ]; then \ + echo \"Error: No remote found\" >&2; \ + return 1; \ + fi; \ branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + if [ $? -ne 0 ]; then \ + echo \"Error: Failed to get remote HEAD reference\" >&2; \ + return 1; \ + fi; \ + commit_hash=$(git log -n 1 --pretty=%H \"$branch\"); \ + if [ -z \"$commit_hash\" ]; then \ + echo \"Error: Failed to get last commit hash\" >&2; \ + return 1; \ + fi; \ - git switch --detach $(git log -n 1 --pretty=%H \"$branch\"); \ + git switch --detach \"$commit_hash\"; \ };last"
30-35
:⚠️ Potential issueFix parameter handling and add error checking.
- The branch parameter is hardcoded and doesn't accept user input
- Multiple git commands in the pipeline could fail silently
Apply this diff to fix the issues:
swc-next = !"next(){ \ - REMOTE=\"$(git remote)\"; \ + REMOTE=\"$(git remote 2>/dev/null)\"; \ + if [ -z \"$REMOTE\" ]; then \ + echo \"Error: No remote found\" >&2; \ + return 1; \ + fi; \ - branch=\"$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")\"; \ + branch=\"${1:-$(git symbolic-ref \"refs/remotes/$REMOTE/HEAD\")}\"; \ + if [ $? -ne 0 ]; then \ + echo \"Error: Failed to get remote HEAD reference\" >&2; \ + return 1; \ + fi; \ - n=\"${1:-1}\"; \ + n=\"${2:-1}\"; \ + if ! [[ \"$n\" =~ ^[0-9]+$ ]]; then \ + echo \"Error: Invalid number of commits\" >&2; \ + return 1; \ + fi; \ + current_hash=$(git rev-parse HEAD); \ + if [ $? -ne 0 ]; then \ + echo \"Error: Failed to get current commit hash\" >&2; \ + return 1; \ + fi; \ + next_hash=$(git log --reverse --pretty=%H \"$branch\" | grep -A $n \"$current_hash\" | tail -1); \ + if [ -z \"$next_hash\" ]; then \ + echo \"Error: No next commit found\" >&2; \ + return 1; \ + fi; \ - git switch --detach $(git log --reverse --pretty=%H \"$branch\" | grep -A $n $(git rev-parse HEAD) | tail -1); \ + git switch --detach \"$next_hash\"; \ };next"
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
-
git-paging-alias.txt
(1 hunks)
🧰 Additional context used
🪛 LanguageTool
git-paging-alias.txt
[style] ~2-2: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 1084 characters long)
Context: [alias] hist = !"hist() { \ if test -z
(EN_EXCESSIVE_EXCLAMATION)
swc-prev = !"prev(){ \ | ||
n=\"${1:-1}\"; \ | ||
git checkout HEAD~$n; \ | ||
};prev" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use consistent Git commands and add parameter validation.
- Use
git switch
instead ofgit checkout
for consistency with other commands - Add validation for the
n
parameter
Apply this diff to improve the implementation:
swc-prev = !"prev(){ \
n=\"${1:-1}\"; \
- git checkout HEAD~$n; \
+ if ! [[ \"$n\" =~ ^[0-9]+$ ]]; then \
+ echo \"Error: Invalid number of commits\" >&2; \
+ return 1; \
+ fi; \
+ git switch --detach HEAD~$n; \
};prev"
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
swc-prev = !"prev(){ \ | |
n=\"${1:-1}\"; \ | |
git checkout HEAD~$n; \ | |
};prev" | |
swc-prev = !"prev(){ \ | |
n=\"${1:-1}\"; \ | |
if ! [[ \"$n\" =~ ^[0-9]+$ ]]; then \ | |
echo \"Error: Invalid number of commits\" >&2; \ | |
return 1; \ | |
fi; \ | |
git switch --detach HEAD~$n; \ | |
};prev" |
install.sh
,使用 git alias 方式来统一管理别名命令Summary by CodeRabbit
Documentation
New Features
hist
: Detailed Git log displayswc-first
: Switch to first commitswc-last
: Switch to last commitswc-prev
: Navigate to previous commitsswc-next
: Navigate to next commitsChores