#!/usr/bin/env bash # Copyright (c) Matt Attaway, 2013 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 COPYRIGHT HOLDERS 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 PERFORCE # SOFTWARE, INC. 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. # User contributed content on the Perforce Public Depot is not supported by Perforce, although it may be supported by its author. # This applies to all contributions even those submitted by Perforce employees. # This script takes a Perforce changelist number and generates a tgz of the changelist contents. # The common path for the files will be automatically removed. # check usage: must be somthing that looks like a change number function p4tgz() { if [ $# -ne 1 ] || ! [[ $1 =~ ^[0-9]+$ ]] then echo "Usage: $0 changelist#" && return 1 fi # the tmp dir for the files in the requested changelist T_DIR=/tmp/$1 mkdir /tmp/$1 # check to see if the changelist is pending or submitted CHANGE_STATUS=`p4 -F %Status% -ztag change -o $1` EXTRA_FLAGS="" # if the change is pending fetch any shelved files, otherwise get the submitted files if [ $CHANGE_STATUS == "pending" ] then EXTRA_FLAGS="-Rs" fi # make sure we get some files FILES=( $(p4 -F %depotFile% fstat -e $1 $EXTRA_FLAGS //...) ) if [ ${#FILES[@]} -eq 0 ] then echo "No files in changelist $1" && return 1 fi # print out the list of files associated with the change into a tmp dir for file in "${FILES[@]}" do p4 print -o "$T_DIR${file:1}" -q "$file@=$1" done # find the common path between the files # thanks to Owen Rees for the easy common path idea: # http://us.generation-nt.com/answer/finding-common-path-prefix-shell-script-help-201237692.html DIR=$T_DIR; while TMP_DIR=$DIR/$(ls "$DIR"); [ -d "$TMP_DIR" ]; do DIR="$TMP_DIR" done # build the tar and clean up our mess tar -f $PWD/$1.tgz -zc -C $DIR . rm -rf $T_DIR }