Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jgit-dev] Commit to bare repo.

Hi Folks.

I've got some problems during trying to write some sample code to make commit to existing bare repo with commits. 

Here's my code: 

public static void main(String[] args) throws Exception {
        Repository repository = new FileRepositoryBuilder().
                setGitDir(new File("/home/tweek/test.git/")).
                readEnvironment().findGitDir().build();

        System.out.println(repository.getDirectory());

        ObjectInserter inserter = repository.newObjectInserter();

        byte[] data = "">
        ObjectId fileId = inserter.insert(Constants.OBJ_BLOB, data, 0, data.length);

        TreeFormatter formatter = new TreeFormatter();
        formatter.append("README", FileMode.REGULAR_FILE, fileId);

        ObjectId treeId = inserter.insert(formatter);

        AnyObjectId headID = repository.resolve(Constants.HEAD);
        System.out.println(headID);

        PersonIdent person = new PersonIdent("Your Name", "noreply@xxxxxxxxxxx");
        CommitBuilder commit = new CommitBuilder();
        commit.setTreeId(treeId);
        commit.setAuthor(person);
        commit.setCommitter(person);
        commit.setMessage("Commit");
        commit.setParentId(headID.toObjectId());

        ObjectId commitId = inserter.insert(commit);
        inserter.flush();

        RefUpdate ru = repository.updateRef(Constants.HEAD);
        ru.setForceUpdate(false);
        ru.setRefLogIdent(person);
        ru.setNewObjectId(commitId);
        ru.setExpectedOldObjectId(headID);

        System.out.println(ru.update());
    }

Everything is looking good. Git log command at bare repo returned proper tree.

But the problem is when i make git pull at working copy - all files except one with changes (README) has been deleted. 

Can anyone tell me what i'm doing wrong??

--
Kind Regards
Darek Sztwiorok

Back to the top