#!/bin/sh # # Copyright (c) 2016 Baptiste Daroussin # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # $FreeBSD$ set -e rc=0 quiet=0 A_flag=0 E_flag=0 e_flag=0 labelcount=0 usage() { echo "$0: usage $0 [-AEpq] [-L label [ -L label [-L label]]] file1 file2 file3" >&2 } clean_tmp() { if [ "${output}" != "/dev/stdout" ]; then rm -f "${output}" fi } while getopts 'AEeL:pq' opt; do case "${opt}" in A) A_flag=1 ;; E) E_flag=1 ;; L) labelcount=$(( labelcount + 1)) if [ ${labelcount} -gt 3 ]; then echo "$0: too many -L options" >&2 echo "$0 aborted" >&2 exit 2 fi labels="${labels} -L ${OPTARG}" ;; p) output=/dev/stdout ;; q) quiet=1 ;; *) usage exit 2 ;; esac done shift $(( ${OPTIND} -1 )) case "${A_flag}${E_flag}" in 11) echo "$0: -A and -E are incompatible" >&2 exit 2 ;; 10) diff3_args="-A" ;; 01) diff3_args="-E" ;; 00) diff3_args="-E" ;; esac if [ $# -ne 3 ]; then echo "$0: not enough arguments" >&2 usage exit 2 fi trap clean_tmp EXIT if [ -z "${output}" ]; then output=$(mktemp -t merge) fi diff3 ${diff3_args} -m ${labels} "$1" "$2" "$3" >"${output}" || rc=$? if [ "${output}" != "/dev/stdout" ]; then if [ -w "$1" ]; then mv -f "${output}" "$1" else echo "$0: $1 permission denied" >&2 fi fi if [ ${rc} -eq 1 ]; then if [ ${quiet} -eq 0 ]; then echo "$0: warning: conflict during merge" >&2 fi exit 1 fi exit 0