当前位置 : 主页 > 编程语言 > c++ >

java将git代码导入svn并覆盖trunk中的内容

来源:互联网 收集:自由互联 发布时间:2021-06-30
依赖及步骤说明 java操作git仓库的代码参见:https://gitee.com/chunsw/codes/ao3pmcdl608q45h9wxg1y33java操作svn的maven依赖为: org.tmatesoft.svnkit svnkit 1.8.7 git代码导入svn并覆盖trunk的步骤: 1、通过jgit库
依赖及步骤说明
java操作git仓库的代码参见:
https://gitee.com/chunsw/codes/ao3pmcdl608q45h9wxg1y33

java操作svn的maven依赖为:

        
 
            
  
   org.tmatesoft.svnkit
  
            
  
   svnkit
  
            
  
   1.8.7
  
        
 

git代码导入svn并覆盖trunk的步骤:
   1、通过jgit库操作git仓库,并将要导入svn的tag或分支checkout
   2、传递git仓库的本地文件夹目录地址给操作svn的方法,在方法中完成后续步骤
SVN初始化设置(含导入svn时要忽略的目录配置,例如“.git”)
private ConcurrentHashMap
 
   svnRepositoryMap = new ConcurrentHashMap<>();
    private SVNClientManager svnClientManager;
    private ISVNAuthenticationManager authManager;

    @Value("${svn.username}")
    private String svnUserName;
    @Value("${svn.password}")
    private String svnPassword;

    @Override
    public void afterPropertiesSet() throws Exception {
        SVNRepositoryFactoryImpl.setup();
        authManager = SVNWCUtil.createDefaultAuthenticationManager(svnUserName, svnPassword); // 创建身份验证管理器

        svnClientManager = SVNClientManager.newInstance();
        DefaultSVNOptions svnOptions = new DefaultSVNOptions();
        svnOptions.addIgnorePattern(".git");
        svnClientManager.setOptions(svnOptions);
    }

    private ConcurrentHashMap
  
    lockMap = new ConcurrentHashMap
   
    (); private ReentrantLock getLock(String svnPath) { if (lockMap.get(svnPath) == null) { lockMap.putIfAbsent(svnPath, new ReentrantLock(true)); // 公平锁 } return lockMap.get(svnPath); } private SVNRepository getSVNRepository(String svnPath) throws Exception { SVNRepository svnRepository = svnRepositoryMap.get(svnPath); if (svnRepository == null) { SVNRepository repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(svnPath)); repository.setAuthenticationManager(authManager); svnRepositoryMap.putIfAbsent(svnPath, repository); svnRepository = svnRepositoryMap.get(svnPath); } return svnRepository; }
   
  
 
删除svn特定目录的内容
/**
     * @param svnPath 例如: svn://ip:port/test/trunk
     */
    public void deleteTrunk(String svnPath) throws Exception {
        if (StringUtils.isBlank(svnPath))
            return;

        ReentrantLock lock = getLock(svnPath);
        try {
            lock.lock();
            SVNRepository svnRepository = getSVNRepository(svnPath);

            // 查询trunk下的所有文件和目录
            List
 
   dirEntryList = new LinkedList<>();
            svnRepository.getDir("", -1, false, dirEntryList);

            List
  
    urlList = new LinkedList<>(); for (SVNDirEntry entry : dirEntryList) { urlList.add(entry.getURL()); } // 如果有文件或目录存在,则删除 if (urlList.size() > 0) { SVNCommitClient commitClient = svnClientManager.getCommitClient(); commitClient.doDelete(urlList.toArray(new SVNURL[urlList.size()]), "delete files and folders before import"); } } catch (Exception e) { throw new RuntimeException(String.format("delete trunk failed: %s", svnPath), e); } finally { lock.unlock(); } }
  
 
将本地文件夹导入到svn
/**
     * @param svnPath 例如: svn://ip:port/test/trunk
     */
    public void doImport(String svnPath, File directoryToImport) throws Exception {
        if (directoryToImport == null || !directoryToImport.exists() || !directoryToImport.isDirectory())
            return;

        ReentrantLock lock = getLock(svnPath);
        try {
            lock.lock();
            SVNCommitClient commitClient = svnClientManager.getCommitClient();
            // useGlobalIgnores为true能过滤掉名为.git的目录,已在afterPropertiesSet方法中设置DefaultSVNOptions.addIgnorePattern(".git")
            commitClient.doImport(directoryToImport, SVNURL.parseURIEncoded(svnPath),
                    "import files and folders", null, true,
                    false, SVNDepth.INFINITY);
        } catch (Exception e) {
            throw new RuntimeException(String.format("import into trunk failed: %s", svnPath), e);
        } finally {
            lock.unlock();
        }
    }
网友评论