JGit 创建项目

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://581854@git.xxx.bj/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://581854@git.xxx.bj/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("黄", "huang@xxx.com").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>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值