CreateBranch.java - AWS Code Sample

CreateBranch.java

CreateBranch.java demonstrates how to create a branch.

Note

To run this Java (SDK V2) code example, ensure that you have set up your development environment, including your credentials. For more information, see AWS SDK for Java 2.x Developer Guide.

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ package com.example.commit; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.codecommit.CodeCommitClient; import software.amazon.awssdk.services.codecommit.model.CodeCommitException; import software.amazon.awssdk.services.codecommit.model.CreateBranchRequest; /** * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateBranch { public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateBranch <repoName> <branchName> <commitId> \n\n" + "Where:\n" + " repoName - the name of the repository.\n" + " branchName - the name of the branch.\n" + " commitId - The ID of the commit to point the new branch to. \n"; if (args.length != 3) { System.out.println(USAGE); System.exit(1); } String repoName = args[0]; String branchName = args[1]; String commitId = args[2]; Region region = Region.US_EAST_1; CodeCommitClient codeCommitClient = CodeCommitClient.builder() .region(region) .build(); createSpecificBranch(codeCommitClient, repoName, branchName, commitId); codeCommitClient.close(); } public static void createSpecificBranch(CodeCommitClient codeCommitClient, String repoName, String branchName, String commitId) { try { CreateBranchRequest branchRequest = CreateBranchRequest.builder() .branchName(branchName) .repositoryName(repoName) .commitId(commitId) .build(); codeCommitClient.createBranch(branchRequest); System.out.println("Branch "+branchName + " was created"); } catch (CodeCommitException e) { System.err.println(e.getMessage()); System.exit(1); } } }

Sample Details

Service: AWS CodeCommit

Last tested: 11/03/2020

Author: AWS - scmacdon

Type: full-example