Perforce - getting stream name in a trigger script - triggers

I want to create a trigger for preventing check-out on files if they belong to a specific stream.
I am using the pre-user-edit trigger.
The trigger executes a Perl script.
When I execute in the Perl script a p4 command, then I get this error:
Perforce password (P4PASSWD) invalid or unset.
What I did is getting the stream name out of the client name (which is passed to the Perl script):
$ stream = `p4 client -o $ client | grep ^Stream: | awk '{print \$ 2}'`;
chomp $ stream;
This does not work.
Also, trying to assign $p4 with new P4, fails.
Anyone has a clue how to solve this?
At least, give me a way to extract the stream name out the client name?
Thanks,

You must be logged in to Perforce to run p4 client. In interactive shell you do this by p4 login which prompts you for a password. Once that's validated, Perforce keeps you logged in for a week or so (depends on your P4 server setting). During that week your command should succeed, but once your session expires it will start failing again.
If session expiry is a problem for you, you will need to get hold of a non-expiring ticket. That must be enabled by your server admin (read "To create tickets that do not expire..." att p4 login). See also P4TICKETS.

As an alternative,
$ stream = p4 -F "%Stream%" -ztag client -o;
This would just give you the stream name(no trimming is required)

As an other alternative, use p4 switch to show the name of the current stream.

Related

How do I get the last commit programmatically in Java code? Jenkins / sbt

I started writing a little tool that basically can do something (ex. compile or test code) and then send an email if it fails.
https://github.com/JohnReedLOL/EmailTestingBot
I want to add a feature where this tool can programmatically look at the last commit in the working directory, look at the author of the commit, extract their email, and then email that person whether their commit resulted in a pass or a failure.
For example, I want it to do something like: Git: See my last commit
Where the email basically says:
Subject: Test Results
Message: All your tests passed in dev for commit 0e39756383662573.
Does Jenkins provide this functionality already? I want to make my setup email the person who put in the most recent commit.
Also, is there a way I can obtain the email of the author of the most recent commit programmatically (ex. perhaps with http://www.eclipse.org/jgit/ or http://javagit.sourceforge.net )?
I don't really care how I get email notifications - I just want them and I can't use TravisCI.
I will try to give solutions part by part.
Part 1 :
Yes, you can run ShellScript(Shell Commands) from Jenkins Link.
Part 2
How to get the Email Id and other Stuff from GitCommit.
For that Jenkins sever should have git command installed in build server.
Create one conf file ex. /conf/reference which have
app {
git {
commit = "CURRENT_COMMIT"
repo = "CURRENT_REPO"
timestamp = "CURRENT_TIMESTAMP"
emailId = "EMAIL_ID"
}
}
When making your build run the command
sed -i'' "s/CURRENT_COMMIT/$(git rev-parse HEAD)/g" conf/reference.conf
sed -i'' "s^CURRENT_REPO^$(git config --get remote.origin.url)^g" conf/reference.conf
sed -i'' "s/CURRENT_TIMESTAMP/$(git show -s --format=%ci HEAD)/g" conf/reference.conf
sed -i'' "s/EMAIL_ID/git --no-pager show -s --format='%an <%ae>' CURRENT_COMMIT/g" conf/reference.conf
above code will put the values in reference.conf.
Now you can use to get the info and send the mail. As far as I know, Jenkins gives the capability to send the Email. Jenkins work on the environment variables rather than putting this into reference.conf you can put this in Environment variable and use the environment variables to send the mail.
FYI: I haven't tested this code but as far as I remember working in Jenkins, we used to send email through this way.
#HappyCoding

How to read a file without checking out in perforce

I'm writing a syntax check tool to parse several files on different branches.
Is there a way for me to read the contents without checking out the file?
The tool is written in Perl.
`p4 print //depot/path/to/file`;
(Usual requirements for running a p4 command apply -- make sure the p4 executable is in your PATH, make sure you're authenticated with p4 login, make sure you're connecting to the right server, etc.)
See p4 help print for more info on the print command -- you might find the -q and/or -o flags helpful depending on what exactly you need to do with the output.

Validate if command was successfull in a batch file or revert back all settings

Please check my batch file command as I need to dis-join computer from old domain and join it to a new one.
Sometimes computer succeeded to dis-join the computer but fails to add it in a new domain so this computer will be out of domain. I need to add command to validate if the computer is successfully joined to the new domain otherwise revert it back to the old domain.
#echo off
netdom.exe remove %computername% /domain:MyOlddomain.local /UserD:Myusername /PasswordD:Mypassword
Ping 127.0.0.1 -n 5 >nul
netdom.exe join %computername% /domain:MyNewDomain /UserD:Myusername /PasswordD:Mypassword
Ping 127.0.0.1 -n 5 >nul
shutdown -t 5 -r -f
The best way about it is to use a IF statement.
The basic structure of a IF statement is as follows.
IF EXIST "%1" (
echo "it's here!"
) ELSE (
echo "it isn't here!"
)
Going about it like this will allow you to revert when needed.
More in depth reading!

p4 sync Perforce command line to sync multiple trees to one changelist by entering the changelist only once

How can I sync 2 completely separate trees at a given changelist by entering the changelist only once, e.g. giving the same result as
p4 sync //tree1/a/b/c/...#1234 //tree2/d/e/...#1234
which works, but requires entering 1234 twice?
This gives "syntax error near unexpected token `('":
p4 sync (//tree1/a/b/c/... //tree2/d/e/...)#1234
This syncs //tree1/a/b/c/... to head instead:
p4 sync //tree1/a/b/c/... //tree2/d/e/...#1234
The main reason is that I want to make a shell alias but the 1234 part is user-entered, so it must be entered only once. I know I can use a shell script or function but for various reasons, I must use an alias.
From the comments, the (bash, at least) shell syntax of
p4 sync {//tree1/a/b/c/...,//tree2/d/e/...}#1234
should work. For more explanation, see the brace expansion section of the bash reference.

In Perforce, what is the command to connect to a different port when switching client user?

What is the command-line equivalent of "Switch Port Client User" as found in the p4win gui client?
I am already logged under one port but now I am attempting to connect to a different port on the same server in order to access a separate source control file depot. I assume it would involve using:
p4 login
However, reading the 'help' for 'login' does not show an option to specify the port #. Both user name and client name would remain the same but just need to change the port #.
The P4PORT configuration variable stores the Perforce server name and port number to connect to. You can set this value as an environment variable or, if you're using Windows, in the registry using 'p4 set':
p4 set P4PORT=perforce:1669
To see what the current value of P4PORT is:
> p4 set P4PORT
P4PORT=perforce:1669
If you want to do it generically for any P4 command then the general form can be found via "p4 help usage".
In a nutshell,
p4 -p <your port> login
will do what you asked for. Note from the usage help that you can specify most things from the command line such as client spec, username, password, etc.
E.g:
p4 set P4PORT=1666
From the help:
C:\> p4 help environment
Environment variables used by Perforce:
Variable Defines For more information see
-------- ------- ------------------------
P4AUDIT name of server audit file p4d -h
P4CHARSET client's local character set p4 help charset
P4COMMANDCHARSET client's local character set for
command line operations p4 help charset
P4CLIENT name of client workspace p4 help client
p4 help usage
P4CONFIG name of configuration file Command Reference Manual
P4DIFF diff program to use on client p4 help diff
P4DIFFUNICODE diff program to use on client p4 help diff
P4EDITOR editor invoked by p4 commands p4 help change, etc
P4HOST name of host computer p4 help client
p4 help usage
P4JOURNAL name of server journal file p4d -h
P4LANGUAGE language for text messages p4 help usage
P4LOG name of server log file p4d -h
P4MERGE merge program to use on client p4 help resolve
P4MERGEUNICODE merge program to use on client p4 help resolve
P4PAGER pager for 'p4 resolve' output p4 help resolve
P4PASSWD user password passed to server p4 help passwd
P4PORT port client connects to p4 help info
or server listens on p4d -h
P4ROOT server root directory p4d -h
P4TARGET target server for proxy Command Reference Manual
P4TICKETS location of tickets file Command Reference Manual
P4USER user name p4 help usage
PWD current working directory p4 help usage
TMP, TEMP directory for temporary files Command Reference Manual
See 'p4 help set' for details specific to Windows. The syntax for
setting an environment variable depends on the OS/shell. Note that many
shells allow the setting of shell variables separate from environment
variables - Perforce cannot see the shell variable, only the environment
variable.
If you are a typical user then the only variables of interest are
$P4CLIENT, $P4PORT and $P4PASSWD.
C:\> p4 help set
set -- Set variables in the registry (Windows only)
p4 set [ -s -S service ] [ var=[value] ]
'p4 set' sets the registry variables used by Perforce on Windows
platforms. Normally, the variable 'var' is set to 'value'.
If 'value' is missing, the variable 'var' is unset. Without
any arguments at all, 'p4 set' list variable settings.
The -s flag causes 'p4 set' to set variables for the whole system
rather than for the user. You must have NT administrator powers
to use this.
The -S service flag causes 'p4 set' to set variables for the named
service. You must have NT administrator powers to use this.
Currently, registry variable entries may be overridden by environment
variables and (in some cases) flags on the command line.
See 'p4 help environment' for a list of environment/registry variables.
You can use a configuration file to set the port that Perforce connects to for each project.
First, create a text file that contains the Perforce configuration variables you want to set for the project. For example, to set the value of P4PORT, the contents of the file would look like this:
P4PORT=hostname:1234
Name the file something descriptive like .p4config and place it in the root of the project folder. Do this for each one of your projects, changing the variables as necessary. Use the same filename.
Then, set the value of P4CONFIG to the name of your configuration files, e.g.
p4 set P4CONFIG=.p4config
This will make Perforce look for configuration values in a file of that name in the current directory or any parent directory, so you don't have to change your configuration variables manually every time you switch projects.