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.