|
| 1 | +/* |
| 2 | + * libgit2 "blame" example - shows how to use the blame API |
| 3 | + * |
| 4 | + * Written by the libgit2 contributors |
| 5 | + * |
| 6 | + * To the extent possible under law, the author(s) have dedicated all copyright |
| 7 | + * and related and neighboring rights to this software to the public domain |
| 8 | + * worldwide. This software is distributed without any warranty. |
| 9 | + * |
| 10 | + * You should have received a copy of the CC0 Public Domain Dedication along |
| 11 | + * with this software. If not, see |
| 12 | + * <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | + |
| 17 | +#include "common.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * This example implements the safe portion of the git's 'pull' |
| 21 | + * command. That is, merging fro upstream, which is the behaviour |
| 22 | + * which does not loose procedence information or reverse history. |
| 23 | + */ |
| 24 | +int main(int argc, char **argv) |
| 25 | +{ |
| 26 | + git_repository *repo; |
| 27 | + git_reference *current_branch, *upstream; |
| 28 | + git_buf remote_name = {0}; |
| 29 | + git_remote *remote; |
| 30 | + |
| 31 | + git_threads_init(); |
| 32 | + |
| 33 | + /** |
| 34 | + * Figure out what the current branch's upstream remote is so |
| 35 | + * we know from which remote to fetch |
| 36 | + */ |
| 37 | + check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "failed to open repo", NULL); |
| 38 | + check_lg2(git_repository_head(¤t_branch, repo), "failed to lookup current branch", NULL); |
| 39 | + check_lg2(git_branch_remote_name(&remote_name, repo, git_reference_name(current_branch)), |
| 40 | + "failed to get the reference's upstream", NULL); |
| 41 | + check_lg2(git_remote_load(&remote, repo, remote_name.ptr), "failed to load remote", NULL); |
| 42 | + git_buf_free(&remote_name); |
| 43 | + check_lg2(git_remote_fetch(remote, NULL, NULL), "failed to fetch from upstream", NULL); |
| 44 | + |
| 45 | + /** |
| 46 | + * Now that we have the updated data from the remote, look up |
| 47 | + * our branch's upstream and merge from it. |
| 48 | + */ |
| 49 | + { |
| 50 | + git_merge_head *merge_heads[1]; |
| 51 | + |
| 52 | + check_lg2(git_branch_upstream(&upstream, current_branch), "failed to get upstream branch", NULL); |
| 53 | + check_lg2(git_merge_head_from_ref(&merge_heads[0], repo, upstream), "failed to create merge head", NULL); |
| 54 | + check_lg2(git_merge(repo, (const git_merge_head **) merge_heads, 1, NULL, NULL), "failed to merge", NULL); |
| 55 | + git_merge_head_free(merge_heads[1]); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Once the merge operation succeeds, we need to check whether |
| 60 | + * there were any conflicts merging |
| 61 | + */ |
| 62 | + { |
| 63 | + git_index *index; |
| 64 | + int has_conflicts; |
| 65 | + |
| 66 | + check_lg2(git_repository_index(&index, repo), "failed to load index", NULL); |
| 67 | + has_conflicts = git_index_has_conflicts(index); |
| 68 | + |
| 69 | + git_index_free(index); |
| 70 | + |
| 71 | + if (has_conflicts) { |
| 72 | + printf("There were conflicts merging. Please resolve them and commit\n"); |
| 73 | + git_reference_free(upstream); |
| 74 | + git_reference_free(current_branch); |
| 75 | + git_repository_free(repo); |
| 76 | + |
| 77 | + return 0; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * If there were no conflicts, then we commit with the message |
| 83 | + * that was prepared by the merge operation. |
| 84 | + * |
| 85 | + * A tool would take this opportunity to spawn the user's |
| 86 | + * editor and let them change it, but that is outside of our |
| 87 | + * purpose here. |
| 88 | + * |
| 89 | + * |
| 90 | + */ |
| 91 | + { |
| 92 | + git_index *index; |
| 93 | + git_buf message = {0}; |
| 94 | + git_oid commit_id, tree_id; |
| 95 | + git_commit *parents[2]; |
| 96 | + git_signature *user; |
| 97 | + git_tree *tree; |
| 98 | + |
| 99 | + /* Get the contents of the index into the repo as the tree want for the commit */ |
| 100 | + check_lg2(git_repository_index(&index, repo), "failed to load index", NULL); |
| 101 | + check_lg2(git_index_write_tree(&tree_id, index), "failed to write tree", NULL); |
| 102 | + git_index_free(index); |
| 103 | + |
| 104 | + check_lg2(git_signature_default(&user, repo), "failed to get user's ident", NULL); |
| 105 | + check_lg2(git_repository_message(&message, repo), "failed to get message", NULL); |
| 106 | + |
| 107 | + check_lg2(git_tree_lookup(&tree, repo, &tree_id), "failed to lookup tree", NULL); |
| 108 | + |
| 109 | + check_lg2(git_commit_lookup(&parents[0], repo, git_reference_target(current_branch)), |
| 110 | + "failed to lookup first parent", NULL); |
| 111 | + check_lg2(git_commit_lookup(&parents[1], repo, git_reference_target(upstream)), |
| 112 | + "failed to lookup second parent", NULL); |
| 113 | + |
| 114 | + check_lg2(git_commit_create(&commit_id, repo, "HEAD", user, user, |
| 115 | + NULL, message.ptr, |
| 116 | + tree, 2, (const git_commit **) parents), |
| 117 | + "failed to create commit", NULL); |
| 118 | + |
| 119 | + git_tree_free(tree); |
| 120 | + git_signature_free(user); |
| 121 | + } |
| 122 | + |
| 123 | + git_reference_free(upstream); |
| 124 | + git_reference_free(current_branch); |
| 125 | + git_repository_free(repo); |
| 126 | + |
| 127 | + return 0; |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments