4

I want to programmatically find out if a workspace has the latest files. I don't want to do a Workspace.Get(), because that performs the equivalent of "Get Latest". I just want to know if my workspace needs a "Get Latest" or not.

I'm doing this check during a Build.I plan on having a method like so:

public static bool HasLatestFiles(Workspace ws)
{
    bool hasChanges = false;

    /* need correct code to query if ws has latest files */

    return hasChanges;
}

What is the correct code to use?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
MRothaus
  • 45
  • 4
  • PS: I found the `hasChanges=false` very confusing. Since you'd need to return the inverse to tell if the user has the latest files. – jessehouwing Oct 25 '14 at 12:29
  • PS: If you plan to do a get-latest anyway if the workspace is 'stale', then you might as well just call Get-Latest. That's faster. – jessehouwing Oct 25 '14 at 13:02
  • 1
    Sorry about the "hasChanges=false", that was leftover from several different things I tried. I do not plan to do a get-latest, but I want to inform the builder that he doesn't have the latest files. – MRothaus Oct 25 '14 at 16:43

1 Answers1

8

Use Workspace.Get(LatestVersionSpec.Instance, GetOptions.Preview) then check the GetStatus.NoActionNeeded that is yielded by the Get operation.

So:

public static bool HasLatestFiles(Workspace ws)
{
    GetStatus result = ws.Get(LatestVersionSpec.Instance, GetOptions.Preview);

    bool hasLatestFiles = result.NoActionNeeded;

    return hasLatestFiles;
}
MRothaus
  • 45
  • 4
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • How do you get the latest version? I noticed that using the command in Source Control in VS.NET is significantly faster than calling the API `workspace.Get(new GetRequest(targetBranch, RecursionType.Full, VersionSpec.Latest), GetOptions.GetAll);` – Karel Frajták Oct 26 '17 at 21:15
  • I think you don't want getall, that forces all files to ge overwritten, not just changed files. – jessehouwing Oct 26 '17 at 21:32