Is there a way to extract only CL number and username from p4 changes - perforce

p4 changes command will give me change list number, date, user by whom submitted, changelist description but how to extract only CL number and user name?

Perhaps not the pretties way to do it, but this works for me:
p4 changes | awk '{print $2" "$6}' | sed "s/\#[^\n]*//"
First, awk extracts the changelist number ($2, i.e. column 2) and the username#workspace ($6, i.e. column 6). Then sed removes the #<workspace>.

You'll need to parse the output from p4 manually, but you might find it easier to do so by using p4 -z tag COMMAND, which generates more parsable output.
For example, p4 -z tag changes changes -s submitted -m 1 will output:
... change 123456
... time 1384458979
... user james
... client james-p4
... status submitted
... changeType public
... path //depot/some/path...
... desc Some truncated description

Related

Tortoise SVN pre-commit script allow commit that contains defined string

Creating a pre commit script that will only allow commits that contain a specific string somewhere in the file Test.cfg
Currently I have it working in that it will look through every file committed and blocks commits that contain a specified string
REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
$SVNLOOK diff -t "$TXN" "$REPOS" | \
grep -i "Sting to search here" > /dev/null && { echo "String exists so block commit" 1>&2; exit 1; }
What I am after is for the above code to pretty do the complete opposite so that if the string exists allow the commit and if not then prevent the commit. It would also be nice if I could specify which file should be searched as currently it searches every file and some of the commits can contain 1000's of files
I beg your pardon, but svnlook diff in your case is ugly stupid way. Re-read svnlook subcommands topic in SVNBook, pay attention to svnlook tree/svnlook changed + svnlook cat
Full business-logic of your test can|have to be something like (I'm too lazy to write full bashism here, it will be your duty)
IF $FILENAME exist in transaction ( I'll prefer svnlook tree --full-paths ... just because svnlook changed ... will require additional | gawk {print $2} for clean filename) AND $FILENAME contains $STRING (svnlook cat "$FILENAME" | grep "STRING" ...) DO SOMETHING
Don't forget also process possible edge cases:
$FILENAME doesn't exist in transaction, but presented in WC with correct $STRING, but file not modified according to svn status
The same as above, but modified
pp 1-2, but with disallow $STRING
Due to above notes, I'll recommend to explore|check possibility of replacing file+string by testing custom revision's property in hook (shorter, easier, more manageable)

How to sort by name then date modification in BASH

Lets say I have a folder of .txt files that have a dd-MM-yyyy_HH-mm-ss time followed by _name.txt. I want to be able to sort by name first then time after. Example:
BEFORE
15-2-2010_10-01-55_greg.txt
10-2-1999_10-01-55_greg.txt
10-2-1999_10-01-55_jason.txt
AFTER
greg_1_10-2-1999_10-01-55
greg_2_15-2-2010_10-01-55
jason_1_10-2-1999_10-01-55
Edit: Apologies, from my "cp" line I was meant to copy them into another directory with a different name to them.
Something I tried to do is make a copy with the count, but it doesn't sort the files with the same name properly in terms of dates:
cd data/unfilteredNames
for filename in *.txt; do
n=${filename%.*}
n=${filename##*_}
filteredName=${n%.*}
count=0
find . -type f -name "*_$n" | while read name; do
count=$(($count+1))
cp -p $name ../filteredNames/"$filteredName"_"$count"
done
done
Not sure that the renaming of files is one of your expectation. At least for only sorting file name, you don't need to.
You can do this by only using GNU sort command:
sort -t- -k5.4 -k3.1,3.4 -k2.1,2.1 -k1.1,1.2 -k3.6,3.13 <(printf "%s\n" *.txt)
-t sets the field separator to a dash -.
-k enables to sort based on fields. As explained in man sort page, the syntax is -k<start>,<stop> where <start> or is composed of <field number>.<position>. Adding several -k option to the command allows to sort on multiple fields; the first in he command line having more precedence than the other.
For example, the first -k5.4 tells to sort based on the 5th fields with an offset of 4 characters. There isn't a stop field because this is the end of the filename.
The -k3.1,3.4 option sorts based on the 3rd field starting from offset 1 to 4.
The same principle applies to other -k options.
In your example the month field only has 1 digit. If you have files with a month coded with 2 digits, you might want to pad with 0 all month filenames. This can be done by adding to the printf statement this <(... | sed 's/-0\?\([0-9]\)/-0\1/') and change the -k 2.1,2.1 by -k2.1,2.2.

How to identify user who created the head revision using fstat?

I can do this using filelog. A command such as
p4 filelog -m1 -s //path/to/file
Produces output of the following form:
#51 change 196811 edit on 2014/05/06 by user#client-workspace ...
I would prefer not to have parse it. I can't figure out how to do it using p4 fstat. I would have thought it would be actionOwner, but:
$ p4 fstat -T actionOwner //path/to/file
Field actionOwner doesn't exist.
Instead of using 'fstat', you could do 'p4 -ztag filelog -m1 -s //path/to/file'.
It will produce output something like:
C:\Users\Bryan\perforce\client>p4 -ztag filelog -m 1 //depot/a
... depotFile //depot/a
... rev0 1
... change0 1
... action0 add
... type0 text
... time0 1399680148
... user0 Bryan
... client0 Dell660
... fileSize0 10
... digest0 733F328D8CFF7DD89970EC34A70AA14F
... desc0 My super change
And you could look at the "user0" field.
By the way, if you have a recent enough client, you can even combine this with the less-known -F option, as in:
C:\Users\Bryan\perforce\client>p4 -ztag -F "%user0%" filelog -m 1 //depot/a
Bryan
There are lots of ways to skin this cat, but this is definitely one of the ways.

perforce: how to find the changelist which deletes a line for a file?

So I just found that someone removed a line from a "global" file and the removal is very likely wrong. I need to trace which changelist did the removal, but it is a global file, everyone edits it from many branches. I randomly picked a couple, they both have that line. Any suggestion to do this more systematically?
Time-lapse view is a really good tool for this. You can check out this video for a better idea of how it works.
I would suggest collecting all change# of the file, then using binary search, grabbing each of the change, and grepping for specific line you are looking for, and the character '-' or '<' (depends on your du setting) in the first line.
The line below will give you all the changes:
p4 filelog yourfile.cpp | egrep "^... \#[0-9]+ change" | cut '-d ' -f 4
If you do not want to do binary search manually or write a code to do that in shell or anything else, then I would suggest a brute force, and scan all changes in search for that line.
For example:
p4 filelog yourfile.cpp | egrep "^... \#[0-9]+ change" | cut '-d ' -f 4 | while read change ; do
p4 describe $change | egrep "^<.*your line that was deleted"
[ $? = 0 ] && echo $change
done
Output in my example:
< /* remove the confirmation record for the outstanding write if found */
234039
Where 234039 is the change number that contains your deletion.
I hope it will help.

Accessing the latest label in perforce through Linux

I get a list of labels when I type in the following command in Linux:
p4 labels -e '< pattern-for-required-label >'
But I want to grab only the latest label in the list. If there any command in perforce through which I can select the latest label from the long list?
p4 labels -e ''|head(or "|tail", depending on whether you want to see the top or bottom of the list).
You can do this with sort command:
p4 labels <branch> | sort -k3
The third column is the date. This has plagued me for months and just finally had the aha moment.
To limit the number of labels returned, you can use the -m option:
p4 labels -m 1 -e 'your pattern'
This will only return a single label. Also of note, many of the p4 commands will take the -m parameter to control the number of results that are returned. One more thing: the -E version of the pattern parameter makes the filter pattern case insensitive if you desire/need that.
Reference: http://www.perforce.com/perforce/doc.current/manuals/cmdref/labels.html
If the p4 labels command isn't getting you what you desire, I would suggest taking a look at the Perl, Python and Ruby APIs provided by perforce - these are all supported libraries by the staff at Perforce, and quite capable of interfacing your custom code needs.
http://www.perforce.com/product/components/apis
To complement #Garrett Waiss's and #Zulu's answer:
You can also tell p4 labels to output time using -t switch, and then sort by both date and time. Pipe this thru tail to get just the latest result as in your question:
p4 labels -t <expression> | sort -k3,3 -k4,4 | tail -n1
Also note:
some p4 client versions expect you to specify -e before label expression
if you're using a label naming scheme, for example mylabel_001, mylabel_002,... then you'd want expression to be mylabel_* i.e.:
p4 labels -t -e mylabel_* | sort -k3,3 -k4,4 | tail -n1

Resources