#!/bin/bash set -u set -e #------------------------------------------------------------------------------ # job-new.sh # # This script simplifies the process of using "named" jobs with a Perforce # server. A "named" job is one that does not use Perforce's built-in job # numbering mechanism, which simply names jobs 'job######', incrementing the # numeric value. # # The idea of job-new.sh is that you can name your jobs something more # meaningful, like MyProj-3, ACME-15, or ACME-15-3 (where the latter # implies that ACME-15-3 is a subtask of ACME-15). By default with # Perforce, jobs named anything other than the default must be manually # named and incremented. With this trigger, you simply name your job # something like "PRJ-new", and the "-new" suffix is replaced with the # next available number. So the first time you create a job with # "Job: PRJ-new", it will be saved as PRJ-1. The next time it will be # PRJ-2. # # This is vaguely like what Perforce does by default when you simply use # "Job: new", except that this numbering scheme does not use left-padding # of zeros, and the built-in prefix of "job" is not used. # # This is not appropriate to us if Perforce is integrated with a defect # tracker such as JIRA for Bugzilla. With such systems, Perforce jobs # mirror issue names from the defect tracker. # # This script must be enabled as a trigger by the Perforce server, with # an entry like the following needed in the Triggers table. # Triggers: # job-new form-in job "/path/to/job-new.sh %formfile%" # # Limitations: # * This assumes old jobs aren't deleted; it will re-use job numbers # if jobs are deleted. # * This dynamically checks existing jobs to determine the next # available job number. The current implementation is not # scalable to large numbers of jobs. (TO DO: Use a 'p4 # key' to recall the highest number for a given project and # the key value). # * Mutex locking is needed. This implementations allows for the # theoretical possibility of a race condition where to users # creating new jobs at the same time could collide. (TO DO: # Use a 'p4 key' to implement a semaphore to prevent the # unlikely chance of a race condition). # # This script has the following runtime dependencies: # This assumes the Perforce context and environment is appropriate, i.e.: # * The preferred 'p4' executable is in the path. # # * P4PORT is set appropriately. # # * P4USER is defined as a currently logged in user (as is typical # for Perforce trigger scripts which operate on the Perforce server # machine). # # * The versionof 'bash' used must be new enough to support the # =~ (regular expression matching) syntax. THISSCRIPT=${0##*/} function bail () { echo -e "\n$THISSCRIPT: Error: $1\n" exit 1 } [[ $# -eq 1 ]] || bail "Bad usage: Missing %formfile% argument." declare oldFormFile=$1 declare newFormFile="${oldFormFile}.NEW.$$.$RANDOM.tmp" declare -i nextJobNum=1 declare thisJobNum=0 declare -i highestJobNum=0 declare -i changed=0 [[ -r $oldFormFile ]] || bail "Error: Missing form file [$oldFormFile]." while read line; do [[ $line == "#"* ]] && continue [[ $line != Job:*-new ]] && continue changed=1 projName=${line#Job:} projName=${projName%-new} projName=$(echo $projName) jobSearchCmd="p4 -ztag -F %Job% jobs -e 'Job=${projName}'*" output=$(eval $jobSearchCmd 2>&1) echo -e "JSC=[$jobSearchCmd]\nOUT=[$output]\n" for job in $(eval $jobSearchCmd); do echo "DBG: Scanning job [$job]" thisJobNum=${job##*-} # Confirm '$thisJobNum' really is purly numeric, so we can do math on it. if [[ $thisJobNum =~ ^[1-9]+$ ]]; then echo "DBG: Looking at TJN[$thisJobNum]." [[ $thisJobNum -gt $highestJobNum ]] && highestJobNum=$thisJobNum fi done nextJobNum=$(($highestJobNum+1)) done < $oldFormFile if [[ $changed -eq 1 ]]; then echo "SED [s:${projName}-new:${projName}-${nextJobNum}:g] $oldFormFile TO $newFormFile" sed "s:${projName}-new:${projName}-${nextJobNum}:g" $oldFormFile > $newFormFile ||\ bail "Failed to update job number." /bin/mv -f "$newFormFile" "$oldFormFile" || bail "Failed to update job number." fi /bin/rm -f "$newFormFile" exit 0
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 12556 | C. Thomas Tyler |
job-new.sh v1.2.0: * Incorporated Lester's feedback. * Moved VERSION tag nearer to top of the file. * Tweaked BASH_VERSION comment to give Mac folks guidance on how to workaround Mac OSX having only an ancinet bash by default. #review-12557 @thomas_gray @lester_cheung |
||
#3 | 12539 | C. Thomas Tyler |
Implemened features to transform job-new.sh into a general purpose job name validation script, in addition to the original mission of incrementing '-new' features, per requirements from Thomas Gray. Updated test suite accordingly. Also simplified the test suite, which now uses the P4PORT 'rsh hack' to avoid needing to fire up a test server. #review-12540 @thomas_gray @lester_cheung |
||
#2 | 12094 | C. Thomas Tyler |
Added version tags to job-new.sh and run-tests.sh. Removed debug statements from job-new.sh. Updated .p4ignore to avoid adding log file. Updated test suite to run multi-pass test. Added APP_DATA setting to env.sh. |
||
#1 | 12093 | C. Thomas Tyler | Added job-new.sh and friends. |