# Root git directorydir="$(git rev-parse --show-toplevel)"# Put the grep output into an array (see below)str_split_line "$(grep -r "^<<<<<<< " "${dir})" filesbn="$(basename "${dir}")"for i in "${files[@]}"; do# Remove the matched string, so we're left with the file namefile="$(sed -e "s/:<<<<<<< .*//" <<< "${i}")"
# Remove the path, keep the project dir's namefileShort="${file#"${dir}"}"fileShort="${bn}${fileShort}"
# Confirm merge divider & closer are presentc1=$(grep -c "^=======" "${file}")c2=$(grep -c "^>>>>>>> " "${file}")if [[ c1 -gt 0 && c2 -gt 0 ]]; thenecho "${fileShort} has a merge conflict"fidone
产出
projectdir/file-nameprojectdir/subdir/file-name
按行函数拆分字符串
如果您不想将其作为单独的函数,则可以复制代码块
function str_split_line(){# for IFS, see https://stackoverflow.com/questions/16831429/when-setting-ifs-to-split-on-newlines-why-is-it-necessary-to-include-a-backspacIFS=""declare -n lines=$2while read line; dolines+=("${line}")done <<< "${1}"}