Git also has a java api package, which can easily use java operations to submit code management activities for branching

http://www.cnblogs.com/songshu120/p/6180747.html


public class GitUtilClass {
    public static String localRepoPath = "D:/repo";
    public static String localRepoGitConfig = "D:/repo/.git";
    public static String remoteRepoURI = "[email protected]:wilson/test.git";
    public static String localCodeDir = "D:/platplat";
    
    /**
     * Create a new branch and sync it to the remote repository
     * @param branchName
     * @throws IOException
     * @throws GitAPIException
     */
    public static String newBranch(String branchName){
        String newBranchIndex = "refs/heads/"+branchName;
        String gitPathURI = "";
        Go Go;
        try {
            
            //Check whether the new branch already exists, if so, delete the existing branch forcibly and create a new branch
            List<Ref> refs = git.branchList().call();
            for (Ref ref : refs) {
                if (ref.getName().equals(newBranchIndex)) {
                    System.out.println("Removing branch before");
                    git.branchDelete().setBranchNames(branchName).setForce(true)
                            .call();
                    break;
                }
            }            
            // create a new branch
            Ref ref = git.branchCreate().setName(branchName).call();
            // push to remote
            git.push().add(ref).call();
            gitPathURI = remoteRepoURI + " " + "feature/" + branchName;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (GitAPIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        
        return gitPathURI;                
    }
    
    public static void commitFiles() throws IOException, GitAPIException{
        String filePath = "";
        Git git = Git.open( new File(localRepoGitConfig) );
        // Process of creating user files
        File myfile = new File(filePath);
        myfile.createNewFile();
        git.add().addFilepattern("pets").call();   
        //submit
        git.commit().setMessage("Added pets").call();   
        // push to remote
        git.push().call();
    }
    
    public static boolean pullBranchToLocal(String cloneURL){
        boolean resultFlag = false;
        String[] splitURL = cloneURL.split(" ");
        String branchName = splitURL [1];
        String fileDir = localCodeDir+"/"+branchName;
        //Check if the destination folder exists
        File file = new File(fileDir);
        if(file.exists()){
            deleteFolder(file);
        }
        Go Go;
        try {
            git = Git.open( new File(localRepoGitConfig) );
            git.cloneRepository().setURI(cloneURL).setDirectory(file).call();
            resultFlag = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (GitAPIException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        return resultFlag;    
    }
    
    public static void deleteFolder(File file){
        if(file.isFile() || file.list().length==0){
            file.delete();
        }else{
            File[] files = file.listFiles();
            for(int i=0;i<files.length;i++){
                deleteFolder(files[i]);
                files[i].delete();
            }
        }
    }
    
    public static void setupRepo() throws GitAPIException{
        / / Establish a connection with the remote warehouse, only need to execute once
        Git git = Git.cloneRepository().setURI(remoteRepoURI).setDirectory(new File(localRepoPath)).call();
    }
    
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228925&siteId=291194637