aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--.config/git/config8
-rw-r--r--.gitignore1
-rw-r--r--TODO3
-rwxr-xr-xsetup.sh41
-rw-r--r--src/gitalias/git-sw.go47
5 files changed, 93 insertions, 7 deletions
diff --git a/.config/git/config b/.config/git/config
index 9036d3e..6e795b8 100644
--- a/.config/git/config
+++ b/.config/git/config
@@ -62,13 +62,7 @@
fixup = commit --fixup
amend = commit --amend --no-edit
clone1 = clone --depth=1
- sw = "!__fn() { \
- if [[ \"$1\" == origin/* || \"$1\" == upstream/* ]]; then \
- git switch -d \"$@\"; \
- else \
- git switch \"$@\"; \
- fi; \
- }; __fn"
+ sw = "!~/bin/gitalias/git-sw"
swc = switch -c
swm = "!__fn() { \
if git rev-parse --verify --quiet main > /dev/null 2>&1; then \
diff --git a/.gitignore b/.gitignore
index 17d629b..bd48d50 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
/.config/alacritty/alacritty.local.yml
/.config/emacs
/.config/newsboat/urls
+/bin/gitalias/git-*
/nvim-startup.log
diff --git a/TODO b/TODO
index 66c2040..4a3dbe7 100644
--- a/TODO
+++ b/TODO
@@ -3,3 +3,6 @@ newsboat
skk
Share jisyo file between machines without opening the content
+
+golang
+ Add golang to INSTALL.txt
diff --git a/setup.sh b/setup.sh
index 7c44391..e41f8be 100755
--- a/setup.sh
+++ b/setup.sh
@@ -1,5 +1,26 @@
#!/bin/bash
+# Requirements. {{{1
+
+ok=1
+for exe in \
+ curl \
+ git \
+ go \
+ gunzip \
+ ; \
+do
+ if which "$exe" >/dev/null; then
+ :
+ else
+ echo "error: $exe is missing" >&2
+ ok=0
+ fi
+done
+if [[ $ok = 0 ]]; then
+ exit 1
+fi
+
# Configurations. {{{1
# Make symlinks to dot files. {{{2
@@ -38,6 +59,21 @@ do
fi
done
+# Tools. {{{1
+
+# Golang: {{{2
+for name in \
+ gitalias/git-sw \
+ ; \
+do
+ src_file="src/$name.go"
+ bin_file="bin/$name"
+ if [[ "$bin_file" -ot "$src_file" ]]; then
+ echo "build: $bin_file"
+ go build -o "$bin_file" "$src_file"
+ fi
+done
+
# Scripts. {{{1
# Make ~/bin directory. {{{2
@@ -45,10 +81,15 @@ if [ ! -d ~/bin ]; then
echo "dir: ~/bin"
mkdir ~/bin
fi
+if [ ! -d ~/bin/gitalias ]; then
+ echo "dir: ~/bin/gitalias"
+ mkdir ~/bin/gitalias
+fi
# Make symlinks to utility scripts. {{{2
for name in \
tmux-pane-idx \
+ gitalias/git-sw \
; \
do
if [ ! -L ~/bin/"$name" ]; then
diff --git a/src/gitalias/git-sw.go b/src/gitalias/git-sw.go
new file mode 100644
index 0000000..f4cab21
--- /dev/null
+++ b/src/gitalias/git-sw.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "log"
+ "os"
+ "os/exec"
+ "strings"
+)
+
+func main() {
+ gitArgs := []string{"switch"}
+ if requiresDetachFlag(os.Args) {
+ gitArgs = append(gitArgs, "--detach")
+ }
+ for i, argv := range os.Args {
+ if i == 0 {
+ continue // argv[0] is a program name.
+ }
+ gitArgs = append(gitArgs, argv)
+ }
+
+ cmd := exec.Command("git", gitArgs...)
+ cmd.Stdin = os.Stdin
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+
+ err := cmd.Run()
+ if err != nil {
+ switch err.(type) {
+ case *exec.ExitError:
+ // Do nothing here because Git has already reported the error.
+ default:
+ log.Fatal(err)
+ }
+ }
+
+ os.Exit(cmd.ProcessState.ExitCode())
+}
+
+func requiresDetachFlag(argv []string) bool {
+ argc := len(argv)
+ if argc == 1 {
+ return false
+ }
+ firstArg := argv[1]
+ return strings.HasPrefix(firstArg, "origin/") || strings.HasPrefix(firstArg, "upstream/")
+}