1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>general.c</title>
<link rel="stylesheet" href="../css.css">
<style type="text/css">
a.fnlink {text-decoration: none}
a.fnlink:hover {text-decoration: underline}
</style>
</head>
<body>
<div id='container'>
<div id="background"></div>
<div id="jump_to">
Jump To …
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="../../#v0.24.0">API Docs</a>
<a class="source" href="add.html">add.c</a>
<a class="source" href="blame.html">blame.c</a>
<a class="source" href="cat-file.html">cat-file.c</a>
<a class="source" href="common.html">common.c</a>
<a class="source" href="describe.html">describe.c</a>
<a class="source" href="diff.html">diff.c</a>
<a class="source" href="for-each-ref.html">for-each-ref.c</a>
<a class="source" href="general.html">general.c</a>
<a class="source" href="init.html">init.c</a>
<a class="source" href="log.html">log.c</a>
<a class="source" href="network/clone.html">clone.c</a>
<a class="source" href="network/common.html">common.c</a>
<a class="source" href="network/fetch.html">fetch.c</a>
<a class="source" href="network/git2.html">git2.c</a>
<a class="source" href="network/index-pack.html">index-pack.c</a>
<a class="source" href="network/ls-remote.html">ls-remote.c</a>
<a class="source" href="remote.html">remote.c</a>
<a class="source" href="rev-list.html">rev-list.c</a>
<a class="source" href="rev-parse.html">rev-parse.c</a>
<a class="source" href="showindex.html">showindex.c</a>
<a class="source" href="status.html">status.c</a>
<a class="source" href="tag.html">tag.c</a>
</div>
</div>
</div>
<table cellspacing=0 cellpadding=0>
<thead>
<tr>
<th class=docs><h1>general.c</h1></th>
<th class=code></th>
</tr>
</thead>
<tbody>
<tr id='section-1'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</td>
<td class=code>
<div class='highlight'><pre><span class="cm">/*</span>
<span class="cm"> * libgit2 "general" example - shows basic libgit2 concepts</span>
<span class="cm"> *</span>
<span class="cm"> * Written by the libgit2 contributors</span>
<span class="cm"> *</span>
<span class="cm"> * To the extent possible under law, the author(s) have dedicated all copyright</span>
<span class="cm"> * and related and neighboring rights to this software to the public domain</span>
<span class="cm"> * worldwide. This software is distributed without any warranty.</span>
<span class="cm"> *</span>
<span class="cm"> * You should have received a copy of the CC0 Public Domain Dedication along</span>
<span class="cm"> * with this software. If not, see</span>
<span class="cm"> * <http://creativecommons.org/publicdomain/zero/1.0/>.</span>
<span class="cm"> */</span></pre></div>
</td>
</tr>
<tr id='section-2'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p><a href="http://libgit2.github.com"><strong>libgit2</strong></a> is a portable, pure C implementation of the Git core
methods provided as a re-entrant linkable library with a solid API,
allowing you to write native speed custom Git applications in any
language which supports C bindings.</p>
<p>This file is an example of using that API in a real, compilable C file.
As the API is updated, this file will be updated to demonstrate the new
functionality.</p>
<p>If you're trying to write something in C using <a href="http://libgit2.github.com">libgit2</a>, you should
also check out the generated <a href="http://libgit2.github.com/libgit2">API documentation</a>. We try to link to
the relevant sections of the API docs in each section in this file.</p>
<p><strong>libgit2</strong> (for the most part) only implements the core plumbing
functions, not really the higher level porcelain stuff. For a primer on
Git Internals that you will need to know to work with Git at this level,
check out <a href="http://progit.org/book/ch9-0.html">Chapter 9</a> of the Pro Git book.</p>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-Includes'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Includes">¶</a>
</div>
<h3>Includes</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-4'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Including the <code>git2.h</code> header will include all the other libgit2 headers
that you need. It should be the only thing you need to include in order
to compile properly and get all the libgit2 API.</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="cp">#include <git2.h></span>
<span class="cp">#include <stdio.h></span></pre></div>
</td>
</tr>
<tr id='section-5'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Almost all libgit2 functions return 0 on success or negative on error.
This is not production quality error checking, but should be sufficient
as an example.</p>
</td>
<td class=code>
<div class='highlight'><pre><span class="k">static</span> <span class="kt">void</span> <span class="nf">check_error</span><span class="p">(</span><span class="kt">int</span> <span class="n">error_code</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">action</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">const</span> <span class="n">git_error</span> <span class="o">*</span><span class="n">error</span> <span class="o">=</span> <span class="n"><a name="giterr_last-27" class="fnlink" href="../../#v0.24.0/group/giterr/giterr_last">giterr_last</a></span><span class="p">();</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">error_code</span><span class="p">)</span>
<span class="k">return</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Error %d %s - %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">error_code</span><span class="p">,</span> <span class="n">action</span><span class="p">,</span>
<span class="p">(</span><span class="n">error</span> <span class="o">&&</span> <span class="n">error</span><span class="o">-></span><span class="n">message</span><span class="p">)</span> <span class="o">?</span> <span class="n">error</span><span class="o">-></span><span class="nl">message</span> <span class="p">:</span> <span class="s">"???"</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">main</span> <span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">**</span> <span class="n">argv</span><span class="p">)</span>
<span class="p">{</span></pre></div>
</td>
</tr>
<tr id='section-6'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Initialize the library, this will set up any global state which libgit2 needs
including threading and crypto</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_libgit2_init-28" class="fnlink" href="../../#v0.24.0/group/libgit2/git_libgit2_init">git_libgit2_init</a></span><span class="p">();</span></pre></div>
</td>
</tr>
<tr id='section-Opening_the_Repository'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Opening_the_Repository">¶</a>
</div>
<h3>Opening the Repository</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-8'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>There are a couple of methods for opening a repository, this being the
simplest. There are also <a href="http://libgit2.github.com/libgit2/#HEAD/group/repository">methods</a> for specifying the index file
and work tree locations, here we assume they are in the normal places.</p>
<p>(Try running this program against tests/resources/testrepo.git.)</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kt">int</span> <span class="n">error</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">repo_path</span> <span class="o">=</span> <span class="p">(</span><span class="n">argc</span> <span class="o">></span> <span class="mi">1</span><span class="p">)</span> <span class="o">?</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">:</span> <span class="s">"/opt/libgit2-test/.git"</span><span class="p">;</span>
<span class="n">git_repository</span> <span class="o">*</span><span class="n">repo</span><span class="p">;</span>
<span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_repository_open-58" class="fnlink" href="../../#v0.24.0/group/repository/git_repository_open">git_repository_open</a></span><span class="p">(</span><span class="o">&</span><span class="n">repo</span><span class="p">,</span> <span class="n">repo_path</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"opening repository"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-SHA-1_Value_Conversions'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-SHA-1_Value_Conversions">¶</a>
</div>
<h3>SHA-1 Value Conversions</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-10'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>For our first example, we will convert a 40 character hex value to the
20 byte raw SHA1 value.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"*Hex to Raw*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="kt">char</span> <span class="n">hex</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"4a202b346bb0fb0db7eff3cffeb3c70babbd2045"</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-11'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>The <code>git_oid</code> is the structure that keeps the SHA value. We will use
this throughout the example for storing the value of the current SHA
key we're working with.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">git_oid</span> <span class="n">oid</span><span class="p">;</span>
<span class="n"><a name="git_oid_fromstr-40" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="n">hex</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-12'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Once we've converted the string into the oid value, we can get the raw
value of the SHA by accessing <code>oid.id</code></p>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-13'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Next we will convert the 20 byte raw SHA1 value to a human readable 40
char hex value.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw to Hex*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="kt">char</span> <span class="n">out</span><span class="p">[</span><span class="n">GIT_OID_HEXSZ</span><span class="o">+</span><span class="mi">1</span><span class="p">];</span>
<span class="n">out</span><span class="p">[</span><span class="n">GIT_OID_HEXSZ</span><span class="p">]</span> <span class="o">=</span> <span class="sc">'\0'</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-14'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>If you have a oid, you can easily get the hex value of the SHA as well.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-48" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"SHA hex string: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Working_with_the_Object_Database'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Working_with_the_Object_Database">¶</a>
</div>
<h3>Working with the Object Database</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-16'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p><strong>libgit2</strong> provides <a href="http://libgit2.github.com/libgit2/#HEAD/group/odb">direct access</a> to the object database. The
object database is where the actual objects are stored in Git. For
working with raw objects, we'll need to get this structure from the
repository.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">git_odb</span> <span class="o">*</span><span class="n">odb</span><span class="p">;</span>
<span class="n"><a name="git_repository_odb-60" class="fnlink" href="../../#v0.24.0/group/repository/git_repository_odb">git_repository_odb</a></span><span class="p">(</span><span class="o">&</span><span class="n">odb</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Raw_Object_Reading'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Raw_Object_Reading">¶</a>
</div>
<h4>Raw Object Reading</h4>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw Object Read*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_odb_object</span> <span class="o">*</span><span class="n">obj</span><span class="p">;</span>
<span class="n">git_otype</span> <span class="n">otype</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="n">data</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">str_type</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-18'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>We can read raw objects directly from the object database if we have
the oid (SHA) of the object. This allows us to access objects without
knowing their type and inspect the raw bytes unparsed.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_odb_read-34" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_read">git_odb_read</a></span><span class="p">(</span><span class="o">&</span><span class="n">obj</span><span class="p">,</span> <span class="n">odb</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"finding object in repository"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-19'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>A raw object only has three properties - the type (commit, blob, tree
or tag), the size of the raw data and the raw, unparsed data itself.
For a commit or tag, that raw data is human readable plain ASCII
text. For a blob it is just file contents, so it could be text or
binary data. For a tree it is a special binary format, so it's unlikely
to be hugely helpful as a raw object.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">data</span> <span class="o">=</span> <span class="p">(</span><span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span><span class="n"><a name="git_odb_object_data-37" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_object_data">git_odb_object_data</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
<span class="n">otype</span> <span class="o">=</span> <span class="n"><a name="git_odb_object_type-39" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_object_type">git_odb_object_type</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-20'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>We provide methods to convert from the object type which is an enum, to
a string representation of that value (and vice-versa).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">str_type</span> <span class="o">=</span> <span class="n"><a name="git_object_type2string-33" class="fnlink" href="../../#v0.24.0/group/object/git_object_type2string">git_object_type2string</a></span><span class="p">(</span><span class="n">otype</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"object length and type: %d, %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span>
<span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n"><a name="git_odb_object_size-38" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_object_size">git_odb_object_size</a></span><span class="p">(</span><span class="n">obj</span><span class="p">),</span>
<span class="n">str_type</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-21'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>For proper memory management, close the object when you are done with
it or it will leak memory.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_odb_object_free-36" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_object_free">git_odb_object_free</a></span><span class="p">(</span><span class="n">obj</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Raw_Object_Writing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Raw_Object_Writing">¶</a>
</div>
<h4>Raw Object Writing</h4>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Raw Object Write*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-23'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>You can also write raw object data to Git. This is pretty cool because
it gives you direct access to the key/value properties of Git. Here
we'll write a new blob object that just contains a simple string.
Notice that we have to specify the object type as the <code>git_otype</code> enum.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_odb_write-35" class="fnlink" href="../../#v0.24.0/group/odb/git_odb_write">git_odb_write</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="n">odb</span><span class="p">,</span> <span class="s">"test data"</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="s">"test data"</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">GIT_OBJ_BLOB</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-24'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Now that we've written the object, we can check out what SHA1 was
generated when the object was written to our database.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-49" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Written Object: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Object_Parsing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Object_Parsing">¶</a>
</div>
<h3>Object Parsing</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-26'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>libgit2 has methods to parse every object type in Git so you don't have
to work directly with the raw data. This is much faster and simpler
than trying to deal with the raw data yourself.</p>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-Commit_Parsing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Commit_Parsing">¶</a>
</div>
<h4>Commit Parsing</h4>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-28'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p><a href="http://libgit2.github.com/libgit2/#HEAD/group/commit">Parsing commit objects</a> is simple and gives you access to all the
data in the commit - the author (name, email, datetime), committer
(same), tree, message, encoding and parent(s).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Commit Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_commit</span> <span class="o">*</span><span class="n">commit</span><span class="p">;</span>
<span class="n"><a name="git_oid_fromstr-41" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"8496071c1b46c854b31185ea97743be6a8774479"</span><span class="p">);</span>
<span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_commit_lookup-5" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">commit</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up commit"</span><span class="p">);</span>
<span class="k">const</span> <span class="n">git_signature</span> <span class="o">*</span><span class="n">author</span><span class="p">,</span> <span class="o">*</span><span class="n">cmtter</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">message</span><span class="p">;</span>
<span class="kt">time_t</span> <span class="n">ctime</span><span class="p">;</span>
<span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">parents</span><span class="p">,</span> <span class="n">p</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-29'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Each of the properties of the commit object are accessible via methods,
including commonly needed variations, such as <code><a name="git_commit_time-16" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_time">git_commit_time</a></code> which
returns the author time and <code><a name="git_commit_message-13" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_message">git_commit_message</a></code> which gives you the
commit message (as a NUL-terminated string).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">message</span> <span class="o">=</span> <span class="n"><a name="git_commit_message-14" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_message">git_commit_message</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span>
<span class="n">author</span> <span class="o">=</span> <span class="n"><a name="git_commit_author-19" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_author">git_commit_author</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span>
<span class="n">cmtter</span> <span class="o">=</span> <span class="n"><a name="git_commit_committer-18" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_committer">git_commit_committer</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span>
<span class="n">ctime</span> <span class="o">=</span> <span class="n"><a name="git_commit_time-17" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_time">git_commit_time</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-30'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>The author and committer methods return [git<em>signature] structures,
which give you name, email and <code>when</code>, which is a `git</em>time` structure,
giving you a timestamp and timezone offset.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"Author: %s (%s)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">author</span><span class="o">-></span><span class="n">name</span><span class="p">,</span> <span class="n">author</span><span class="o">-></span><span class="n">email</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-31'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Commits can have zero or more parents. The first (root) commit will
have no parents, most commits will have one (i.e. the commit it was
based on) and merge commits will have two or more. Commits can
technically have any number, though it's rare to have more than two.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">parents</span> <span class="o">=</span> <span class="n"><a name="git_commit_parentcount-21" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_parentcount">git_commit_parentcount</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">p</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span><span class="n">p</span> <span class="o"><</span> <span class="n">parents</span><span class="p">;</span><span class="n">p</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="n">git_commit</span> <span class="o">*</span><span class="n">parent</span><span class="p">;</span>
<span class="n"><a name="git_commit_parent-22" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_parent">git_commit_parent</a></span><span class="p">(</span><span class="o">&</span><span class="n">parent</span><span class="p">,</span> <span class="n">commit</span><span class="p">,</span> <span class="n">p</span><span class="p">);</span>
<span class="n"><a name="git_oid_fmt-50" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n"><a name="git_commit_id-12" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_id">git_commit_id</a></span><span class="p">(</span><span class="n">parent</span><span class="p">));</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Parent: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span>
<span class="n"><a name="git_commit_free-8" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">parent</span><span class="p">);</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-32'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>Don't forget to close the object to prevent memory leaks. You will have
to do this for all the objects you open and parse.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_commit_free-9" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Writing_Commits'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Writing_Commits">¶</a>
</div>
<h4>Writing Commits</h4>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-34'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>libgit2 provides a couple of methods to create commit objects easily as
well. There are four different create signatures, we'll just show one
of them here. You can read about the other ones in the <a href="http://libgit2.github.com/libgit2/#HEAD/group/commit">commit API
docs</a>.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Commit Writing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_oid</span> <span class="n">tree_id</span><span class="p">,</span> <span class="n">parent_id</span><span class="p">,</span> <span class="n">commit_id</span><span class="p">;</span>
<span class="n">git_tree</span> <span class="o">*</span><span class="n">tree</span><span class="p">;</span>
<span class="n">git_commit</span> <span class="o">*</span><span class="n">parent</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-35'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Creating signatures for an authoring identity and time is simple. You
will need to do this to specify who created a commit and when. Default
values for the name and email should be found in the <code>user.name</code> and
<code>user.email</code> configuration options. See the <code>config</code> section of this
example file to see how to access config values.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_signature_new-67" class="fnlink" href="../../#v0.24.0/group/signature/git_signature_new">git_signature_new</a></span><span class="p">((</span><span class="n">git_signature</span> <span class="o">**</span><span class="p">)</span><span class="o">&</span><span class="n">author</span><span class="p">,</span>
<span class="s">"Scott Chacon"</span><span class="p">,</span> <span class="s">"schacon@gmail.com"</span><span class="p">,</span> <span class="mi">123456789</span><span class="p">,</span> <span class="mi">60</span><span class="p">);</span>
<span class="n"><a name="git_signature_new-68" class="fnlink" href="../../#v0.24.0/group/signature/git_signature_new">git_signature_new</a></span><span class="p">((</span><span class="n">git_signature</span> <span class="o">**</span><span class="p">)</span><span class="o">&</span><span class="n">cmtter</span><span class="p">,</span>
<span class="s">"Scott A Chacon"</span><span class="p">,</span> <span class="s">"scott@github.com"</span><span class="p">,</span> <span class="mi">987654321</span><span class="p">,</span> <span class="mi">90</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-36'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>Commit objects need a tree to point to and optionally one or more
parents. Here we're creating oid objects to create the commit with,
but you can also use</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fromstr-42" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">tree_id</span><span class="p">,</span> <span class="s">"f60079018b664e4e79329a7ef9559c8d9e0378d1"</span><span class="p">);</span>
<span class="n"><a name="git_tree_lookup-75" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_lookup">git_tree_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tree</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">tree_id</span><span class="p">);</span>
<span class="n"><a name="git_oid_fromstr-43" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">parent_id</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">);</span>
<span class="n"><a name="git_commit_lookup-6" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">parent</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">parent_id</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-37'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Here we actually create the commit object with a single call with all
the values we need to create the commit. The SHA key is written to the
<code>commit_id</code> variable here.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_commit_create_v-23" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_create_v">git_commit_create_v</a></span><span class="p">(</span>
<span class="o">&</span><span class="n">commit_id</span><span class="p">,</span> <span class="cm">/* out id */</span>
<span class="n">repo</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">,</span> <span class="cm">/* do not update the HEAD */</span>
<span class="n">author</span><span class="p">,</span>
<span class="n">cmtter</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">,</span> <span class="cm">/* use default message encoding */</span>
<span class="s">"example commit"</span><span class="p">,</span>
<span class="n">tree</span><span class="p">,</span>
<span class="mi">1</span><span class="p">,</span> <span class="n">parent</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-38'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>Now we can take a look at the commit SHA we've generated.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fmt-51" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="o">&</span><span class="n">commit_id</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"New Commit: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Tag_Parsing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Tag_Parsing">¶</a>
</div>
<h4>Tag Parsing</h4>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-40'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>You can parse and create tags with the <a href="http://libgit2.github.com/libgit2/#HEAD/group/tag">tag management API</a>, which
functions very similarly to the commit lookup, parsing and creation
methods, since the objects themselves are very similar.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Tag Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_tag</span> <span class="o">*</span><span class="n">tag</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">tmessage</span><span class="p">,</span> <span class="o">*</span><span class="n">tname</span><span class="p">;</span>
<span class="n">git_otype</span> <span class="n">ttype</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-41'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>We create an oid for the tag object if we know the SHA and look it up
the same way that we would a commit (or any other object).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fromstr-44" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"b25fa35b38051e4ae45d4222e795f9df2e43f1d1"</span><span class="p">);</span>
<span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_tag_lookup-70" class="fnlink" href="../../#v0.24.0/group/tag/git_tag_lookup">git_tag_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tag</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up tag"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-42'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<p>Now that we have the tag object, we can extract the information it
generally contains: the target (usually a commit object), the type of
the target object (usually 'commit'), the name ('v1.0'), the tagger (a
git_signature - name, email, timestamp), and the tag message.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_tag_target-71" class="fnlink" href="../../#v0.24.0/group/tag/git_tag_target">git_tag_target</a></span><span class="p">((</span><span class="n">git_object</span> <span class="o">**</span><span class="p">)</span><span class="o">&</span><span class="n">commit</span><span class="p">,</span> <span class="n">tag</span><span class="p">);</span>
<span class="n">tname</span> <span class="o">=</span> <span class="n"><a name="git_tag_name-73" class="fnlink" href="../../#v0.24.0/group/tag/git_tag_name">git_tag_name</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="c1">// "test"</span>
<span class="n">ttype</span> <span class="o">=</span> <span class="n"><a name="git_tag_target_type-72" class="fnlink" href="../../#v0.24.0/group/tag/git_tag_target_type">git_tag_target_type</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="c1">// GIT_OBJ_COMMIT (otype enum)</span>
<span class="n">tmessage</span> <span class="o">=</span> <span class="n"><a name="git_tag_message-74" class="fnlink" href="../../#v0.24.0/group/tag/git_tag_message">git_tag_message</a></span><span class="p">(</span><span class="n">tag</span><span class="p">);</span> <span class="c1">// "tag message\n"</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Tag Message: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">tmessage</span><span class="p">);</span>
<span class="n"><a name="git_commit_free-10" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">commit</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Tree_Parsing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Tree_Parsing">¶</a>
</div>
<h4>Tree Parsing</h4>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-44'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-44">¶</a>
</div>
<p><a href="http://libgit2.github.com/libgit2/#HEAD/group/tree">Tree parsing</a> is a bit different than the other objects, in that
we have a subtype which is the tree entry. This is not an actual
object type in Git, but a useful structure for parsing and traversing
tree entries.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Tree Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="k">const</span> <span class="n">git_tree_entry</span> <span class="o">*</span><span class="n">entry</span><span class="p">;</span>
<span class="n">git_object</span> <span class="o">*</span><span class="n">objt</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-45'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-45">¶</a>
</div>
<p>Create the oid and lookup the tree object just like the other objects.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_oid_fromstr-45" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"2a741c18ac5ff082a7caaec6e74db3075a1906b5"</span><span class="p">);</span>
<span class="n"><a name="git_tree_lookup-76" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_lookup">git_tree_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">tree</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-46'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-46">¶</a>
</div>
<p>Getting the count of entries in the tree so you can iterate over them
if you want to.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kt">size_t</span> <span class="n">cnt</span> <span class="o">=</span> <span class="n"><a name="git_tree_entrycount-77" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entrycount">git_tree_entrycount</a></span><span class="p">(</span><span class="n">tree</span><span class="p">);</span> <span class="c1">// 3</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"tree entries: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">cnt</span><span class="p">);</span>
<span class="n">entry</span> <span class="o">=</span> <span class="n"><a name="git_tree_entry_byindex-79" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entry_byindex">git_tree_entry_byindex</a></span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Entry name: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n"><a name="git_tree_entry_name-80" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entry_name">git_tree_entry_name</a></span><span class="p">(</span><span class="n">entry</span><span class="p">));</span> <span class="c1">// "hello.c"</span></pre></div>
</td>
</tr>
<tr id='section-47'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-47">¶</a>
</div>
<p>You can also access tree entries by name if you know the name of the
entry you're looking for.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">entry</span> <span class="o">=</span> <span class="n"><a name="git_tree_entry_byname-78" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entry_byname">git_tree_entry_byname</a></span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="s">"README"</span><span class="p">);</span>
<span class="n"><a name="git_tree_entry_name-81" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entry_name">git_tree_entry_name</a></span><span class="p">(</span><span class="n">entry</span><span class="p">);</span> <span class="c1">// "hello.c"</span></pre></div>
</td>
</tr>
<tr id='section-48'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-48">¶</a>
</div>
<p>Once you have the entry object, you can access the content or subtree
(or commit, in the case of submodules) that it points to. You can also
get the mode if you want.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_tree_entry_to_object-82" class="fnlink" href="../../#v0.24.0/group/tree/git_tree_entry_to_object">git_tree_entry_to_object</a></span><span class="p">(</span><span class="o">&</span><span class="n">objt</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="n">entry</span><span class="p">);</span> <span class="c1">// blob</span></pre></div>
</td>
</tr>
<tr id='section-49'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>Remember to close the looked-up object once you are done using it</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_object_free-32" class="fnlink" href="../../#v0.24.0/group/object/git_object_free">git_object_free</a></span><span class="p">(</span><span class="n">objt</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Blob_Parsing'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Blob_Parsing">¶</a>
</div>
<h4>Blob Parsing</h4>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-51'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-51">¶</a>
</div>
<p>The last object type is the simplest and requires the least parsing
help. Blobs are just file contents and can contain anything, there is
no structure to it. The main advantage to using the <a href="http://libgit2.github.com/libgit2/#HEAD/group/blob">simple blob
api</a> is that when you're creating blobs you don't have to calculate
the size of the content. There is also a helper for reading a file
from disk and writing it to the db and getting the oid back so you
don't have to do all those steps yourself.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Blob Parsing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_blob</span> <span class="o">*</span><span class="n">blob</span><span class="p">;</span>
<span class="n"><a name="git_oid_fromstr-46" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"1385f264afb75a56a5bec74243be9b367ba4ca08"</span><span class="p">);</span>
<span class="n"><a name="git_blob_lookup-1" class="fnlink" href="../../#v0.24.0/group/blob/git_blob_lookup">git_blob_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">blob</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-52'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-52">¶</a>
</div>
<p>You can access a buffer with the raw contents of the blob directly.
Note that this buffer may not be contain ASCII data for certain blobs
(e.g. binary files): do not consider the buffer a NULL-terminated
string, and use the <code><a name="git_blob_rawsize-3" class="fnlink" href="../../#v0.24.0/group/blob/git_blob_rawsize">git_blob_rawsize</a></code> attribute to find out its exact
size in bytes</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"Blob Size: %ld</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">long</span><span class="p">)</span><span class="n"><a name="git_blob_rawsize-4" class="fnlink" href="../../#v0.24.0/group/blob/git_blob_rawsize">git_blob_rawsize</a></span><span class="p">(</span><span class="n">blob</span><span class="p">));</span> <span class="c1">// 8</span>
<span class="n"><a name="git_blob_rawcontent-2" class="fnlink" href="../../#v0.24.0/group/blob/git_blob_rawcontent">git_blob_rawcontent</a></span><span class="p">(</span><span class="n">blob</span><span class="p">);</span> <span class="c1">// "content"</span></pre></div>
</td>
</tr>
<tr id='section-Revwalking'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Revwalking">¶</a>
</div>
<h3>Revwalking</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-54'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-54">¶</a>
</div>
<p>The libgit2 <a href="http://libgit2.github.com/libgit2/#HEAD/group/revwalk">revision walking api</a> provides methods to traverse the
directed graph created by the parent pointers of the commit objects.
Since all commits point back to the commit that came directly before
them, you can walk this parentage as a graph and find all the commits
that were ancestors of (reachable from) a given starting point. This
can allow you to create <code>git log</code> type functionality.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Revwalking*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_revwalk</span> <span class="o">*</span><span class="n">walk</span><span class="p">;</span>
<span class="n">git_commit</span> <span class="o">*</span><span class="n">wcommit</span><span class="p">;</span>
<span class="n"><a name="git_oid_fromstr-47" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fromstr">git_oid_fromstr</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="s">"5b5b025afb0b4c913b4c338a42934a3863bf3644"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-55'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-55">¶</a>
</div>
<p>To use the revwalker, create a new walker, tell it how you want to sort
the output and then push one or more starting points onto the walker.
If you want to emulate the output of <code>git log</code> you would push the SHA
of the commit that HEAD points to into the walker and then start
traversing them. You can also 'hide' commits that you want to stop at
or not see any of their ancestors. So if you want to emulate <code>git log
branch1..branch2</code>, you would push the oid of <code>branch2</code> and hide the oid
of <code>branch1</code>.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_revwalk_new-62" class="fnlink" href="../../#v0.24.0/group/revwalk/git_revwalk_new">git_revwalk_new</a></span><span class="p">(</span><span class="o">&</span><span class="n">walk</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span>
<span class="n"><a name="git_revwalk_sorting-65" class="fnlink" href="../../#v0.24.0/group/revwalk/git_revwalk_sorting">git_revwalk_sorting</a></span><span class="p">(</span><span class="n">walk</span><span class="p">,</span> <span class="n">GIT_SORT_TOPOLOGICAL</span> <span class="o">|</span> <span class="n">GIT_SORT_REVERSE</span><span class="p">);</span>
<span class="n"><a name="git_revwalk_push-63" class="fnlink" href="../../#v0.24.0/group/revwalk/git_revwalk_push">git_revwalk_push</a></span><span class="p">(</span><span class="n">walk</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="k">const</span> <span class="n">git_signature</span> <span class="o">*</span><span class="n">cauth</span><span class="p">;</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">cmsg</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-56'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-56">¶</a>
</div>
<p>Now that we have the starting point pushed onto the walker, we start
asking for ancestors. It will return them in the sorting order we asked
for as commit oids. We can then lookup and parse the committed pointed
at by the returned OID; note that this operation is specially fast
since the raw contents of the commit object will be cached in memory</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">while</span> <span class="p">((</span><span class="n"><a name="git_revwalk_next-64" class="fnlink" href="../../#v0.24.0/group/revwalk/git_revwalk_next">git_revwalk_next</a></span><span class="p">(</span><span class="o">&</span><span class="n">oid</span><span class="p">,</span> <span class="n">walk</span><span class="p">))</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
<span class="n">error</span> <span class="o">=</span> <span class="n"><a name="git_commit_lookup-7" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_lookup">git_commit_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">wcommit</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="o">&</span><span class="n">oid</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n">error</span><span class="p">,</span> <span class="s">"looking up commit during revwalk"</span><span class="p">);</span>
<span class="n">cmsg</span> <span class="o">=</span> <span class="n"><a name="git_commit_message-15" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_message">git_commit_message</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span>
<span class="n">cauth</span> <span class="o">=</span> <span class="n"><a name="git_commit_author-20" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_author">git_commit_author</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%s (%s)</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">cmsg</span><span class="p">,</span> <span class="n">cauth</span><span class="o">-></span><span class="n">email</span><span class="p">);</span>
<span class="n"><a name="git_commit_free-11" class="fnlink" href="../../#v0.24.0/group/commit/git_commit_free">git_commit_free</a></span><span class="p">(</span><span class="n">wcommit</span><span class="p">);</span>
<span class="p">}</span></pre></div>
</td>
</tr>
<tr id='section-57'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-57">¶</a>
</div>
<p>Like the other objects, be sure to free the revwalker when you're done
to prevent memory leaks. Also, make sure that the repository being
walked it not deallocated while the walk is in progress, or it will
result in undefined behavior</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_revwalk_free-66" class="fnlink" href="../../#v0.24.0/group/revwalk/git_revwalk_free">git_revwalk_free</a></span><span class="p">(</span><span class="n">walk</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Index_File_Manipulation'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Index_File_Manipulation">¶</a>
</div>
<h3>Index File Manipulation</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-59'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-59">¶</a>
</div>
<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/index">index file API</a> allows you to read, traverse, update and write
the Git index file (sometimes thought of as the staging area).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Index Walking*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">git_index</span> <span class="o">*</span><span class="n">index</span><span class="p">;</span>
<span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">ecount</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-60'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-60">¶</a>
</div>
<p>You can either open the index from the standard location in an open
repository, as we're doing here, or you can open and manipulate any
index file with <code>git_index_open_bare()</code>. The index for the repository
will be located and loaded from disk.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_repository_index-61" class="fnlink" href="../../#v0.24.0/group/repository/git_repository_index">git_repository_index</a></span><span class="p">(</span><span class="o">&</span><span class="n">index</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-61'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-61">¶</a>
</div>
<p>For each entry in the index, you can get a bunch of information
including the SHA (oid), path and mode which map to the tree objects
that are written out. It also has filesystem properties to help
determine what to inspect for changes (ctime, mtime, dev, ino, uid,
gid, file<em>size and flags) All these properties are exported publicly in
the `git</em>index_entry` struct</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">ecount</span> <span class="o">=</span> <span class="n"><a name="git_index_entrycount-30" class="fnlink" href="../../#v0.24.0/group/index/git_index_entrycount">git_index_entrycount</a></span><span class="p">(</span><span class="n">index</span><span class="p">);</span>
<span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">ecount</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
<span class="k">const</span> <span class="n">git_index_entry</span> <span class="o">*</span><span class="n">e</span> <span class="o">=</span> <span class="n"><a name="git_index_get_byindex-31" class="fnlink" href="../../#v0.24.0/group/index/git_index_get_byindex">git_index_get_byindex</a></span><span class="p">(</span><span class="n">index</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"path: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">e</span><span class="o">-></span><span class="n">path</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"mtime: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">e</span><span class="o">-></span><span class="n">mtime</span><span class="p">.</span><span class="n">seconds</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"fs: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">e</span><span class="o">-></span><span class="n">file_size</span><span class="p">);</span>
<span class="p">}</span>
<span class="n"><a name="git_index_free-29" class="fnlink" href="../../#v0.24.0/group/index/git_index_free">git_index_free</a></span><span class="p">(</span><span class="n">index</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-References'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-References">¶</a>
</div>
<h3>References</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-63'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-63">¶</a>
</div>
<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/reference">reference API</a> allows you to list, resolve, create and update
references such as branches, tags and remote references (everything in
the .git/refs directory).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Reference Listing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-64'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-64">¶</a>
</div>
<p>Here we will implement something like <code>git for-each-ref</code> simply listing
out all available references and the object SHA they resolve to.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">git_strarray</span> <span class="n">ref_list</span><span class="p">;</span>
<span class="n"><a name="git_reference_list-57" class="fnlink" href="../../#v0.24.0/group/reference/git_reference_list">git_reference_list</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref_list</span><span class="p">,</span> <span class="n">repo</span><span class="p">);</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">refname</span><span class="p">;</span>
<span class="n">git_reference</span> <span class="o">*</span><span class="n">ref</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-65'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-65">¶</a>
</div>
<p>Now that we have the list of reference names, we can lookup each ref
one at a time and resolve them to the SHA, then print both values out.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">ref_list</span><span class="p">.</span><span class="n">count</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span>
<span class="n">refname</span> <span class="o">=</span> <span class="n">ref_list</span><span class="p">.</span><span class="n">strings</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="n"><a name="git_reference_lookup-53" class="fnlink" href="../../#v0.24.0/group/reference/git_reference_lookup">git_reference_lookup</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref</span><span class="p">,</span> <span class="n">repo</span><span class="p">,</span> <span class="n">refname</span><span class="p">);</span>
<span class="k">switch</span> <span class="p">(</span><span class="n"><a name="git_reference_type-56" class="fnlink" href="../../#v0.24.0/group/reference/git_reference_type">git_reference_type</a></span><span class="p">(</span><span class="n">ref</span><span class="p">))</span> <span class="p">{</span>
<span class="k">case</span> <span class="nl">GIT_REF_OID</span><span class="p">:</span>
<span class="n"><a name="git_oid_fmt-52" class="fnlink" href="../../#v0.24.0/group/oid/git_oid_fmt">git_oid_fmt</a></span><span class="p">(</span><span class="n">out</span><span class="p">,</span> <span class="n"><a name="git_reference_target-54" class="fnlink" href="../../#v0.24.0/group/reference/git_reference_target">git_reference_target</a></span><span class="p">(</span><span class="n">ref</span><span class="p">));</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%s [%s]</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">refname</span><span class="p">,</span> <span class="n">out</span><span class="p">);</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">case</span> <span class="nl">GIT_REF_SYMBOLIC</span><span class="p">:</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%s => %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">refname</span><span class="p">,</span> <span class="n"><a name="git_reference_symbolic_target-55" class="fnlink" href="../../#v0.24.0/group/reference/git_reference_symbolic_target">git_reference_symbolic_target</a></span><span class="p">(</span><span class="n">ref</span><span class="p">));</span>
<span class="k">break</span><span class="p">;</span>
<span class="k">default</span><span class="o">:</span>
<span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Unexpected reference type</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="n"><a name="git_strarray_free-69" class="fnlink" href="../../#v0.24.0/group/strarray/git_strarray_free">git_strarray_free</a></span><span class="p">(</span><span class="o">&</span><span class="n">ref_list</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-Config_Files'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-Config_Files">¶</a>
</div>
<h3>Config Files</h3>
</td>
<td class=code>
<div class='highlight'><pre></pre></div>
</td>
</tr>
<tr id='section-67'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-67">¶</a>
</div>
<p>The <a href="http://libgit2.github.com/libgit2/#HEAD/group/config">config API</a> allows you to list and updatee config values
in any of the accessible config file locations (system, global, local).</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">*Config Listing*</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">email</span><span class="p">;</span>
<span class="kt">int32_t</span> <span class="n">j</span><span class="p">;</span>
<span class="n">git_config</span> <span class="o">*</span><span class="n">cfg</span><span class="p">;</span></pre></div>
</td>
</tr>
<tr id='section-68'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-68">¶</a>
</div>
<p>Open a config object so we can read global values from it.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="kt">char</span> <span class="n">config_path</span><span class="p">[</span><span class="mi">256</span><span class="p">];</span>
<span class="n">sprintf</span><span class="p">(</span><span class="n">config_path</span><span class="p">,</span> <span class="s">"%s/config"</span><span class="p">,</span> <span class="n">repo_path</span><span class="p">);</span>
<span class="n">check_error</span><span class="p">(</span><span class="n"><a name="git_config_open_ondisk-24" class="fnlink" href="../../#v0.24.0/group/config/git_config_open_ondisk">git_config_open_ondisk</a></span><span class="p">(</span><span class="o">&</span><span class="n">cfg</span><span class="p">,</span> <span class="n">config_path</span><span class="p">),</span> <span class="s">"opening config"</span><span class="p">);</span>
<span class="n"><a name="git_config_get_int32-25" class="fnlink" href="../../#v0.24.0/group/config/git_config_get_int32">git_config_get_int32</a></span><span class="p">(</span><span class="o">&</span><span class="n">j</span><span class="p">,</span> <span class="n">cfg</span><span class="p">,</span> <span class="s">"help.autocorrect"</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Autocorrect: %d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">j</span><span class="p">);</span>
<span class="n"><a name="git_config_get_string-26" class="fnlink" href="../../#v0.24.0/group/config/git_config_get_string">git_config_get_string</a></span><span class="p">(</span><span class="o">&</span><span class="n">email</span><span class="p">,</span> <span class="n">cfg</span><span class="p">,</span> <span class="s">"user.email"</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"Email: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">email</span><span class="p">);</span></pre></div>
</td>
</tr>
<tr id='section-69'>
<td class=docs>
<div class="pilwrap">
<a class="pilcrow" href="#section-69">¶</a>
</div>
<p>Finally, when you're done with the repository, you can free it as well.</p>
</td>
<td class=code>
<div class='highlight'><pre> <span class="n"><a name="git_repository_free-59" class="fnlink" href="../../#v0.24.0/group/repository/git_repository_free">git_repository_free</a></span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span></pre></div>
</td>
</tr>
</table>
</div>
</body>
|