Repository navigation
Expand file tree
/
Copy pathvcs.bash
More file actions
executable file
·141 lines (116 loc) · 3.22 KB
/
Copy pathvcs.bash
File metadata and controls
executable file
·141 lines (116 loc) · 3.22 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
#!/usr/bin/env bash
pretty_date() {
_TS=$1
_FORMAT='+%Y%m%dT%H%M'
if [[ $(uname -s) == 'Darwin' ]]; then
date -u -j -f '%s' "${_TS}" "${_FORMAT}"
else
date -u --date @"${_TS}" "${_FORMAT}"
fi
}
git_revid() {
git log -n1 --format='%H'
}
git_epoch() {
git log -n1 --format='%at'
}
git_branch() {
git symbolic-ref --short -q HEAD
}
git_clean() {
git clean -fdx
git reset --hard
}
git_is_dirty() {
[[ -n "$(git status --porcelain=v1 --untracked-files=normal)" ]]
}
git_has_unmerged_paths() {
[[ -n "$(git diff --name-only --diff-filter=U)" ]]
}
git_has_in_progress_operation() {
_git_dir=$(git rev-parse --git-dir 2>/dev/null) || return 1
[[ -d "${_git_dir}/rebase-merge" ]] ||
[[ -d "${_git_dir}/rebase-apply" ]] ||
[[ -f "${_git_dir}/MERGE_HEAD" ]] ||
[[ -f "${_git_dir}/CHERRY_PICK_HEAD" ]] ||
[[ -f "${_git_dir}/REVERT_HEAD" ]] ||
[[ -f "${_git_dir}/BISECT_LOG" ]]
}
run_with_optional_timeout() {
_timeout_seconds=$1
shift
if command -v timeout &>/dev/null; then
timeout "${_timeout_seconds}" "$@"
elif command -v gtimeout &>/dev/null; then
gtimeout "${_timeout_seconds}" "$@"
else
"$@"
fi
}
git_default_branch_for_remote() {
_remote=$1
_default_branch=$(
git symbolic-ref --quiet --short "refs/remotes/${_remote}/HEAD" 2>/dev/null |
sed "s#^${_remote}/##"
)
if [[ -n "${_default_branch}" ]]; then
echo "${_default_branch}"
return 0
fi
_default_branch=$(
git remote show "${_remote}" 2>/dev/null |
grep 'HEAD branch' |
perl -pe 's|HEAD branch: (.*)|${1}|g' |
awk '{print $1}'
)
if [[ -n "${_default_branch}" ]]; then
echo "${_default_branch}"
return 0
fi
echo "Unable to determine default branch for ${_remote}" >&2
return 1
}
git_update() {
_current_branch=$(git branch --show-current)
if git_has_in_progress_operation; then
echo "Skipping repo with in-progress git operation"
return 10
fi
if git_has_unmerged_paths; then
echo "Skipping repo with unmerged paths"
return 11
fi
if git_is_dirty; then
echo "Skipping dirty repo"
return 12
fi
# Try origin first, fall back to upstream if origin is unreachable
_remote="origin"
if ! run_with_optional_timeout 5 git ls-remote --exit-code origin &>/dev/null; then
if git remote get-url upstream &>/dev/null; then
echo "origin unreachable, using upstream instead"
_remote="upstream"
else
echo "origin unreachable and no upstream remote, skipping"
return 20
fi
fi
# Stale reflog for ${_remote}/<repo-dir> can break fetch; remove before pull.
_git_dir=$(git rev-parse --git-dir)
_repo_reflog="${_git_dir}/logs/refs/remotes/${_remote}/$(basename "$(git rev-parse --show-toplevel)")"
if [[ -f "${_repo_reflog}" ]]; then
rm -f -- "${_repo_reflog}"
fi
git fetch --prune "${_remote}" || return 21
_default_branch=$(git_default_branch_for_remote "${_remote}") || return 22
git checkout "${_default_branch}" || return 23
git branch --set-upstream-to="${_remote}"/"${_default_branch}" "${_default_branch}"
if [[ -f .gitmodules ]]; then
git submodule update --init || return 24
fi
git merge --ff-only "${_remote}"/"${_default_branch}" || return 25
if [[ -n "${_current_branch}" && $_default_branch != "$_current_branch" ]]; then
git checkout "${_current_branch}" || return 26
fi
#git-delete-merged-branches
}