-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathpull
More file actions
executable file
·157 lines (136 loc) · 4.29 KB
/
Copy pathpull
File metadata and controls
executable file
·157 lines (136 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
#
# Pull remote changes using rebase and try to rebundle, safely stashing and re-applying your local changes, if any.
#
# - Pull remote changes:
#
# `pull`
#
# ---
# Based on git-friendly: https://github.com/git-friendly/git-friendly
#
# Author: Nick Plekhanov, https://plekhanov.me
# License: MIT
# https://github.com/nicksp/dotfiles
# Abort if this isn't a git repository
git rev-parse --is-inside-work-tree > /dev/null || exit $?
# Colors
color_error="$(tput sgr 0 1)$(tput setaf 1)"
color_reset="$(tput sgr0)"
# Pop any stashed changes
unstash() {
if [ -n "$stashed" ]; then
echo
echo "🍯 Restoring tree from stash…"
git stash pop
fi
}
# Pop any stashed changes and exit
rollback() {
echo
echo "${color_error}Something went wrong, rolling back…${color_reset}"
unstash
exit $1
}
# Test whether a command exists
# $1 - cmd to test
cmd_exists() {
command -v "$1" > /dev/null 2>&1
}
# Test whether a file exists
# $1 - file to test
file_exists() {
if [[ -r "./$base_dir$1" ]]; then
return 0
fi
return 1
}
# Go to directory of changed file
# $1 - filename
change_dir() {
file=$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | grep "$1")
if [[ -e "./$base_dir$file" ]]; then
cd "$(dirname "./$base_dir$file")" || return 1
return 0
fi
return 1
}
# Go back to
reset_dir() {
cd "$current_dir" || return 1
}
# Test whether a file has changed since the pre-pull HEAD
# $1 - filename
has_changed() {
# ORIG_HEAD is the value of HEAD before the pull.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD 2> /dev/null)"
echo "$changed_files" | grep --quiet "$1"
}
branch=$(git branch --show-current) || exit $?
if [ -z "$branch" ]; then
echo "${color_error}❌ You're not on a branch (detached HEAD). Check out a branch first.${color_reset}" >&2
exit 1
fi
default_remote="origin"
remote=$(git config "branch.${branch}.remote" || echo "$default_remote")
remote_branch=$( (git config "branch.${branch}.merge" || echo "refs/heads/$branch") | cut -d/ -f3-)
# Stash any local changes, including untracked files.
# Compare refs/stash before/after to know if anything was actually stashed —
# more robust than parsing the human-readable output (which is localized).
stash_before=$(git rev-parse --verify --quiet refs/stash || true)
git stash --include-untracked
stash_after=$(git rev-parse --verify --quiet refs/stash || true)
stashed=
[ "$stash_before" != "$stash_after" ] && stashed=1
# Fetch all branches (and prune stale ones) so remote-tracking refs
# stay complete — `git pull <remote> <branch>` alone only updates the
# one ref you name. Skip submodules here; the recursive pull below owns
# submodule updates, so recursing now just double-fetches them.
echo "🚀 Fetching from ${remote}…"
git fetch --prune --no-recurse-submodules --jobs=10 "$remote" || rollback $?
# Pull, using rebase
git pull --rebase --recurse-submodules --jobs=10 $remote $remote_branch || rollback $?
# Pop any stashed changes
unstash
# JS packages — each block fires when its own lockfile is present;
# npm is the fallback used only when no JS lockfile exists at all.
has_js_lockfile() {
file_exists 'yarn.lock' || file_exists 'pnpm-lock.yaml' \
|| file_exists 'bun.lock' || file_exists 'bun.lockb'
}
if cmd_exists 'yarn' \
&& file_exists 'yarn.lock' && (has_changed 'yarn.lock' || has_changed 'package.json'); then
change_dir 'yarn.lock' || change_dir 'package.json'
echo
echo '⚔ Installing packages with yarn…'
yarn install
reset_dir
fi
if cmd_exists 'pnpm' \
&& file_exists 'pnpm-lock.yaml' && (has_changed 'pnpm-lock.yaml' || has_changed 'package.json'); then
change_dir 'pnpm-lock.yaml' || change_dir 'package.json'
echo
echo '⚔ Installing packages with pnpm…'
pnpm install
reset_dir
fi
if cmd_exists 'bun' \
&& (file_exists 'bun.lock' || file_exists 'bun.lockb') \
&& (has_changed 'bun.lock' || has_changed 'bun.lockb' || has_changed 'package.json'); then
change_dir 'bun.lock' || change_dir 'bun.lockb' || change_dir 'package.json'
echo
echo '⚔ Installing packages with bun…'
bun install
reset_dir
fi
if cmd_exists 'npm' \
&& has_changed 'package.json' && ! has_js_lockfile; then
change_dir 'package.json'
echo
echo '⚔ Installing packages with npm…'
npm install
reset_dir
fi
echo
echo "🦄 Done"
exit 0