Pregunta

When I try to do the following, i simply end up generating empty commits; that is, it creates a commit with nothing changed in it.

string filename = "path/to/file";
repo.Index.Stage(filename);
repo.Commit();

please note that the path is relative, meaning, i create my Repository using Repository repo = new Repository("."); and then use paths (called filename above) like ".gitignore" or "files/text.txt" which seems to be the correct way to do it, but it doesn't work.

The code that demonstrates this "in action" can be found in full here but is essentially ran though like this:

int count = 0;
//iterate all entries in the index (correct me if i'm wrong, but this should be all the changed files? or is it just all the files..)
//this is in AddWindow.cs
foreach (LibGit2Sharp.IndexEntry entry in vcs.GetRepository().Index)
        {
            //store an the entry.Path as Representation in an Object FileToggle
            toggles[count] = new FileToggle(entry.Path);
            count++;
        }

//then later, we do this (also in AddWindow.cs)
foreach (FileToggle f in toggles)
        {
            if (f.GetToggle()) //this is a bool set by user, so lets assume its on
                vcs.GetRepository().Index.Stage(f.GetRepresenation()); //stage all selected files, using their representation (ie entry.Path set above)
        } 

//and then finally, later, we do this in CommitWindow.cs
   if (message != null && signature != null && email != null)
                    vcs.GetRepository().Commit(message, new LibGit2Sharp.Signature(signature, email, System.DateTimeOffset.Now), new LibGit2Sharp.Signature(signature, email, System.DateTimeOffset.Now));

it's also important to note that vcs.GetRepository() returns a Repository
you can also see some examples of blank commits, as i used the tool to try to commit itself. check out one here.

Furthermore, I have verified that I'm attempting to stage and then commit files that are marked as modified in the command line, using git status. So, I am not just attempting to commit files that are not modified, nor am i commiting files that are already staged. I am (as far as i know) trying to do this .Stage() .Commit() process for files that ARE in need of update.

Update

as per nulltoken's requests:

Add Console.WriteLine(string.Format("{0} - {1}", f.GetRepresenation(), vcs.GetRepository().Index.RetrieveStatus(f.GetRepresenation()))) before the line invoking Index.Stage().

the resulting output when staging my "test" files is

Assets\editor\GitTool\AddWindow.cs - Modified
Assets\editor\GitTool\CommitWindow.cs - Modified

Add a breakpoint on the line performing the call to repo.Commit Run the code. When you hit the breakpoint, please run git status against the repository pointed at by vcs.GetRepository()

this yields the output of git status indicating

# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Assets/editor/GitTool/AddWindow.cs
#       modified:   Assets/editor/GitTool/CommitWindow.cs

both before and after letting Commit() execute. Although after, it does contain

# Your branch is ahead of 'origin/master' by 1 commit. as well.

Update 2

This behaves as expected, producing the following output after staging:

# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   Assets/editor/GitTool/AddWindow.cs

then i call Commit immediately, and i get:

# Your branch is ahead of 'origin/master' by 2 commits.
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   Assets/editor/GitTool/AddWindow.cs

so, the ahead by count incremented up once, indicating there was a commit, but the file is still being reported as newly changed, even though to the best of my knowledge it isn't!

update 3

i just noticed something upon running git diff. the differences being picked up there are file mode changes (ie 100644 to 100755) perhaps this is whats actually happening. I'm going to try everything with a file that can CLEARLY remain static (won't be open ANYWHERE and see if the system is functioning like git properly, but the editor is doing some weird file mode things).

update 4

in response to update 3, i realized that i am an idiot. lib2gitsharp is functioning properly, but i was stupidly not checking to see if anything that github (apparently) doesn't visually show had changed. In this case, the file modes of the things i was trying to Stage and Commit were changing constantly (for whatever reason, though i'm blaming unity). This was causing it to appear that nothing was changing, when really stuff was changing, just not file content. So all in all, this question answered itself, it just took me way too long to realize it.

I'll also take a minute to give a shoutout to nulltoken because despite my inability to realize little things, he provided some great help, that got me there in the end.

¿Fue útil?

Solución 2

The question contains the answer, as to successfully stage and then commit files, you must simply

//the path to our repo
string pathToRepo = ".";

//create a new repo
Repository r = new Repository(pathToRepo);

//call stage on all files you wish to stage (stage takes their filenames as strings)
r.Index.Stage("testfile.txt");

//note that below, signatures take a username string, an email string, and a System.DateTimeOffset

//call Commit on the repo, giving a message (string), an author(Signature), and a commit (Signature)
r.Commit("updating testfile.txt",new Signature("username","email",System.DateTimeOffset.Now),new Signature("username","email",System.DateTimeOffset.Now));

//and you've successfully commited to your repo.

i was simply being an idiot, and not realizing that things were being updated.

Otros consejos

you can also see some examples of blank commits, as i used the tool to try to commit itself. check out one here.

Actually, the commit is not empty. It contains the snapshot of the whole repository. You can check this by clicking the blue Browse Code of the far right of the screen, which leads to displaying the content of this commit.

If the "empty commit" is the issue, my guess would be that you're currently staging unaltered files (ie. files with no modifications, when compared to the parent commit). Which leads the GitHub interface to show no "changes".

You can easily check this by inserting a call to Index.RetrieveStatus(f.GetRepresenation()); just before the line Index.Stage(f.GetRepresenation());.

If I'm correct, the returned FileStatus should be Unaltered.

Update

Furthermore, I have verified that I'm attempting to stage and then commit files that are marked as modified in the command line, using git status.

That's very strange. Let's try something else.

Could you please:

  • Add Console.WriteLine(string.Format("{0} - {1}", f.GetRepresenation(), vcs.GetRepository().Index.RetrieveStatus(f.GetRepresenation()))) before the line invoking Index.Stage().
  • Add a breakpoint on the line performing the call to repo.Commit
  • Run the code. When you hit the breakpoint, please run git status against the repository pointed at by vcs.GetRepository()
  • Update your question with the console output and the git status result.

Update 2

Thanks for the update. Now, what happens if you also add another Console.WriteLine call after the Stage invocation? This should produce two output lines in the Console per file (one being Modified, the second one being Staged), and the git status call should list the same files under a Changes to be committed section.

If this performs accordingly to what's above, can you add a repo.Commit() call immediately after the foreach loop that stages the files? This should produce a Commit including changes.

Update 3

so, the ahead by count incremented up once, indicating there was a commit

At this very point, issuing git diff HEAD~1..HEAD should list the changes that have just been committed. Are there any?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top