findchange.sh #1

  • //
  • guest/
  • amo/
  • scripts/
  • findchange.sh
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#!/bin/bash

# ==========
# $File: //guest/amo/scripts/findchange.sh $
# $Revision: #1 $
# $Change: 24203 $
#
# $Author: amo $
# $DateTimeTZ: 2018/06/12 03:39:29 -0700 PDT $
# ----------
# Bash script for searching change descriptions for a given string.
#
# Takes four parameters:
# status:	one of pending, shelved, submitted
# depth: 	an integer
# pattern:	the string for which you are searching
# depotpath:	a pattern for matching files, eg. //depot/path/...
#
# With this information, the script builds a 'p4 changes' command
# then searches each change for the string given. 
# ==========

stat=$1		# 'type' of changelist
deep=$2		# depth (number of changes to check)  
patt=$3		# pattern to search for ('grep -e "$patt"')
path=$4		# changes relate to this set of files

# messages
usage="USAGE: $0 status depth pattern depotpath"
nocli="ERROR: 'p4' client not found."
nocon="ERROR: 'p4' not logged in or no connection available."
stats="ERROR: Status must be pending, shelved or submitted."
nstat="ERROR: Unknown status: '$stat'."
nonum="ERROR: Non-numeric depth: $deep"
param="ERROR: Unexpected parameters provided - need four."

# Checking what we've been given

# no parameters given, so report 'usage'
if [ "$#" -eq 0 ]; then printf "%s\n" "$usage"; exit 1; fi

# check we have 'p4' available
p4cli=`which p4`
if [ "$?" -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocli"; exit 2; fi

# check there's a valid connection (looks for ticket)
$p4cli login -s > /dev/null 2>&1
if [ $? -ne 0 ]; then printf "%s\n%s\n" "$usage" "$nocon"; exit 3; fi

# expecting four parameters; error if not
if [ "$#" -ne 4 ]; then printf "%s\n%s\n" "$usage" "$param"; exit 4; fi

# number of changes must be integer; error if not.
if ! [[ $deep =~ ^[0-9]+$ ]] ; then printf "%s\n%s\n" "$usage" "$nonum"; exit 5; fi

# status must be one of three possibilities; error if something else is provided.
case "$stat" in 
	pending|shelved|submitted) ;;
	*) printf "%s\n%s\n%s\n" "$usage" "$stats" "$nstat"; exit 6 ;;
esac

# initialise counters
total=0
found=0

# we have got through the checks so let's say what we're doing.
conn=("`$p4cli -F "%serverAddress% %userName% %clientName%" -ztag info`")
printf "Connecting to '%s' as user '%s' using client '%s'.\n" $conn
printf "Searching the last %s changes in '%s' for descriptions containing '%s'\n\n" "$deep" "$path" "$patt"

# core 'changes' command - we'll search these changes for the pattern
chgs=`$p4cli -F %change% -ztag changes -s $stat -m$deep $path`

# loop through the changes and note which ones (if any) contain the pattern
for c in $chgs; do
	testdesc=`$p4cli -F %Description% -ztag change -o $c | grep -e "$patt"`
	rc=$?; if [[ $rc == 0 ]]; then printf "Change: %s\n" $c; (( found++ )); fi 
	(( total++ ))
done
printf "\nSearched %u, found %u.\nNB. may search more than %s changes and\\or \
show duplicates depending on expansion of '%s'.\n" $total $found $deep "$path"
# Change User Description Committed
#1 24203 Adam Morriss Moving files to 'amo'
//guest/adam_morriss/scripts/findchange.sh
#11 18663 Adam Morriss Swapped 'echo' for 'printf', added check for valid status
some restructuring (fewer lines used than before)
#10 18658 Adam Morriss Additional error checking and comments in code
#9 18588 Adam Morriss removed unused message
#8 18558 Adam Morriss correcting mistyped keyword
#7 18557 Adam Morriss added quotes to the grep command allowing handling of patterns containing spaces.
#6 18556 Adam Morriss Removed case statement.
This changed the underlying 'changes' command, ignoring the depot path for pending changes or those containing shelved files. Not required (at least not that I've noticed so far).
There is a difference with 'p4 changes' commands when no status is provided - possibly what I was thinking of when I added this piece in the first place.
#5 18554 Adam Morriss changing filetype to add keywords
#4 18553 Adam Morriss Adding comment to explain purpose.
#3 18552 Adam Morriss update to findchange script for searching change descriptions
#2 18549 Adam Morriss Potential fix evaluating command.
#1 18478 Adam Morriss A couple of bash scripts to carry out basic checks.

Both are functional, though need further checks and measures, and neither have been tested to destruction.

'working-autolabel' attempts to produce a list of automatic labels corresponding to a (currently hard-coded) depot path.
'findchange' searches the description of changes related to a specified path for a provided string, and lists the change number for those that match. This needs further error checking, and the ability to search pending changes (which won't work if you provide a path). Would be good to check that the depot path exists too.