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