Skip to content

Fix multi remote error、add git hist and update the install.sh #4

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

Merged
merged 6 commits into from
Jan 27, 2025
Merged
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

Treat git log as a book, exec `git next` or `git prev` to checkout the next or the previous commit.

### 像翻页一样跳转到上一(n)条或下一(n)条 Git 历史节点

## How to install?

1. Clone this repository
2. Execute command : `./install.sh`

or

1. Download the `git-paging-alias.txt`
2. Execute `git config --global --add include.path PATH/git-paging-ali.txt`
Comment on lines +13 to +14
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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


## Git alias guidelines

1. `git hist [<BRANCH_NAME> or <revision-range> [<options>]]` - show all history commits
2. `git swc-first` - switch the first commit
3. `git swc-last` - switch the last commit
4. `git swc-prev` - switch previous commit
5. `git swc-next` - switch next commit

## 像翻页一样跳转到上一(n)条或下一(n)条 Git 历史节点

请查看博客文章 [阅读开源代码小技巧](https://hutusi.com/git-paging) 获得更多使用帮助。

5 changes: 3 additions & 2 deletions git-first
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh

first() {
branch=`git symbolic-ref refs/remotes/origin/HEAD`
git log --reverse --pretty=%H $branch | head -1 | xargs git checkout
REMOTE=$(git remote)
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
git log --reverse --pretty=%H "$branch" | head -1 | xargs git checkout
Comment on lines +5 to +6
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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

}
first "$@"
14 changes: 14 additions & 0 deletions git-hist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

hist() {
if test -z $1;then
REMOTE=$(git remote)
params=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
else
params="$@"
fi
Comment on lines +4 to +9
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve parameter handling and add error handling.

The parameter handling can be improved with better shell syntax.

Apply this diff:

-    if test -z $1;then
+    if [ $# -eq 0 ]; then
         REMOTE=$(git remote | head -n1)
+        if [ $(git remote | wc -l) -gt 1 ]; then
+            echo "Warning: Multiple remotes found, using '$REMOTE'" >&2
+        fi
-        params=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
+        if ! params=$(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
     else
-        params="$@"
+        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.

Suggested change
if test -z $1;then
REMOTE=$(git remote)
params=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
else
params="$@"
fi
if [ $# -eq 0 ]; then
REMOTE=$(git remote | head -n1)
if [ $(git remote | wc -l) -gt 1 ]; then
echo "Warning: Multiple remotes found, using '$REMOTE'" >&2
fi
if ! params=$(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
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 "$@"
7 changes: 4 additions & 3 deletions git-last
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh

last() {
branch=`git symbolic-ref refs/remotes/origin/HEAD`
git log --pretty=%H $branch | head -1 | xargs git checkout
REMOTE=$(git remote)
branch=${1:-$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")}
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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")}

git log -n 1 --pretty=%H "$branch" | git checkout
Comment on lines +5 to +6
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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

}
last "$@"
last "$@"
5 changes: 3 additions & 2 deletions git-next
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh

next() {
branch=`git symbolic-ref refs/remotes/origin/HEAD`
REMOTE=$(git remote)
branch=$(git symbolic-ref "refs/remotes/$REMOTE/HEAD")
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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 [ -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
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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

}
next "$@"
35 changes: 35 additions & 0 deletions git-paging-alias.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[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"
Comment on lines +2 to +15
Copy link

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.

Suggested change
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 $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)

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"
Comment on lines +16 to +20
Copy link

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

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"
Comment on lines +26 to +29
Copy link

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.

  1. Use git switch instead of git checkout for consistency with other commands
  2. 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.

Suggested change
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"

swc-next = !"next(){ \
REMOTE=\"$(git remote)\"; \
branch=\"$(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"
1 change: 0 additions & 1 deletion git-prev
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/bin/sh

prev() {
branch=`git symbolic-ref refs/remotes/origin/HEAD`
if [ -z "$1" ]; then
n=1
else
Expand Down
6 changes: 5 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#!/bin/sh
install -m 755 git-* /usr/local/bin
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
Comment on lines +2 to +6
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling and fix security issues.

The installation script has several issues that need to be addressed:

  1. The readlink command output should be quoted to prevent word splitting
  2. Installation to /usr/local/bin requires sudo privileges but there's no check
  3. 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.

Suggested change
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)