JGit create project

import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by 581854 on 2017-06-09 10:03.
 * GitTest
 */
public class GitTest {

    public static void main(String[] args) throws GitAPIException, IOException {

        File dir = new File("/test");

        String url = "http://[email protected]/archg/test.git";
        String name = "581854";
        String password = "******";

// credentials
        CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(name, password);

        Git git = Git.init().setDirectory( dir ).call();

        StoredConfig config = git.getRepository (). GetConfig ();
        config.setString("remote", "origin", "url", "http://[email protected]/archg/test.git");
        config.save();

// clone
//        CloneCommand cloneCommand = new CloneCommand().setCredentialsProvider(credentialsProvider).setDirectory(dir).setURI(url);
// Git git = cloneCommand.call();

// add
        AddCommand addCommand = git.add();
        addCommand.addFilepattern(".");
        addCommand.call();

// commit
        CommitCommand commit = git.commit();
        commit.setCommitter("黄", "[email protected]").setMessage("Add all project template files ...");
        commit.call();
// push
        PushCommand pushCommand = git.push();
        pushCommand.setCredentialsProvider(credentialsProvider).setForce(true).setPushAll();

        Iterator<PushResult> it = pushCommand.call().iterator();
        if (it.hasNext()) {
            System.out.println(it.next().toString());
        }
// cleanup
        //dir.deleteOnExit();
    }

}

<dependency>
    <groupId>org.eclipse.jgit</groupId>
    <artifactId>org.eclipse.jgit</artifactId>
    <version>4.4.0.201606070830-r</version>
</dependency>

 

Guess you like

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