Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Invalid history entries after push
Invalid history entries after push [message #1760250] Tue, 25 April 2017 08:18 Go to next message
 is currently offline Friend
Messages: 20
Registered: September 2015
Junior Member
Hi all,
I have faced following problem with JGit. Let's say, there is repo A with .git dir. Next, there is subdirectory B in this repo.

My aim is to add, commit and push new file to B subdirectory.

Repo initialization (LOCAL_PATH i A directory path):

git = Git.open(new File(propertiesManager.get(ProperiesUtils.LOCAL_PATH)))


Adding new file:
git.add().addFilepattern("B/"+this.fileName).call()


Commit:
git.commit().setMessage("Comment#1").call()


Push to remote:
git.push().setRemote("origin").call()


Finally, when I create second file and use this code to push it to B subdirectory with comment "Comment#2" I'll meet very strange file commits history (looks like history from previous files is connected to new files):
Comment#2
Comment#1

Could you please explain what is wrong in my case?






Re: Invalid history entries after push [message #1760254 is a reply to message #1760250] Tue, 25 April 2017 08:40 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
Don't get your problem. If I understood you right you are doing two commits in a row on the same branch. And when you inspect the history you wonder why the second commit is based on the first one. What do you expect?

Here is what I understood what you do in native git:

> git init test
Initialized empty Git repository in /private/tmp/test/.git/
> cd test/
> mkdir b
> touch b/f
> git add b/f
> git commit -m 'add b/f'
[master (root-commit) 1596ef9] add b/f
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 b/f
> touch g
> git add g
> git commit -m 'add g'
[master a5192a9] add g
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 g
> git log --oneline --decorate --graph --all
* a5192a9 (HEAD -> master) add g
* 1596ef9 add b/f


Ciao
Chris
Re: Invalid history entries after push [message #1760256 is a reply to message #1760254] Tue, 25 April 2017 09:03 Go to previous messageGo to next message
 is currently offline Friend
Messages: 20
Registered: September 2015
Junior Member
In general, I try to add one file, commit and push and do same operation with other file. If I try do it in main repo dir, it works fine. The problem is with subdirectories.
Re: Invalid history entries after push [message #1760260 is a reply to message #1760256] Tue, 25 April 2017 09:11 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
My example is also with subdirectories. How do you inspect the history? Maybe add some some java program showing the problem

Ciao
Chris
Re: Invalid history entries after push [message #1760264 is a reply to message #1760260] Tue, 25 April 2017 09:19 Go to previous messageGo to next message
 is currently offline Friend
Messages: 20
Registered: September 2015
Junior Member
I check particular files in GitLab. I run attached code for single file - it is copied to local repo subdir and pushed to remote. When I run this code second time (for other file) it is pushed, but file contains history form first run :/

JAVA code:
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.SubmoduleAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.util.FS;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

public class GitManager {
    private static final String DIR_PATH = "patch";
    private Git git;
    private PropertiesManager propertiesManager;
    private String dirPath, fileName;

    public GitManager(PropertiesManager propertiesManager) {
        this.propertiesManager = propertiesManager;
        this.dirPath = propertiesManager.get(ProperiesUtils.LOCAL_PATH) + "/" + DIR_PATH;
    }

    public void save() throws IOException {
        try {
            connect();
            this.fileName = generateFileName();
            copyFiles();
            add();
            commit();
            push();
        } catch (Exception e) {
            e.printStackTrace();
        }
        git.close();
    }

    public void add() throws IOException, GitAPIException {
        git.add().addFilepattern(DIR_PATH + "/" + this.fileName).call();
    }

    public void commit() throws IOException, GitAPIException, JGitInternalException {
        Object command = git.commit().setMessage("Added new patch file").call();
    }

    public void push() throws IOException, JGitInternalException, GitAPIException {
        Object result = git.push().call();
    }

    private void connect() throws IOException, URISyntaxException, GitAPIException {
        final JschConfigSessionFactory f = new JschConfigSessionFactory() {
            @Override
            protected void configure(OpenSshConfig.Host host, Session sn) {
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch defaultJSch = super.createDefaultJSch(fs);
                defaultJSch.addIdentity(propertiesManager.get(ProperiesUtils.PRIVATE_KEY), propertiesManager.get(ProperiesUtils.PASSPHRASE));
                return defaultJSch;
            }
        };
        JschConfigSessionFactory.setInstance(f);

        git = Git.open(new File(propertiesManager.get(ProperiesUtils.LOCAL_PATH)));
        git.pull().call();
    }


    private void copyFiles() throws IOException {
        Path source = Paths.get(propertiesManager.get(ProperiesUtils.FILE));
        Path destination = Paths.get(propertiesManager.get(ProperiesUtils.LOCAL_PATH) + "/" + DIR_PATH + "/" + this.fileName);
        Files.copy(source, destination);
    }
}

Re: Invalid history entries after push [message #1760273 is a reply to message #1760264] Tue, 25 April 2017 12:06 Go to previous messageGo to next message
 is currently offline Friend
Messages: 20
Registered: September 2015
Junior Member
OK, code works fine. However, there is problem with GitLab/InteliJ git plugin (when two files with same content are pushed, their history is merged in GitLab/InteliJ - I've checked in shell (using gitk) and history is displayed correctly)

[Updated on: Tue, 25 April 2017 12:08]

Report message to a moderator

Re: Invalid history entries after push [message #1760275 is a reply to message #1760264] Tue, 25 April 2017 12:12 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
Looks all good - your code runs (slightly modified) on my side. But where is your problem? For me everyting looks like expected.


Ciao
Chris
Previous Topic:egit shows ">" on all files in newly cloned project
Next Topic:different status between JGit and console command "git status"
Goto Forum:
  


Current Time: Mon Sep 25 16:58:12 GMT 2017

Powered by FUDForum. Page generated in 0.03956 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software