1 /* 2 * libgit2 "commit" example - shows how to create a git commit 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 * <https://creativecommons.org/publicdomain/zero/1.0/>. 13 */ 14 /** 15 * This example demonstrates the libgit2 commit APIs to roughly 16 * simulate `git commit` with the commit message argument. 17 * 18 * This does not have: 19 * 20 * - Robust error handling 21 * - Most of the `git commit` options 22 * 23 * This does have: 24 * 25 * - Example of performing a git commit with a comment 26 * 27 * License: $(LINK2 https://creativecommons.org/publicdomain/zero/1.0/, CC0 1.0 Universal) 28 */ 29 module libgit2.example.commit; 30 31 32 private static import core.stdc.stdio; 33 private static import core.stdc.string; 34 private static import libgit2.commit; 35 private static import libgit2.errors; 36 private static import libgit2.example.common; 37 private static import libgit2.index; 38 private static import libgit2.object; 39 private static import libgit2.oid; 40 private static import libgit2.refs; 41 private static import libgit2.repository; 42 private static import libgit2.revparse; 43 private static import libgit2.signature; 44 private static import libgit2.tree; 45 private static import libgit2.types; 46 47 extern (C) 48 nothrow @nogc 49 public int lg2_commit(libgit2.types.git_repository* repo, int argc, char** argv) 50 51 do 52 { 53 const (char)* opt = argv[1]; 54 const (char)* comment = argv[2]; 55 56 /* Validate args */ 57 if ((argc < 3) || (core.stdc..string.strcmp(opt, "-m") != 0)) { 58 core.stdc.stdio.printf("USAGE: %s -m <comment>\n", argv[0]); 59 60 return -1; 61 } 62 63 libgit2.types.git_object* parent = null; 64 libgit2.types.git_reference* ref_ = null; 65 int error = libgit2.revparse.git_revparse_ext(&parent, &ref_, repo, "HEAD"); 66 67 if (error == libgit2.errors.git_error_code.GIT_ENOTFOUND) { 68 core.stdc.stdio.printf("HEAD not found. Creating first commit\n"); 69 error = 0; 70 } else if (error != 0) { 71 const (libgit2.errors.git_error)* err = libgit2.errors.git_error_last(); 72 73 if (err) { 74 core.stdc.stdio.printf("ERROR %d: %s\n", err.klass, err.message); 75 } else { 76 core.stdc.stdio.printf("ERROR %d: no detailed info\n", error); 77 } 78 } 79 80 libgit2.types.git_index* index; 81 libgit2.example.common.check_lg2(libgit2.repository.git_repository_index(&index, repo), "Could not open repository index", null); 82 libgit2.oid.git_oid tree_oid; 83 libgit2.example.common.check_lg2(libgit2.index.git_index_write_tree(&tree_oid, index), "Could not write tree", null); 84 //; 85 libgit2.example.common.check_lg2(libgit2.index.git_index_write(index), "Could not write index", null); 86 //; 87 88 libgit2.types.git_tree* tree; 89 libgit2.example.common.check_lg2(libgit2.tree.git_tree_lookup(&tree, repo, &tree_oid), "Error looking up tree", null); 90 91 libgit2.types.git_signature* signature; 92 libgit2.example.common.check_lg2(libgit2.signature.git_signature_default(&signature, repo), "Error creating signature", null); 93 94 libgit2.oid.git_oid commit_oid; 95 libgit2.example.common.check_lg2(libgit2.commit.git_commit_create_v(&commit_oid, repo, "HEAD", signature, signature, null, comment, tree, (parent) ? (1) : (0), parent), "Error creating commit", null); 96 97 libgit2.index.git_index_free(index); 98 libgit2.signature.git_signature_free(signature); 99 libgit2.tree.git_tree_free(tree); 100 libgit2.object.git_object_free(parent); 101 libgit2.refs.git_reference_free(ref_); 102 103 return error; 104 }