##
# P4 EVERYDAY
# A set of aliases to make P4 behave more like Git
# https://git-scm.com/docs/everyday
#
# Note: this requires an internal build of p4 with bash alias support (sorry!)
# 
# Please see 
# https://swarm.workshop.perforce.com/projects/perforce_software-p4alias-examples/ 
# instead.
##


# STANDALONE DEVELOPER

# add show-branch like output
show-branch = !\
    `which p4` streams //stream/... | \
    cut -f2 -d " " | \
    while read stream; do \
        branch=${stream##*/}; \
        /bin/echo -n "[$branch] "; \
        `which p4` -ztag -F %desc% changes -m1 $stream/...; \
    done;

log = changes -L

# map checkout to switch, also:
# treat -b as -c
# make -r default
# treat 'master' as 'main'
checkout = !\
    args=('-r'); \
    for arg in "$@"; do \
        if [[ $arg == "-b" ]]; then \
            arg="-c"; \
        fi; \
        if [[ $arg == "master" ]]; then \
            arg="main"; \
        fi; \
        args+=("$arg"); \
    done; \
    `which p4` switch "${args[@]}";

# branch runs switch -l, no support for add/delete
branch = switch -l

# add is basically reconcile, but treat '.' as './...'
add = !\
    for arg in "$@"; do \
        if [[ $arg == "." ]]; then \
            arg="./..."; \
        fi; \
        args+=("$arg"); \
    done; \
    `which p4` reconcile "${args[@]}";

# run diff on unopened files (like git)
diff = diff -f -Od

# commit maps to submit, but with -m as -d
commit = !\
    for arg in "$@"; do \
        if [[ $arg == "-m" ]]; then \
            arg="-d"; \
        fi; \
        args+=("$arg"); \
    done; \
    `which p4` submit "${args[@]}";

# when users run reset, direct them to obliterate
reset = !echo "Try: p4 help obliterate"

# merge maps to merge with changes:
# first arg is the from branch
# treat 'master' as 'main'
# auto resolve and submit
original-merge = merge
merge = !\
    if [[ "$1" == "master" ]]; then \
        1="main"; \
    fi; \
    `which p4` original-merge --from "$1" > /dev/null && \
    `which p4` resolve -am > /dev/null && \
    `which p4` submit -d "Merged $1"

# when users run rebase, direct them to unsubmit/resubmit
rebase = !echo "Try: p4 help unsubmit and p4 help resubmit"

# run p4 label when git tag is invoked
tag = label


# PARTICIPANT DEVELOPER

# support p4:// clone URIs
original-clone = clone
clone = !\
    args=('original-clone'); \
    for arg in "$@"; do \
        if [[ $arg == p4://* ]]; then \
            port=`expr "$arg" : 'p4://\([^/]*\)'`; \
            path=`expr "$arg" : 'p4://[^/]*/\(.*\)'`; \
            name=${path##*/}; \
            if [[ -z "$port" || -z "$path" ]]; then \
                echo "p4:// URI must have both a server:port and a path"; \
                exit 1; \
            fi; \
            args=('-d' $name "${args[@]}" '-p' $port '-f' "//$path/main/..."); \
        else \
            args+=("$arg"); \
        fi; \
    done; \
    `which p4` "${args[@]}";

# pull runs fetch
pull = fetch

# fetch suggests running pull
fetch = !echo "Try: p4 help pull"

# push maps to push with some changes:
# --set-upstream as an instruction to map this branch
# treat 'master' as 'main'
original-push = push
push = !\
    [[ "$1" == "--set-upstream" ]] && map=true || map=false; \
    [[ "$map" = true ]] && remote="$2" || remote="$1"; \
    [[ "$map" = true ]] && stream="$3" || stream="$2"; \
    if [[ "$stream" == "master" ]]; then \
        stream="main"; \
    fi; \
    if [[ "$map" = true ]]; then \
        map=$(`which p4` remote -o "$remote" | sed -n "s:/main/:/$stream/:pg"); \
        (`which p4` remote -o "$remote"; echo "$map") | p4 remote -i; \
    fi; \
    `which p4` original-push -r "$remote" -S "//stream/$stream";

# format-patch suggests diff2
format-patch = !echo "Try: p4 help diff2 (see -u argument)"