Как программно добавить, зафиксировать и pu sh в git в плагине intellij - PullRequest
0 голосов
/ 11 июля 2020

В рамках разработки моего плагина я хочу создать плагин, в котором я выберу содержимое файла и от sh до git, где пользователю нужно заполнить форму, содержащую URL-адрес репо, имя ветки, сообщение фиксации. Можем ли мы выполнить этот сценарий?

Я просмотрел git4idea, но тогда проект должен быть настроен VCS. Я хочу выполнить этот сценарий, в котором я выбираю контент и pu sh для любого репо. После выбора содержимого файла мы можем создать файл, который может быть выполнен, но необходимо, чтобы указать sh на любое репо из вашего текущего рабочего проекта, введя URL-адрес репо. Я пытался, но не смог получить для этого подходящую документацию.

В eclipse с использованием j git мы можем сделать это легко, но в intellij мы не можем этого добиться.

// Код Eclipe

public boolean createLocalTempRepoAndPush(String fileContent, String fileName, String remoteUrl, String commitMessage, String branch) throws IOException, NoFilepatternException, GitAPIException {
    final File localPath;
    
    try (Repository repository = createNewRepository()) {
        localPath = repository.getWorkTree();
        // create the file
        File myFile = new File(repository.getDirectory().getParent().concat("/" +fileName));
        
        try (Git git = new Git(repository)) {
            
            System.out.println(repository.getDirectory().getParent().concat("/" +fileName));
            
            //open the file output
            BufferedWriter output = new BufferedWriter(new FileWriter(myFile));
            
            //write the fileContent to file
            output.write(fileContent);
            
            if(!myFile.exists()) {
                throw new IOException("Could not create file " + myFile);
            }

            // run the add
            git.add().addFilepattern(fileName).call();
            
            // and then commit the changes
            git.commit().setMessage(commitMessage).call();
            
            // set the branch as per user choice
            RefSpec spec = new RefSpec("refs/heads/master:refs/heads/" + branch);
            
            // push the changes to remote
            Object push = git.push().setRemote(remoteUrl).setRefSpecs(spec).setPushAll().setPushTags().setCredentialsProvider(null).call();
            System.out.println(push);
            System.out.println("Committed file " + myFile + " to repository at " + repository.getDirectory());
            
            if(push != null) {
                return true;
            }
            
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            //delete the temp repository once the file is committed
            myFile.delete();
            //FileUtils.cleanDirectory(localPath);
            //FileUtils.deleteDirectory(localPath);
        }
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}


public static Repository createNewRepository() throws IOException {
    // prepare a new folder
    File localPath = File.createTempFile("TestGitRepository", "");
    if(!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }
    
    // create the directory
    Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
    repository.create();
    return repository;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...