C# API To get Diff btwn Current and Previous Check in

Post your questions regarding using the Vault and Fortress API in your programs.

Moderator: SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Tue Apr 26, 2016 10:50 am

Hi Beth -

Can I use the C# API to return the Difference between each file attached to a Label and it's previous version.

So the current version would be the latest checkin and the previous would be the version just prior to it.

We get the label and the files associated with it, for each of those files we want to return the difference listed above. Is there a way to do this without writing those versions to a folder first? In other words - I pass a label via C# and the vault server does the diff and returns the results?
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Tue Apr 26, 2016 12:19 pm

Getting the differences is a 'client side' operation. The Vault Client can do a GET on each of the separate versions, and then launch a client-side diff process of your choice to show the differences between those two files.

You can see how this works from a command line point of view by tracking VaultCmdLineClient.ProcessCommand() for Command.DIFF which boils down to VaultClientIntegrationLib.ServerOperations.ProcessCommandDiff(). A look at that code base within the API download code shows exactly how this is handled by the Command Line Client and a variation of this exists for the GUI client.
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Wed Apr 27, 2016 7:19 pm

Can you give me an idea of what

DIFF -repository REPO -compareto lastget -norecursive $/project/file.txt \\server\project\file.txt

Error: Working Folder not set for Repository path.

We use a vault client on an application server. No working folders are set. How do I pass one?
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Thu Apr 28, 2016 7:29 am

In the provided example, do you have a working folder set on $/project/ ? If so, does the file at $/project/file.txt exist in that working folder on disk? The 'lastget' compareto option will compare the working folder's file (whatever $/projects/file.txt resolves to on disk) against the contents of the file at the time of the last GET operation.

All of the options of DIFF from the command line client will require an existing file/folder in the working folder.

-----

Assuming I interpreted your post correctly, in that use case, can you call SETWORKINGFOLDER and then GET? If so, the DIFF from the command line should work. If you want to DIFF without assigning a working folder to the folder or file's folder, you would want to do a GET and then run an external diff process of your choosing against those two files/folders.

Please let me know if I misunderstood.
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Thu Apr 28, 2016 7:46 am

I make a call to GETLABEL. Afterwards, I send a request to do a diff.
Requirement: Get the files associated with a label and write them to a SAMBA share \\server\folder then get the difference between that file and the previous version in the repository - which is the lastget option.

Here is the code. First GETLABEL then DIFF. How can I do this differently so that I get a difference between the latest version of the file and the one prior? (i.e. Diff v1 v2)
//GETLABEL
var psi = new ProcessStartInfo()
{
FileName = plinkPath,
Arguments = string.Format("-P 44022 " + vaultRequest.Username + "@ghelsiapp \"GETLABEL -repository {0} {1} {2} -backup no -setfiletime modification -nonworkingfolder {3} -verbose\"", vaultRequest.Repository, objectPath, vaultRequest.ActualLabelName, SambaPath),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var p = Process.Start(psi);

//Later, I run the DIFF
var psi = new ProcessStartInfo()
{
FileName = plinkPath,
Arguments = string.Format("-P 44022 {0}@ghelsiapp \"DIFF -repository {1} -compareto lastget -norecursive {2} {3}\"", vaultRequest.Username, vaultRequest.Repository, "$/" + l_file1 , "\\ghopstestetl1\\src_dartload\\plans\\TEST.txt"),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var p = Process.Start(psi);
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Thu Apr 28, 2016 8:57 am

It looks as if the call to GETLABEL is used with the option to place the results in a non-working folder, but the requirement of DIFF is to use the file found in a working folder.

Can you modify the procedure to first SETWORKING FOLDER, and then change params of GETLABEL to use that working folder? This assumes the process is running under an account with a security context in which can write to %localappdata% or is configured to use a pre-configured cache location**. Upon existing, you may also want to call UNSETWORKINGFOLDER assuming it is no longer needed.

** - Note, I'm not certain how the command line would work when run under a service as some of this configuration is controlled by HKCU\Software\SourceGear\Vault(Pro)\Client\Settings, which may not be available to non-logged in processes. If the cache location *is* needed, one could write the Vault.ClientInstance setup and connectivity portion write into their app, passing the 'cache location' into the call to .Init(), or re-compile the command line with a variation of that change.
Jeff Clausius
SourceGear

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Thu Apr 28, 2016 9:28 am

I don't know if this would work, but have you looked at Folder Changes Archive? You might have to determine both end points (or use Date.Now() as the 'End' endpoint), but I'm trying to figure out if the CREATE_FCA command could somehow help.
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Thu Apr 28, 2016 11:59 am

Ok did the SETWORKINGFOLDER, now I get this error

Arguments = string.Format("-P 44022 {0}@ghelsiapp \"DIFF -repository {1} -compareto lastget -norecursive {2} {3}\"", vaultRequest.Username, vaultRequest.Repository, "$/" + l_file1 , "\\ghtestetl1\\src_launchpad\\README.txt"),

<exception>System.Exception: The Diff utility encountered an error during execution. Please verify the use of VAULTDIFF or the "diff" utility.
at VaultClientIntegrationLib.ServerOperations.ProcessCommandDiff(String diffProgram, String diffArguments, CompareToOption compareToOption, Boolean recursive, String objectPathLeft, String objectPathRight)
at VaultCmdLineClient.VaultCmdLineClient.ProcessCommand(Args curArg)
at VaultCmdLineClient.VaultCmdLineClient.Main(String[] args)</exception>

I tried to specify the default DIFF tool by using "-vaultdiff VAULTDIFF"
Arguments = string.Format("-P 44022 {0}@ghelsiapp \"DIFF -repository {1} -compareto lastget -norecursive -vaultdiff VAULTDIFF {2} {3}\"", vaultRequest.Username, vaultRequest.Repository, "$/" + l_file1 , "\\ghtestetl1\\src_launchpad\\README.txt"),

What do I need to do to tell it to use its internal diff tool?
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Thu Apr 28, 2016 12:12 pm

Let's check the arguments:

a) "Vault DIFF" - looks ok

b) -repository {1} - does your repository name have spaces? If so, then use '-repository \"{1}\"'

c) repository path = '{2}', does your repository path have spaces? If so, then use '\"{2}\"'

d) The use of -VAULTDIFF might be required. Since diff is launched from a command line, a console based DIFF tool was expected to be used instead of the GUI based diff that is installed with the Vault Client. Since Vault doesn't ship with one, the user must provide the details to the diff utility.

With this example, you can tackle this in one of three ways:
  • Explicitly use ' -VAULTDIFF \"\\Some\Path\to\diff-utility-of-your-chose" ' For example, ' -VAULTDIFF \"C:\Program Files\SourceGear\Vault Client\sgdm.exe\" '
  • Do not use -VAULTDIFF in the arguments, but create an environment variable named VAULTDIFF, that points to the diff tool you wish to use. For example, ' echo %VAULTDIFF%' returns "C:\Program Files\SourceGear\Vault Client\sgdm.exe"
  • Do not use -VAULTDIFF in the arguments, but have a binary or batch file named 'diff' that can be found on the PATH environment variable. Note, if you want to use sgdm as your diff utility, you can create a diff.bat file in your path containing the line ' "C:\Program Files\SourceGear\Vault Client\sgdm.exe" "%1" "%2" '
e) what is {3} ? There shouldn't be any other parameters required when compareto is set to 'lastget'.
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Thu Apr 28, 2016 3:37 pm

Tried both below.
plink -P 44022 ehansen@ghelsiapp "DIFF -repository Beachwood -compareto lastget -norecursive -vaultdiff %VAULT_EXE_DIR%/sgdm.exe \"$/Launchpad_src/README.txt\""
plink -P 44022 ehansen@ghelsiapp "DIFF -repository Beachwood -compareto lastget -norecursive -vaultdiff %VAULT_EXE_DIR%/sgdm.exe \\ghtestetl1\src_launchpad\README.txt"

Tried directly with the API:
ServerOperations.ProcessCommandDiff("%VAULT_EXE_DIR%/sgdm.exe", null, CompareToOption.lastget, false, "$/Launchpad_src/README.txt", null);

In every case, it returned "Item Unknown Item cannot be found." Looking at the API, it means it cannot find the left file. However, I am sure it exists.

CMD:
<vault>
<error>
<exception>System.Exception: Item Unknown Item could not be found.
at VaultClientIntegrationLib.ServerOperations.ProcessCommandDiff(String diffP
rogram, String diffArguments, CompareToOption compareToOption, Boolean recursive
, String objectPathLeft, String objectPathRight)
at VaultCmdLineClient.VaultCmdLineClient.ProcessCommand(Args curArg)
at VaultCmdLineClient.VaultCmdLineClient.Main(String[] args)</exception>
</error>
<result>
<success>False</success>
</result>
</vault>

C#:
Item Unknown Item could not be found.
at VaultClientIntegrationLib.ServerOperations.ProcessCommandDiff(String diffProgram, String diffArguments, CompareToOption compareToOption, Boolean recursive, String objectPathLeft, String objectPathRight)
at hdms.sourcecontrol.vault.promotiontool.PromoteLabel.PopulateLinuxFilesForCompareOrDelete(VaultRequest vaultRequest) in j:\vault\development_no_bat\hdms.sourcecontrol.vault.promotiontool\PromoteLabel.cs:line 271
at hdms.sourcecontrol.vault.promotiontool.PromoteLabel.PromoteCodeProcess(VaultRequest vaultRequest, String promoteToThisServer, String label) in j:\vault\development_no_bat\hdms.sourcecontrol.vault.promotiontool\PromoteLabel.cs:line 139
The program '[79600] hdms.sourcecontrol.vault.promotiontool.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Fri Apr 29, 2016 8:14 am

The diff command will not expand environment variables used with -vaultdiff option. Can you try the resolved, absolute path to sgdm.exe instead of %VAULT_EXE_DIR%?
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Fri Apr 29, 2016 3:22 pm

Same Result:

I set the working folder successfully
Succeeds:
plink -P 44022 ehansen@ghelsiapp "SETWORKINGFOLDER -repository NZLoad -forcesubfolderstoinherit $/ntzload \\ghtestetl1\src_ntzload "

Failed:
plink -P 44022 ehansen@ghelsiapp "DIFF -repository NZLoad -compareto lastget -norecursive -vaultdiff \"C:\\Program Files (x86)\\SourceGear\\Vault Client\\sgdm.exe\" $/ntzload/scripts/dirValidate.sh"

<vault>
<error>
<exception>System.Exception: Item Unknown Item could not be found.
at VaultClientIntegrationLib.ServerOperations.ProcessCommandDiff(String diffP
rogram, String diffArguments, CompareToOption compareToOption, Boolean recursive
, String objectPathLeft, String objectPathRight)
at VaultCmdLineClient.VaultCmdLineClient.ProcessCommand(Args curArg)
at VaultCmdLineClient.VaultCmdLineClient.Main(String[] args)</exception>
</error>
<result>
<success>False</success>
</result>
</vault>
Thanks!

Eric

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Fri Apr 29, 2016 4:01 pm

I couldn't find that error message off the bat. Some more digging uncovered the code.

Code: Select all

if (objectPathLeft == null) { objectPathLeft = "Unknown Item"; }
if (objectPathRight == null) { objectPathRight = "Unknown Item"; }
...
strMessage = string.Format("Item {0} could not be found.", objectPathLeft);
...
strMessage = string.Format("Item {0} could not be found.", objectPathRight);
So, it appears as if one of the files cannot be found. Can you verify the file exists in both the working folder and the corresponding _sgvault directory?

Another check you might want to try is to use '-compareto current'. If this fails, then you know there's something wrong with the working folder's file. If this works, then the corresponding file in _sgvault folder must be missing.
Jeff Clausius
SourceGear

jclausius
Posts: 3702
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: C# API To get Diff btwn Current and Previous Check in

Post by jclausius » Mon May 02, 2016 9:33 am

For other viewers, if anyone is trying to find out what has changed between two labels, a date and a label, or two dates? The manifest found in the CREATE_FCA command should give you the files that have changed between those two points in time.
Jeff Clausius
SourceGear

ejhansen71
Posts: 60
Joined: Thu Nov 13, 2014 10:12 am
Location: Lake in the Hills, IL

Re: C# API To get Diff btwn Current and Previous Check in

Post by ejhansen71 » Mon May 02, 2016 4:45 pm

I could not get it to work on 2 different repositories witrh 2 different files, but it finally worked on the 3rd one I tried.

Result?
<vault>
<result>
<success>True</success>
</result>
</vault>

Excellent! Now the dumb part, how do I actually get the diff?
Thanks!

Eric

Post Reply