Skip to content

Instantly share code, notes, and snippets.

@jsoriano
Last active August 29, 2015 14:07
Show Gist options
  • Save jsoriano/bacb188e7a60093c6497 to your computer and use it in GitHub Desktop.
Save jsoriano/bacb188e7a60093c6497 to your computer and use it in GitHub Desktop.
Script to reproduce libgit2#2515
#include <stdio.h>
#include <git2.h>
int main(int argc, char *argv[]) {
int error;
git_repository *repo = NULL;
git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
git_threads_init();
error = git_repository_open(&repo, argv[1]);
if (error < 0) goto error;
options.checkout_strategy = GIT_CHECKOUT_FORCE;
error = git_checkout_head(repo, &options);
if (error < 0) goto error;
git_repository_free(repo);
return 0;
error:
{
const git_error *e = giterr_last();
printf("Error %d/%d: %s\n", error, e->klass, e->message);
exit(error);
}
}
import pygit2
import sys
import os.path
import sh
def write_to_file(repository, path, content):
with open(os.path.join(repository.workdir, path), 'w') as f:
f.write(content)
repository.index.add(path)
def commit(repository, signature):
repository.index.write()
tree = repository.index.write_tree()
parents = []
if repository.head_is_unborn:
tip = 'refs/heads/master'
else:
tip = repository.head.name
parents.append(repository.head.target)
return repository.create_commit(
tip,
signature, signature, 'message',
tree, parents
)
if __name__ == '__main__':
print 'pygit2', pygit2.__version__
path = sys.argv[1]
signature = pygit2.Signature('Alice', '[email protected]')
repository = pygit2.init_repository(path)
conflict_file = 'conflict'
write_to_file(repository, conflict_file, 'A')
commit(repository, signature)
first_commit = repository.head.get_object()
write_to_file(repository, conflict_file, 'B')
commit(repository, signature)
branch = repository.create_branch('branch1', first_commit)
repository.checkout(branch.name)
write_to_file(repository, conflict_file, 'C')
commit(repository, signature)
repository.merge(repository.lookup_branch('master').target)
repository.checkout_head(strategy=pygit2.GIT_CHECKOUT_FORCE)
print sh.git('--git-dir=' + repository.path, 'ls-files', '-s')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment