#!/bin/bash
#------------------------------------------------------------------------------
# Sample script illustratring Protections table entries to deny ability to
# branch from a particular path. It generates a disposable micro-repo
# for playing with the feature.
set -u
# Sample Usage:
# ./virtual_exclusion.sh -f 2>&1 | tee log.virtual_exclusion
#
# Then you can play with the resulting sample.
# export P4CONFIG=.p4config
# cd /tmp/virtual_exclusions
# p4 info
#
export P4CONFIG=.p4config
export P4ENVIRO=/dev/null/.p4enviro
declare Version=1.0.1
# Micro functions.
function msg () { echo -e "${1:-Hi}"; }
function bail () { msg "Error: ${1:-Unknown Error}"; exit ${2:-1}; }
function cmd () { msg "${2:-Executing command: $1}"; $1; return $?; }
declare -i shiftArgs=0
declare ScenarioTitle="Virtual Exclusion"
declare -i Force=0
#------------------------------------------------------------------------------
# Command Line Args
set +u; while [[ $# -gt 0 ]]; do
case $1 in
(-f) Force=1;;
(*) usageError "Unknown arg ($1).";;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ $shiftArgs -gt 0 ]]; do
[[ $# -eq 0 ]] && usageError "Bad usage."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
ReproDir=/tmp/virtual_exclusions
msg "Started ${0##*/} v$Version at $(date)."
msg "ReproDir=$ReproDir"
[[ -d $ReproDir && $Force -eq 1 ]] && /bin/rm -rf "$ReproDir"
[[ -d $ReproDir ]] && bail "Old repro dir [$ReproDir] exists. To remove it, run again using -f."
mkdir $ReproDir || bail "Failed to create dir: $ReproDir"
cd $ReproDir || bail "Failed to cd to $ReproDir."
msg "==============================================================================\nScenario: $ScenarioTitle\n"
msg "\nPreliminary info: Show versions of p4/p4d on the PATH:"
cmd "p4 -V"
cmd "p4d -V"
msg "\nPreliminary setup: Spin up a local repo."
cmd "p4 init -C0 -n" || bail "Failed to set up repo. Ensure p4 and p4d are on path, the same version, and that version should be 2015.1 or later."
msg "\nMaking and populating sample source tree in main."
cmd "mkdir -p project/tests goofy_test mac_test"
for d in *; do echo Hello > $d/main.c; done
echo Big Hello > project/tests/BigDataFile.txt
cmd "p4 switch"
cmd "p4 rec"
cmd "p4 submit -d AddedSourceTree."
cmd "find * -type f -print" "\nShowing files in main, big data stuff is here."
msg "\nCreating virtual stream to exclude big test data."
echo -e "Stream:\t//stream/mac_view\n
Name:\tmac_view\n
Owner:\t$USER\n
Parent: //stream/main\n
Type: virtual\n
Description:\n\tTrimmed view of main.\n
Paths:
\tshare mac_test/...
\t share project/...
\t exclude project/tests/...\n" | p4 stream -i
cmd "p4 switch mac_view" "\nSwitching to mac_view stream."
cmd "p4 switch"
cmd "find * -type f -print" "\nShowing files in mac_view, no big data stuff here."
msg "\nNext create and populate a dev stream parented directly by main."
p4 stream -t development -P //stream/main -o //stream/dev | p4 stream -i
cmd "p4 populate -o -r -S //stream/dev" "\nPopulate the new dev stream."
msg "\nThis dev stream, parented directly by main, branched everything."
msg "\nNext create a dev stream parented by the mac_view virtual stream."
p4 stream -t development -P //stream/mac_view -o //stream/mac_dev | p4 stream -i
cmd "p4 populate -o -r -S //stream/mac_dev" "\nPopulate the new mac_dev stream."
msg "\nAs expected, big data stuff is not in the dev stream."
exit 0