Version Control Tool ( Perforce )
Today let us look at what is Version Control System and its importance in the field of software engineering.
Typically in a development project, there will be multiple modules and resources working on these modules independently. Definitely one would agree that whatever changes a person X makes to the centralized codebase(system) should not cause any issues and the same goes with changes made by person Y as well and so on.
The challenge is how to make sure that changes made by all of the team members gets tested real time without hampering the work of each of them.
One way would be to discuss among each other and submit it one after the other so that changes will go in sequence at various times. But this would lead to a single monolithic code and if there is a build failure, it would be very difficult to track the cause of the issue. Hence in order to ensure that there is no room for errors and no man hours gets wasted in al these chained events, there came in the concept of Versioning of the Software.
Same like Initial Version will have all the changes, then the revised versions will have more accurate changes and the final version will have the ready-to-go version of the software.
‘Perforce’ is one such tool which i have come across which seems to be extremely useful in performing these tasks. You can create your changes , shelve it and it will give you a perforce link for review. One can create a separate branch from the main trunk(source code ) and do their coding submissions in parallel and once the final version goes through, it can be integrated with the main trunk.
I will share a few commonly used commands which needs to be kept handy while coding , so that you don’t have to read articles or watch videos to use them.
p4 user #username + projects / repositories that you have access to.
p4 groups <ldap> # groups that you are part of
p4 info # will give your client name, server name and license details
p4 diff -duw <file name> # to see the differences in the file
p4 client #gives the mapping of your ldap to sandbox/repo
p4 print -q <file name> # to see the file in the terminal
p4 sync # to download all the files
p4 sync @<change number> #to sync upto this change number
p4 submit # to submit the shelved files
p4 resolve # If multiple people have submitted, resolves the changes
p4 opened # to see the list of open files and who has opened it
p4 edit filename #Will open the file for editing
p4 revert filename # To rollback the changes done in filename
p4 shelve # To shelve your changes for review before submission
p4 reopen -c <change number> # To open for edit of a shelved change
p4 shelve -c -f <change number> # Force shelve to the change number
p4 describe -s <change number> #to see the shelved changes
p4 add <file name> # to add a new file to the shelve / repo
p4 delete <filename> # to remove the shelved filed from repro
p4 shelve -c <change number> -d #deletes the shelve
p4 revert -c <change number> # reverting the changes
p4 unshelve -s <change number> # will pull the shelved changes to local
Scenarios:
If you want to remove a particular file from shelve follow the sequence:
1. p4 unshelve -c <change number>
2. p4 revert <file name>
p4 shelve -r -c <same change number> #this would replace the changes file
If you want to do some minor changes and shelve to the same change number:
1. p4 reopen -c <change number> <file to modify>
2. p4 shelve -f -c <change number> # this will force the changes to same shelve
If you want to add a new file to the same change / shelve:
- p4 edit <file name>
- p4 add -c <change number> <file name>
- p4 reopen -c <change number>
- p4 shelve -f -c <change number>
Basically perforce (p4) commands, just blindly compares whatever you give with the repo /sandbox etc. This is the advantage of using perforce to revert, sync,add,submit,delete,modify,shelve etc your code changes before submission.
Example:
Steps to sync a file from a sandbox named : //sandbox/devops/ to your local drive.
Step 1 : Create a directory in your drive.
mkdir tools
cd tools
Step 2: vi .perforce and type the below, save and close.
P4CLIENT=username_projectname
eg: P4CLIENT=siddarth_devops
Step 3: p4 client [ this will open the mapping file ]
example:
//sandbox/siddarth/devops/… //sidmanoh_devops/…
This basically means all the sub folders and directories under //sandbox/siddarth/devops/ will be synced to //sidmanoh_devops/ which the label for our local drive space that is [ drive:~/tools/]
Save and close the file.
Step 4: p4 sync [ You should start seeing all the files downloaded to your local drive:~/tools/ folder ]
Conclusion:
With a few commands, we can make our life easier. So please try this tool and enjoy your coding experience and feel free to comment for any doubts.
Note: For those who haven’t used perforce at all, this article might be a bit difficult to follow. I would recommend to read through setting up perforce client first and then to play with the commands listed.