2018-03-28 12:48:49 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# doc cmd_update{
|
|
|
|
#
|
|
|
|
# DESCRIPTION
|
|
|
|
# cmd_update - Updates database of TARGET from git HEAD to last commit hash.
|
|
|
|
#
|
|
|
|
# USAGE
|
|
|
|
# cmd_update </path/to/git/dir> </path/to/target/dir>
|
|
|
|
#
|
|
|
|
# }
|
|
|
|
|
|
|
|
cmd_update() {
|
|
|
|
local GIT_DIR="${1}"
|
|
|
|
local TARGET="${2}"
|
|
|
|
local DB_FILE="${TARGET}/${_OPT_DB_FILE}"
|
|
|
|
|
|
|
|
# In the event that LAST_COMMIT OR DB_FILE does not exist, echo out that the LAST_COMMIT
|
|
|
|
# is the first COMMIT of GIT_DIR, thus stating that no database update has ever occured
|
|
|
|
if [[ -z "$(grep LAST_COMMIT ${DB_FILE})" ]]; then
|
2018-03-28 19:26:38 -04:00
|
|
|
echo -e "LAST_COMMIT\t$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" >> ${DB_FILE}
|
2018-03-28 12:48:49 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Defines what imageids needs updating by determining what files has changed since last
|
|
|
|
# update via git diff --name-only.
|
|
|
|
local NEW_COMMIT=$(git -C "${GIT_DIR}" rev-parse HEAD)
|
2018-03-28 19:26:38 -04:00
|
|
|
local OLD_COMMIT=$(awk 'BEGIN{FS="\t"}{if($1=="LAST_COMMIT"){print $2}}' ${DB_FILE})
|
2018-03-28 12:48:49 -04:00
|
|
|
imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(git -C "${GIT_DIR}" diff --name-only ${NEW_COMMIT} ${OLD_COMMIT}) | awk '!seen[$0]++' | grep SHA256))
|
|
|
|
|
|
|
|
_msg EXEC "Updating database from ${OLD_COMMIT} to ${NEW_COMMIT}"
|
2018-03-28 19:26:38 -04:00
|
|
|
db_update "${GIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageidList[@]}
|
2018-03-28 12:48:49 -04:00
|
|
|
[[ $? -ne 0 ]] && { _msg WARN; local ERR=true; } || _msg OK
|
2018-03-29 09:24:58 -04:00
|
|
|
[[ ${ERR} ]] && return 1 || { awk -v newcommit=${NEW_COMMIT} 'BEGIN{FS="\t"}{if($1=="LAST_COMMIT"){$2=newcommit}{print $0}}' ${DB_FILE} > ${DB_FILE}.tmp; mv ${DB_FILE}.tmp ${DB_FILE}; return 0; }
|
2018-03-28 12:48:49 -04:00
|
|
|
}
|