Skip to content

Commit 0371337

Browse files
committed
1.2.x: Load external configuration (git_xy.env)
Allow to refine git_xy_rsync
1 parent 72fc167 commit 0371337

File tree

3 files changed

+47
-32
lines changed

3 files changed

+47
-32
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
git_xy.env
2+
git_xy.config

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ Generated by some older versions of the script:
105105

106106
### Environment variables
107107

108+
* `GIT_XY_ENV_FILE`: Where you define some configuration for your
109+
script. In this file you can define/do whatever you want before
110+
the script starts. Please note that you may want to use `export`
111+
to make your variables available to subsequent processes.
108112
* `GIT_XY_CONFIG`: Path to the configuration file. When it is empty and/or
109113
not specified, the value `git_xy.config` is used. You can also use `-`
110114
to specify `stdin` as the input source of configurations.

git_xy.sh

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ requirements_check() {
2828
# Prepare some essential configurations for the script.
2929
# We read them from the current shell environment.
3030
git_xy_env() {
31+
GIT_XY_ENV_FILE="${GIT_XY_ENV_FILE:-git_xy.env}"
32+
export GIT_XY_ENV_FILE
33+
readonly GIT_XY_ENV_FILE
34+
35+
if [[ -f "${GIT_XY_ENV_FILE}" ]]; then
36+
log "INFO: Loading external configuration file: '$GIT_XY_ENV_FILE'"
37+
source "$GIT_XY_ENV_FILE" || return
38+
fi
39+
3140
GIT_XY_CONFIG="${GIT_XY_CONFIG:-git_xy.config}"
3241
D_GIT_SYNC="$HOME/.local/share/git_xy/"
3342
GIT_XY_HOOKS="${GIT_XY_HOOKS-gh}"
@@ -107,6 +116,29 @@ git_pull() {
107116
)
108117
}
109118

119+
__pr_commit_message() {
120+
cat <<EOF
121+
git_xy:
122+
version: ${GIT_XY_VERSION}
123+
src:
124+
repo : $src_repo
125+
branch : $src_branch
126+
path : $o_src_path
127+
commit : $src_commit_hash
128+
subject : $src_commit_subject
129+
dst:
130+
repo : $dst_repo
131+
branch : $dst_branch
132+
path : $o_dst_path
133+
commit : $dst_commit_hash
134+
options:
135+
rsync :
136+
options: $rsync_opts
137+
def: |
138+
$(declare -f git_xy_rsync | awk '{printf(" %s\n",$0)}')
139+
EOF
140+
}
141+
110142
# Generate a Github pull request
111143
# Will return 0 if there is an existing branch and/or
112144
# there is not commits between two branches.
@@ -141,21 +173,7 @@ __hook_gh() {
141173
--base "$dst_branch" \
142174
--title "git_xy/sync from $src_repo ref=$src_branch path=$src_path" \
143175
--body "\`\`\`
144-
git_xy:
145-
version: ${GIT_XY_VERSION}
146-
src:
147-
repo : $src_repo
148-
branch : $src_branch
149-
path : $o_src_path
150-
commit : $src_commit_hash
151-
subject : $src_commit_subject
152-
dst:
153-
repo : $dst_repo
154-
branch : $dst_branch
155-
path : $o_dst_path
156-
commit : $dst_commit_hash
157-
options:
158-
rsync : $rsync_opts
176+
$(__pr_commit_message)
159177
\`\`\`" \
160178
| awk '
161179
BEGIN{
@@ -202,21 +220,7 @@ __dst_commit_changes_if_any() {
202220
git commit -a -m"git_xy/$src_repo branch $src_branch path $src_path
203221
204222
\`\`\`
205-
git_xy:
206-
version: ${GIT_XY_VERSION}
207-
src:
208-
repo : $src_repo
209-
branch : $src_branch
210-
path : $o_src_path
211-
commit : $src_commit_hash
212-
subject : $src_commit_subject
213-
dst:
214-
repo : $dst_repo
215-
branch : $dst_branch
216-
path : $o_dst_path
217-
commit : $dst_commit_hash
218-
options:
219-
rsync : $rsync_opts
223+
$(__pr_commit_message)
220224
\`\`\`
221225
"
222226
# shellcheck disable=SC2086
@@ -246,6 +250,11 @@ __last_error() {
246250
last_error=""
247251
}
248252

253+
# A simple wrapper for rsync.
254+
git_xy_rsync() {
255+
rsync "${@}"
256+
}
257+
249258
# The main program
250259
git_xy() {
251260
n_config=0
@@ -375,7 +384,7 @@ git_xy() {
375384
# FIXED: as we may want to grab some configuration file (rsync_opts)
376385
# FIXME: Better quoting handle for `rsync_opts`
377386
# shellcheck disable=SC2086
378-
rsync -rap \
387+
git_xy_rsync -rap \
379388
$rsync_opts \
380389
--exclude=".git/*" \
381390
"$src_local_full_path/$src_path" \
@@ -415,7 +424,7 @@ main() {
415424
&& git_xy
416425
}
417426

418-
GIT_XY_VERSION="1.0.0"
427+
GIT_XY_VERSION="1.2.0"
419428
export GIT_XY_VERSION
420429

421430
declare -A GIT_XY_ERRORS

0 commit comments

Comments
 (0)