Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFS JAVA SDK 14.0.3 - How to fetch a remote file from server and make changes and check in again #260

Open
AnsarSamad opened this issue Apr 22, 2018 · 0 comments

Comments

@AnsarSamad
Copy link

AnsarSamad commented Apr 22, 2018

I have a project in team foundation server named master-builds . the master-build folder has a xml file named build-main.xml

i want to fetch this files from server and make some changes in it (change the version number ) and check in again , how can i achieve it using TFS JAVA SDK 14.0.3 .

I tried the samples provided in the folder but not able to achieve

This is the code snippet to create workspace

public static Workspace createAndMapWorkspace(final TFSTeamProjectCollection tpc) {
        final String workspaceName = "SampleVCWorkspace" + System.currentTimeMillis(); //$NON-NLS-1$
        Workspace workspace = null;

        // Get the workspace
        workspace = tpc.getVersionControlClient().tryGetWorkspace(ConsoleSettings.MAPPING_LOCAL_PATH);

        // Create and map the workspace if it does not exist
        if (workspace == null) {
            workspace = tpc.getVersionControlClient().createWorkspace(
                null,
                workspaceName,
                "Sample workspace comment", //$NON-NLS-1$
                WorkspaceLocation.SERVER,
                null,
                WorkspacePermissionProfile.getPrivateProfile());

            // Map the workspace
            final WorkingFolder workingFolder = new WorkingFolder(
                ConsoleSettings.MAPPING_SERVER_PATH,
                LocalPath.canonicalize(ConsoleSettings.MAPPING_LOCAL_PATH));
            workspace.createWorkingFolder(workingFolder);
        }

        System.out.println("Workspace '" + workspaceName + "' now exists and is mapped"); //$NON-NLS-1$ //$NON-NLS-2$

        return workspace;
    }
public static void getLatest(final Workspace workspace) {
        final ItemSpec spec = new ItemSpec(ConsoleSettings.MAPPING_LOCAL_PATH, RecursionType.FULL);
        final GetRequest request = new GetRequest(spec, LatestVersionSpec.INSTANCE);
        workspace.get(request, GetOptions.NONE);
    }

    public static File addFile(final Workspace workspace) {

        // Create the file locally
        final File newFile = new File(ConsoleSettings.MAPPING_LOCAL_PATH, "SampleAppFile"); //$NON-NLS-1$
        writeFileContents(newFile, "", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

        // Pend an add
        // The encoding is passed as null and the add will detect the encoding
        // of the file
        workspace.pendAdd(new String[] {
            newFile.getAbsolutePath()
        }, false, ENCODING, LockLevel.UNCHANGED, GetOptions.NONE, PendChangesOptions.NONE);

        // Checkin the pending change
        final int cs = checkinPendingChanges(workspace, "Adding a sample file"); //$NON-NLS-1$

        System.out.println("Added file " + newFile.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$

        return newFile;
    }

    public static void editFile(final Workspace workspace, final File file) {
        // Pend edit
        final ItemSpec fileSpec = new ItemSpec(file.getAbsolutePath(), RecursionType.NONE);
        workspace.pendEdit(
            new ItemSpec[] {
                fileSpec
        },
            LockLevel.UNCHANGED,
            ENCODING,

            GetOptions.NONE,
            PendChangesOptions.NONE);

        // Edit the file
        writeFileContents(file, "Edited this file at " + new Date().toString(), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

        // Checkin the pending change
        final int cs = checkinPendingChanges(workspace, "Editing the sample file"); //$NON-NLS-1$

        System.out.println("Edited file " + file.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$
    }

`
These are my questions

I was able to successfully add a new file to the remote server , but am not able to edit an existing remote file .

If i call editFile followed by addFile method it will get updated , but i dont want to add enw file every time ,what i want is to fetch the latest version of the buils-main.xml file from server and make changes and do the check in.

Also after creating the workspace in local path i couldnt see it in that path , is that the correct behavior ?

Could anyone Please help me on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant