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
|
2018-04-15 22:58:41 -04:00
|
|
|
# cmd_update </path/to/git/dir> </path/to/target/dir> [<old git commit>]
|
2018-03-28 12:48:49 -04:00
|
|
|
#
|
2018-05-18 13:39:39 -07:00
|
|
|
# DEPENDENCIES src/db_update
|
|
|
|
#
|
2018-03-28 12:48:49 -04:00
|
|
|
# }
|
|
|
|
|
|
|
|
cmd_update() {
|
|
|
|
local GIT_DIR="${1}"
|
|
|
|
local TARGET="${2}"
|
2018-04-15 22:58:41 -04:00
|
|
|
local OLD_COMMIT="${3}"
|
2018-03-28 12:48:49 -04:00
|
|
|
local DB_FILE="${TARGET}/${_OPT_DB_FILE}"
|
2018-04-07 21:40:41 -04:00
|
|
|
[[ ! -f "${DB_FILE}" ]] && return 3
|
2018-04-07 21:18:44 -04:00
|
|
|
[[ ! -d "${GIT_DIR}/.git" ]] && return 2
|
2018-05-16 00:09:46 -07:00
|
|
|
[[ "$(git -C "${GIT_DIR}" rev-list HEAD | tail -n 1)" != "$(awk 'BEGIN{FS="\t"}{if($1=="REPO_ID"){print $2}}' "${DB_FILE}")" ]] && return 2
|
2018-03-28 12:48:49 -04:00
|
|
|
|
|
|
|
# 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
|
2018-04-06 18:40:19 -04:00
|
|
|
if [[ -z "$(grep LAST_UPDATE ${DB_FILE})" ]]; then
|
|
|
|
echo -e "LAST_UPDATE\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-04-15 22:58:41 -04:00
|
|
|
[[ -z "${OLD_COMMIT}" ]] && local OLD_COMMIT=$(awk 'BEGIN{FS="\t"}{if($1=="LAST_UPDATE"){print $2}}' ${DB_FILE})
|
2018-04-06 18:40:19 -04:00
|
|
|
local imageidList=($(sed 's/\(.*\)\..*/\1/' <<< $(git -C "${GIT_DIR}" diff --name-only ${NEW_COMMIT} ${OLD_COMMIT}) | awk '!seen[$0]++' | grep SHA256))
|
2018-03-28 12:48:49 -04:00
|
|
|
|
2018-04-08 13:01:55 -04:00
|
|
|
local COUNT=1
|
|
|
|
for imageid in ${imageidList[@]}; do
|
|
|
|
echo "[ ${COUNT} / ${#imageidList[@]} ] Processing ${imageid}"
|
|
|
|
db_update "${GIT_DIR}" "${TARGET}" "${DB_FILE}" ${imageid} >${STDERR} 2>&1
|
2018-04-08 13:51:31 -04:00
|
|
|
local EXIT=$?
|
|
|
|
[[ ${EXIT} -eq 0 ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#imageidList[@]} ] Added ${imageid}"; }
|
|
|
|
[[ ${EXIT} -eq 1 ]] && { _ansi up 2; echo -en '\033[K'; echo "[ ${COUNT} / ${#imageidList[@]} ] Removed ${imageid}"; }
|
2018-04-08 13:01:55 -04:00
|
|
|
local COUNT=$(( ${COUNT} + 1 ))
|
|
|
|
done
|
2018-04-06 18:40:19 -04:00
|
|
|
[[ ${ERR} ]] && return 1 || { gawk -i inplace -v newcommit=${NEW_COMMIT} 'BEGIN{FS="\t";OFS="\t"}{if($1=="LAST_UPDATE"){$2=newcommit}{print $0}}' ${DB_FILE}; return 0; }
|
2018-03-28 12:48:49 -04:00
|
|
|
}
|