@@ -802,85 +802,188 @@ int error = git_tag_peel(&dereferenced_target, tag);
802
802
<h3 id =" index_loading " >Loading</h3 >
803
803
804
804
``` c
805
+ /* Each repository owns an index */
806
+ git_index *idx = NULL ;
807
+ int error = git_repository_index(&idx, repo);
808
+
809
+ /* Or you can open it by path */
810
+ error = git_index_open(&idx, " /path/to/repo/.git/index" );
805
811
```
806
812
807
813
(
808
- [ ``] ( ) ,
814
+ [ ` git_repository_index ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/repository/git_repository_index ) ,
815
+ [ ` git_index_open ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_open )
809
816
)
810
817
811
- <h3 id =" index_creating " >Creating</h3 >
818
+ <h3 id =" index_creating " >Creating (in-memory)</h3 >
819
+
820
+ In-memory indexes cannot be saved to disk, but can be useful for creating trees.
812
821
813
822
``` c
823
+ git_index *idx = NULL ;
824
+ int error = git_index_new(&idx);
814
825
```
815
826
816
827
(
817
- [ ``] ( ) ,
828
+ [ ` git_index_new ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_new )
818
829
)
819
830
820
831
<h3 id =" index_capabilities " >Capabilities</h3 >
821
832
833
+ An index has certain properties, indicated by capability flags.
834
+
822
835
``` c
836
+ unsigned int caps = git_index_caps(idx);
837
+
838
+ /* These are settable as well */
839
+ caps |= GIT_INDEXCAP_NO_SYMLINKS;
840
+ int error = git_index_set_caps(idx, caps);
823
841
```
824
842
825
843
(
826
- [ ``] ( ) ,
844
+ [ ` git_index_caps ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_caps ) ,
845
+ [ ` git_indexcap_t ` ] ( http://libgit2.github.com/libgit2/#HEAD/type/git_indexcap_t )
827
846
)
828
847
829
848
<h3 id =" index_disk " >Disk</h3 >
830
849
831
850
``` c
851
+ /* Make the in-memory index match what's on disk */
852
+ int error = git_index_read(idx, true );
853
+
854
+ /* Write the in-memory index to disk */
855
+ error = git_index_write(idx);
832
856
```
833
857
834
858
(
835
- [ ``] ( ) ,
859
+ [ ` git_index_read ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_read ) ,
860
+ [ ` git_index_write ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_write )
836
861
)
837
862
838
863
<h3 id =" index_trees " >Trees</h3 >
839
864
840
865
``` c
866
+ /* Overwrite the index contents with those of a tree */
867
+ git_tree *tree = NULL ;
868
+ int error = git_revparse_single((git_object**)&tree,
869
+ repo, " HEAD~^{tree}" );
870
+ error = git_index_read_tree(idx, tree);
871
+
872
+ /* Write the index contents to the ODB as a tree */
873
+ git_oid new_tree_id = {{0}};
874
+ error = git_index_write_tree(&new_tree_id, idx);
875
+
876
+ /* In-memory indexes can write trees to any repo * /
877
+ error = git_index_write_tree_to(&new_tree_id, idx, other_repo);
841
878
```
842
879
843
880
(
844
- [ ``] ( ) ,
881
+ [`git_revparse_single`](http://libgit2.github.com/libgit2/#HEAD/group/revparse/git_revparse_single),
882
+ [`git_index_read_tree`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_read_tree),
883
+ [`git_index_write_tree`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_write_tree),
884
+ [`git_index_write_tree_to`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_write_tree_to)
845
885
)
846
886
847
887
<h3 id="index_entries">Entries</h3>
848
888
849
889
```c
890
+ /* Access by index */
891
+ size_t count = git_index_entrycount(idx);
892
+ for (size_t i=0; i<count; i++) {
893
+ const git_index_entry *entry = git_index_get_byindex(idx, i);
894
+ /* … */
895
+ }
896
+
897
+ /* Access by path */
898
+ const git_index_entry *entry = git_index_get_bypath(
899
+ idx, /* index */
900
+ "path/to/file.rb", /* path */
901
+ 0); /* stage */
850
902
```
851
903
852
904
(
853
- [ ``] ( ) ,
905
+ [ ` git_index_entrycount ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_entrycount ) ,
906
+ [ ` git_index_get_byindex ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_get_byindex ) ,
907
+ [ ` git_index_get_bypath ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_get_bypath ) ,
908
+ [ ` git_index_entry ` ] ( http://libgit2.github.com/libgit2/#HEAD/type/git_index_entry )
854
909
)
855
910
856
911
<h3 id =" index_conflicts " >Conflicts</h3 >
857
912
858
913
``` c
914
+ if (git_index_has_conflicts(idx)) {
915
+ /* If you know the path of a conflicted file * /
916
+ const git_index_entry * ancestor = NULL,
917
+ * ours = NULL,
918
+ * theirs = NULL;
919
+ int error = git_index_conflict_get(&ancestor, &ours, &theirs
920
+ idx, "path/to/file.cs");
921
+
922
+ /* Or, iterate through all conflicts * /
923
+ git_index_conflict_iterator * iter = NULL;
924
+ error = git_index_conflict_iterator_new(&iter, idx);
925
+ while (git_index_conflict_next(&ancestor, &ours, &theirs, iter)
926
+ != GIT_ITEROVER) {
927
+ /* Mark this conflict as resolved * /
928
+ error = git_index_conflict_remove(idx, ours->path);
929
+ }
930
+ git_index_conflict_iterator_free (iter);
931
+ }
859
932
```
860
933
861
934
(
862
- [ ``] ( ) ,
935
+ [ ` git_index_has_conflicts ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_has_conflicts ) ,
936
+ [ ` git_index_conflict_get ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_conflict_get ) ,
937
+ [ ` git_index_conflict_iterator_new ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_conflict_iterator_new ) ,
938
+ [ ` git_index_conflict_next ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_conflict_next ) ,
939
+ [ ` git_index_conflict_iterator_free ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_conflict_iterator_free ) ,
940
+ [ ` git_index_conflict_remove ` ] ( http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_conflict_remove )
863
941
)
864
942
865
- <h3 id =" index_add " >Add</h3 >
943
+ <h3 id =" index_add " >Add & Remove </h3 >
866
944
867
945
``` c
868
- ```
946
+ typedef struct { /* … * / } match_data;
947
+ int match_cb(const char * path, const char * spec, void * payload)
948
+ {
949
+ match_data * d = (match_data* )payload;
950
+ /*
951
+ * return 0 to add/remove this path,
952
+ * a positive number to skip this path,
953
+ * or a negative number to abort the operation.
954
+ * /
955
+ }
869
956
870
- (
871
- [ ``] ( ) ,
872
- )
957
+ const char * paths[ ] = {"src/* ", "test/* "};
958
+ git_strarray arr = {paths, 2};
873
959
874
- <h3 id =" index_remove " >Remove</h3 >
960
+ /* Add matching files; this skips ignored files * /
961
+ match_data d = {0};
962
+ int error = git_index_add_all(idx, &arr, GIT_INDEX_ADD_DEFAULT,
963
+ match_cb, &d);
964
+ /* … or remove them * /
965
+ error = git_index_remove_all(idx, &arr, match_cb, &d);
875
966
876
- ``` c
967
+ /* Something like 'git add .' * /
968
+ error = git_index_update_all(idx, &arr, match_cb, &d);
969
+
970
+ /* Force a single file to be added (even if it is ignored) * /
971
+ error = git_index_add_bypath(idx, "path/to/file.py");
972
+ /* …or removed * /
973
+ error = git_index_remove_bypath(idx, "path/to/file.py");
877
974
```
878
975
879
976
(
880
- [ ``] ( ) ,
977
+ [`git_strarray`](http://libgit2.github.com/libgit2/#HEAD/type/git_strarray),
978
+ [`git_index_add_all`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_add_all),
979
+ [`git_index_remove_all`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_remove_all),
980
+ [`git_index_update_all`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_update_all),
981
+ [`git_index_add_bypath`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_add_bypath),
982
+ [`git_index_remove_bypath`](http://libgit2.github.com/libgit2/#HEAD/group/index/git_index_remove_bypath)
881
983
)
882
984
883
985
986
+
884
987
<h2 id="status">Status</h2>
885
988
886
989
<h3 id="status_iterating_simple">Iterating (Simple)</h3>
0 commit comments