简体   繁体   中英

Pushing files to remote repository using jGit

I need to programattically push files using jGit from different local repositories to one remote repository hosted on Github. The code below works with no exceptions, but nothing is being changed in Github.

Every user defined 'group' in my program has its own folder under another folder called data. And every group folder contains a folder called repo, which contains an html and css file. When the time's right, I need to push these two files to github.

Here's the structure of the folders:

program (folder)
     [reciter.jar]
     [start.bat]
     data (folder)
          <group 1> (group folder)
               repo (group specific repository folder)
                    [resultsFile.html]
                    [styleFile.css]
          <another group> (group folder)
               ... (omitted)

Here's what I need to do: I want make one repository one Github. (done: https://github.com/Skultrix/reciter.git ) When a group pushes their files to Github, I need them to be in this order:

github's root
     <group 1>
          resultsFile.html
          styleFile.css
     <another group>
          resultsFile.html
          styleFile.css

Meaning, if I want to access "another group"'s html file, the path would be another_group/results.html.

Here's what I have tried with jGit:

    public void load() {
        try {
            git = Git.init().setGitDir(group.getDataManager().getRepoDirectory()).call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //load() is always before sendFile()
    public void sendFile() {
        LoadingModal.showModal(main, () -> {
            try {
                System.out.println("adding");
                git.add()
                        .addFilepattern("resultsFile.html")
                        .call();
                System.out.println("commiting");
                git.commit()
                        .setAll(true)
                        .setMessage("Update results")
                        .call();
                System.out.println("remote adding");
                git.remoteAdd()
                        .setName("origin")
                        .setUri(new URIish("https://github.com/Skultrix/reciter.git"))
                        .call();
                System.out.println("pushing");
                git.push()
                        .setRemote("https://github.com/Skultrix/reciter.git")
                        .setCredentialsProvider(
                                new UsernamePasswordCredentialsProvider("skultrix", <my password>)
                        )
                        .call();
                System.out.println("finish");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "Uploading results to site.", true).run();
    }

When the code above is executed (#sendFile) the console prints:

adding
commiting
remote adding
pushing
finish

But when I check github, nothing is changed, and not even empty commits are made. No exceptions or errors either.

Thanks for any assistance or guidance in advance.

You might need to add the path of that file (not just its name), as in this example

adder.addFilepattern(getPath(fileVersion.getFile(), repository));

The path can be relative to the repository , so <group 1> in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM