]> git.proxmox.com Git - libgit2.git/blame - include/git2/index.h
Vector improvements and their fallout
[libgit2.git] / include / git2 / index.h
CommitLineData
f5918330 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
f5918330 3 *
bb742ede
VM
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
f5918330 6 */
68535125
VM
7#ifndef INCLUDE_git_index_h__
8#define INCLUDE_git_index_h__
9
10#include "common.h"
f1587b97 11#include "indexer.h"
d12299fe 12#include "types.h"
fc70832a 13#include "oid.h"
68535125
VM
14
15/**
f5918330 16 * @file git2/index.h
68535125
VM
17 * @brief Git index parsing and manipulation routines
18 * @defgroup git_index Git index parsing and manipulation routines
19 * @ingroup Git
20 * @{
21 */
22GIT_BEGIN_DECL
23
f45d51ff 24#define GIT_IDXENTRY_NAMEMASK (0x0fff)
8c1f9e4d 25#define GIT_IDXENTRY_STAGEMASK (0x3000)
f45d51ff
BS
26#define GIT_IDXENTRY_EXTENDED (0x4000)
27#define GIT_IDXENTRY_VALID (0x8000)
8c1f9e4d
VM
28#define GIT_IDXENTRY_STAGESHIFT 12
29
26f2c897
JP
30/*
31 * Flags are divided into two parts: in-memory flags and
32 * on-disk ones. Flags in GIT_IDXENTRY_EXTENDED_FLAGS
33 * will get saved on-disk.
34 *
35 * In-memory only flags:
36 */
f45d51ff
BS
37#define GIT_IDXENTRY_UPDATE (1 << 0)
38#define GIT_IDXENTRY_REMOVE (1 << 1)
39#define GIT_IDXENTRY_UPTODATE (1 << 2)
40#define GIT_IDXENTRY_ADDED (1 << 3)
26f2c897 41
f45d51ff
BS
42#define GIT_IDXENTRY_HASHED (1 << 4)
43#define GIT_IDXENTRY_UNHASHED (1 << 5)
44#define GIT_IDXENTRY_WT_REMOVE (1 << 6) /* remove in work directory */
45#define GIT_IDXENTRY_CONFLICTED (1 << 7)
26f2c897 46
f45d51ff 47#define GIT_IDXENTRY_UNPACKED (1 << 8)
1648fbd3 48#define GIT_IDXENTRY_NEW_SKIP_WORKTREE (1 << 9)
26f2c897
JP
49
50/*
51 * Extended on-disk flags:
52 */
f45d51ff
BS
53#define GIT_IDXENTRY_INTENT_TO_ADD (1 << 13)
54#define GIT_IDXENTRY_SKIP_WORKTREE (1 << 14)
26f2c897 55/* GIT_IDXENTRY_EXTENDED2 is for future extension */
f45d51ff 56#define GIT_IDXENTRY_EXTENDED2 (1 << 15)
26f2c897
JP
57
58#define GIT_IDXENTRY_EXTENDED_FLAGS (GIT_IDXENTRY_INTENT_TO_ADD | GIT_IDXENTRY_SKIP_WORKTREE)
59
6fd195d7
VM
60/** Time used in a git index entry */
61typedef struct {
f0bde7fa
AB
62 git_time_t seconds;
63 /* nsec should not be stored as time_t compatible */
073fa812 64 unsigned int nanoseconds;
6fd195d7
VM
65} git_index_time;
66
68535125 67/** Memory representation of a file entry in the index. */
6fd195d7
VM
68typedef struct git_index_entry {
69 git_index_time ctime;
70 git_index_time mtime;
71
322086f9
VM
72 unsigned int dev;
73 unsigned int ino;
74 unsigned int mode;
75 unsigned int uid;
76 unsigned int gid;
f0bde7fa 77 git_off_t file_size;
6fd195d7
VM
78
79 git_oid oid;
80
322086f9
VM
81 unsigned short flags;
82 unsigned short flags_extended;
6fd195d7 83
7d9cc9f8 84 char *path;
6fd195d7 85} git_index_entry;
68535125 86
f45ec1a0
ET
87/** Representation of a resolve undo entry in the index. */
88typedef struct git_index_reuc_entry {
4c0b6a6d
JP
89 unsigned int mode[3];
90 git_oid oid[3];
7d9cc9f8 91 char *path;
f45ec1a0 92} git_index_reuc_entry;
68535125 93
da825c92
RB
94/** Capabilities of system that affect index actions. */
95enum {
96 GIT_INDEXCAP_IGNORE_CASE = 1,
97 GIT_INDEXCAP_NO_FILEMODE = 2,
98 GIT_INDEXCAP_NO_SYMLINKS = 4,
77d65af4 99 GIT_INDEXCAP_FROM_OWNER = ~0u
da825c92
RB
100};
101
f45ec1a0
ET
102/** @name Index File Functions
103 *
104 * These functions work on the index file itself.
105 */
106/**@{*/
107
68535125 108/**
f7e59c4d 109 * Create a new bare Git index object as a memory representation
c3a20d5c
VM
110 * of the Git index file in 'index_path', without a repository
111 * to back it.
68535125 112 *
f7e59c4d
VM
113 * Since there is no ODB or working directory behind this index,
114 * any Index methods which rely on these (e.g. index_add) will
115 * fail with the GIT_EBAREINDEX error code.
6fd195d7 116 *
f7e59c4d
VM
117 * If you need to access the index of an actual repository,
118 * use the `git_repository_index` wrapper.
119 *
120 * The index must be freed once it's no longer in use.
c3a20d5c 121 *
f45d51ff 122 * @param out the pointer for the new index
f7e59c4d 123 * @param index_path the path to the index file in disk
e172cf08 124 * @return 0 or an error code
c3a20d5c 125 */
f45d51ff 126GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path);
68535125 127
1e808f9c
VM
128/**
129 * Create an in-memory index object.
130 *
131 * This index object cannot be read/written to the filesystem,
132 * but may be used to perform in-memory index operations.
133 *
134 * The index must be freed once it's no longer in use.
135 *
f45d51ff 136 * @param out the pointer for the new index
1e808f9c
VM
137 * @return 0 or an error code
138 */
f45d51ff 139GIT_EXTERN(int) git_index_new(git_index **out);
1e808f9c 140
68535125 141/**
f45ec1a0 142 * Free an existing index object.
68535125
VM
143 *
144 * @param index an existing index object
145 */
f45ec1a0 146GIT_EXTERN(void) git_index_free(git_index *index);
68535125
VM
147
148/**
f45ec1a0 149 * Get the repository this index relates to
68535125 150 *
f45ec1a0
ET
151 * @param index The index
152 * @return A pointer to the repository
68535125 153 */
f45ec1a0 154GIT_EXTERN(git_repository *) git_index_owner(const git_index *index);
68535125 155
da825c92
RB
156/**
157 * Read index capabilities flags.
158 *
159 * @param index An existing index object
160 * @return A combination of GIT_INDEXCAP values
161 */
162GIT_EXTERN(unsigned int) git_index_caps(const git_index *index);
163
164/**
165 * Set index capabilities flags.
166 *
167 * If you pass `GIT_INDEXCAP_FROM_OWNER` for the caps, then the
168 * capabilities will be read from the config of the owner object,
169 * looking at `core.ignorecase`, `core.filemode`, `core.symlinks`.
170 *
171 * @param index An existing index object
172 * @param caps A combination of GIT_INDEXCAP values
173 * @return 0 on success, -1 on failure
174 */
175GIT_EXTERN(int) git_index_set_caps(git_index *index, unsigned int caps);
176
68535125
VM
177/**
178 * Update the contents of an existing index object in memory
179 * by reading from the hard disk.
180 *
181 * @param index an existing index object
e172cf08 182 * @return 0 or an error code
68535125
VM
183 */
184GIT_EXTERN(int) git_index_read(git_index *index);
185
186/**
187 * Write an existing index object from memory back to disk
188 * using an atomic file lock.
189 *
190 * @param index an existing index object
e172cf08 191 * @return 0 or an error code
68535125
VM
192 */
193GIT_EXTERN(int) git_index_write(git_index *index);
194
195/**
f45ec1a0
ET
196 * Read a tree into the index file with stats
197 *
198 * The current index contents will be replaced by the specified tree.
68535125
VM
199 *
200 * @param index an existing index object
f45ec1a0
ET
201 * @param tree tree to read
202 * @return 0 or an error code
68535125 203 */
f45d51ff 204GIT_EXTERN(int) git_index_read_tree(git_index *index, const git_tree *tree);
f45ec1a0 205
276ea401
VM
206/**
207 * Write the index as a tree
208 *
209 * This method will scan the index and write a representation
210 * of its current state back to disk; it recursively creates
211 * tree objects for each of the subtrees stored in the index,
212 * but only returns the OID of the root tree. This is the OID
213 * that can be used e.g. to create a commit.
214 *
215 * The index instance cannot be bare, and needs to be associated
216 * to an existing repository.
217 *
f92bcaea 218 * The index must not contain any file in conflict.
219 *
f45d51ff 220 * @param out Pointer where to store the OID of the written tree
276ea401 221 * @param index Index to write
f92bcaea 222 * @return 0 on success, GIT_EUNMERGED when the index is not clean
223 * or an error code
276ea401 224 */
f45d51ff 225GIT_EXTERN(int) git_index_write_tree(git_oid *out, git_index *index);
276ea401
VM
226
227/**
228 * Write the index as a tree to the given repository
229 *
230 * This method will do the same as `git_index_write_tree`, but
231 * letting the user choose the repository where the tree will
232 * be written.
233 *
f92bcaea 234 * The index must not contain any file in conflict.
235 *
f45d51ff 236 * @param out Pointer where to store OID of the the written tree
276ea401
VM
237 * @param index Index to write
238 * @param repo Repository where to write the tree
f92bcaea 239 * @return 0 on success, GIT_EUNMERGED when the index is not clean
240 * or an error code
276ea401 241 */
f45d51ff 242GIT_EXTERN(int) git_index_write_tree_to(git_oid *out, git_index *index, git_repository *repo);
276ea401 243
f45ec1a0
ET
244/**@}*/
245
246/** @name Raw Index Entry Functions
247 *
248 * These functions work on index entries, and allow for raw manipulation
249 * of the entries.
250 */
251/**@{*/
252
253/* Index entry manipulation */
68535125 254
245adf4f 255/**
f45ec1a0 256 * Get the count of entries currently in the index
245adf4f
KS
257 *
258 * @param index an existing index object
f45ec1a0 259 * @return integer of count of current entries
245adf4f 260 */
a8122b5d 261GIT_EXTERN(size_t) git_index_entrycount(const git_index *index);
245adf4f 262
68535125 263/**
f45ec1a0
ET
264 * Clear the contents (all the entries) of an index object.
265 * This clears the index object in memory; changes must be manually
266 * written to disk for them to take effect.
f7a5058a 267 *
f45ec1a0
ET
268 * @param index an existing index object
269 */
270GIT_EXTERN(void) git_index_clear(git_index *index);
271
272/**
273 * Get a pointer to one of the entries in the index
f7a5058a 274 *
f45ec1a0
ET
275 * The values of this entry can be modified (except the path)
276 * and the changes will be written back to disk on the next
277 * write() call.
68535125 278 *
f45ec1a0 279 * The entry should not be freed by the caller.
cfbc880d 280 *
68535125 281 * @param index an existing index object
f45ec1a0
ET
282 * @param n the position of the entry
283 * @return a pointer to the entry; NULL if out of bounds
284 */
16248ee2
RB
285GIT_EXTERN(const git_index_entry *) git_index_get_byindex(
286 git_index *index, size_t n);
f45ec1a0
ET
287
288/**
289 * Get a pointer to one of the entries in the index
290 *
291 * The values of this entry can be modified (except the path)
292 * and the changes will be written back to disk on the next
293 * write() call.
294 *
295 * The entry should not be freed by the caller.
296 *
297 * @param index an existing index object
298 * @param path path to search
299 * @param stage stage to search
300 * @return a pointer to the entry; NULL if it was not found
301 */
16248ee2
RB
302GIT_EXTERN(const git_index_entry *) git_index_get_bypath(
303 git_index *index, const char *path, int stage);
f45ec1a0
ET
304
305/**
306 * Remove an entry from the index
307 *
308 * @param index an existing index object
309 * @param path path to search
310 * @param stage stage to search
e172cf08 311 * @return 0 or an error code
68535125 312 */
f45ec1a0 313GIT_EXTERN(int) git_index_remove(git_index *index, const char *path, int stage);
3f43678e 314
7fc00435
RB
315/**
316 * Remove all entries from the index under a given directory
317 *
318 * @param index an existing index object
319 * @param dir container directory path
320 * @param stage stage to search
321 * @return 0 or an error code
322 */
323GIT_EXTERN(int) git_index_remove_directory(
324 git_index *index, const char *dir, int stage);
325
3f43678e 326/**
f7a5058a
VM
327 * Add or update an index entry from an in-memory struct
328 *
f45ec1a0
ET
329 * If a previous index entry exists that has the same path and stage
330 * as the given 'source_entry', it will be replaced. Otherwise, the
331 * 'source_entry' will be added.
332 *
f7a5058a
VM
333 * A full copy (including the 'path' string) of the given
334 * 'source_entry' will be inserted on the index.
3f43678e
VM
335 *
336 * @param index an existing index object
f7a5058a 337 * @param source_entry new entry object
e172cf08 338 * @return 0 or an error code
3f43678e 339 */
f45ec1a0 340GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_entry);
3f43678e
VM
341
342/**
f45ec1a0
ET
343 * Return the stage number from a git index entry
344 *
345 * This entry is calculated from the entry's flag
346 * attribute like this:
347 *
348 * (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT
f7a5058a 349 *
f45ec1a0
ET
350 * @param entry The entry
351 * @returns the stage number
352 */
353GIT_EXTERN(int) git_index_entry_stage(const git_index_entry *entry);
354
355/**@}*/
356
357/** @name Workdir Index Entry Functions
358 *
359 * These functions work on index entries specifically in the working
360 * directory (ie, stage 0).
361 */
362/**@{*/
363
364/**
25743bd7 365 * Add or update an index entry from a file on disk
f7a5058a
VM
366 *
367 * The file `path` must be relative to the repository's
368 * working folder and must be readable.
369 *
370 * This method will fail in bare index instances.
371 *
f45ec1a0
ET
372 * This forces the file to be added to the index, not looking
373 * at gitignore rules. Those rules can be evaluated through
374 * the git_status APIs (in status.h) before calling this.
375 *
376 * If this file currently is the result of a merge conflict, this
377 * file will no longer be marked as conflicting. The data about
378 * the conflict will be moved to the "resolve undo" (REUC) section.
379 *
f7a5058a
VM
380 * @param index an existing index object
381 * @param path filename to add
e172cf08 382 * @return 0 or an error code
f7a5058a 383 */
25743bd7
ET
384GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path);
385
386/**
387 * Remove an index entry corresponding to a file on disk
388 *
389 * The file `path` must be relative to the repository's
390 * working folder. It may exist.
391 *
392 * If this file currently is the result of a merge conflict, this
393 * file will no longer be marked as conflicting. The data about
394 * the conflict will be moved to the "resolve undo" (REUC) section.
395 *
396 * @param index an existing index object
397 * @param path filename to remove
398 * @return 0 or an error code
399 */
400GIT_EXTERN(int) git_index_remove_bypath(git_index *index, const char *path);
3f43678e
VM
401
402/**
f45ec1a0
ET
403 * Find the first index of any entries which point to given
404 * path in the Git index.
f7a5058a 405 *
11d9f6b3 406 * @param at_pos the address to which the position of the index entry is written (optional)
f45ec1a0
ET
407 * @param index an existing index object
408 * @param path path to search
11d9f6b3 409 * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND)
f45ec1a0 410 */
11d9f6b3 411GIT_EXTERN(int) git_index_find(size_t *at_pos, git_index *index, const char *path);
f45ec1a0
ET
412
413/**@}*/
414
415/** @name Conflict Index Entry Functions
f7a5058a 416 *
f45ec1a0
ET
417 * These functions work on conflict index entries specifically (ie, stages 1-3)
418 */
419/**@{*/
420
421/**
422 * Add or update index entries to represent a conflict
423 *
424 * The entries are the entries from the tree included in the merge. Any
425 * entry may be null to indicate that that file was not present in the
426 * trees during the merge. For example, ancestor_entry may be NULL to
427 * indicate that a file was added in both branches and must be resolved.
3f43678e
VM
428 *
429 * @param index an existing index object
f45ec1a0
ET
430 * @param ancestor_entry the entry data for the ancestor of the conflict
431 * @param our_entry the entry data for our side of the merge conflict
432 * @param their_entry the entry data for their side of the merge conflict
e172cf08 433 * @return 0 or an error code
3f43678e 434 */
f45d51ff
BS
435GIT_EXTERN(int) git_index_conflict_add(
436 git_index *index,
f45ec1a0
ET
437 const git_index_entry *ancestor_entry,
438 const git_index_entry *our_entry,
439 const git_index_entry *their_entry);
f7a5058a
VM
440
441/**
f45ec1a0 442 * Get the index entries that represent a conflict of a single file.
f7a5058a 443 *
f45ec1a0
ET
444 * The values of this entry can be modified (except the paths)
445 * and the changes will be written back to disk on the next
446 * write() call.
447 *
448 * @param ancestor_out Pointer to store the ancestor entry
449 * @param our_out Pointer to store the our entry
450 * @param their_out Pointer to store the their entry
f7a5058a 451 * @param index an existing index object
f45ec1a0 452 * @param path path to search
f7a5058a 453 */
f45ec1a0 454GIT_EXTERN(int) git_index_conflict_get(git_index_entry **ancestor_out, git_index_entry **our_out, git_index_entry **their_out, git_index *index, const char *path);
68535125 455
6fd195d7 456/**
f45ec1a0 457 * Removes the index entries that represent a conflict of a single file.
6fd195d7 458 *
f45ec1a0
ET
459 * @param index an existing index object
460 * @param path to search
461 */
462GIT_EXTERN(int) git_index_conflict_remove(git_index *index, const char *path);
463
464/**
465 * Remove all conflicts in the index (entries with a stage greater than 0.)
f4e2aca2 466 *
6fd195d7 467 * @param index an existing index object
6fd195d7 468 */
f45ec1a0
ET
469GIT_EXTERN(void) git_index_conflict_cleanup(git_index *index);
470
7cc1bf0f 471/**
472 * Determine if the index contains entries representing file conflicts.
473 *
474 * @return 1 if at least one conflict is found, 0 otherwise.
475 */
16248ee2 476GIT_EXTERN(int) git_index_has_conflicts(const git_index *index);
7cc1bf0f 477
f45ec1a0
ET
478/**@}*/
479
480/** @name Resolve Undo (REUC) index entry manipulation.
481 *
482 * These functions work on the Resolve Undo index extension and contains
483 * data about the original files that led to a merge conflict.
484 */
485/**@{*/
6fd195d7 486
0be42199 487/**
f45ec1a0 488 * Get the count of resolve undo entries currently in the index.
0be42199
SC
489 *
490 * @param index an existing index object
f45ec1a0 491 * @return integer of count of current resolve undo entries
0be42199 492 */
f45ec1a0 493GIT_EXTERN(unsigned int) git_index_reuc_entrycount(git_index *index);
0be42199 494
4c0b6a6d 495/**
f45ec1a0
ET
496 * Finds the resolve undo entry that points to the given path in the Git
497 * index.
4c0b6a6d 498 *
11d9f6b3 499 * @param at_pos the address to which the position of the reuc entry is written (optional)
4c0b6a6d 500 * @param index an existing index object
f45ec1a0 501 * @param path path to search
11d9f6b3 502 * @return 0 if found, < 0 otherwise (GIT_ENOTFOUND)
4c0b6a6d 503 */
11d9f6b3 504GIT_EXTERN(int) git_index_reuc_find(size_t *at_pos, git_index *index, const char *path);
4c0b6a6d
JP
505
506/**
f45ec1a0 507 * Get a resolve undo entry from the index.
4c0b6a6d 508 *
f4e2aca2 509 * The returned entry is read-only and should not be modified
f45d51ff 510 * or freed by the caller.
f4e2aca2 511 *
4c0b6a6d
JP
512 * @param index an existing index object
513 * @param path path to search
f45ec1a0 514 * @return the resolve undo entry; NULL if not found
4c0b6a6d 515 */
f45ec1a0 516GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_bypath(git_index *index, const char *path);
f11e0797
RG
517
518/**
f45ec1a0 519 * Get a resolve undo entry from the index.
f11e0797
RG
520 *
521 * The returned entry is read-only and should not be modified
f45d51ff 522 * or freed by the caller.
f11e0797
RG
523 *
524 * @param index an existing index object
525 * @param n the position of the entry
f45ec1a0 526 * @return a pointer to the resolve undo entry; NULL if out of bounds
f11e0797 527 */
f45ec1a0 528GIT_EXTERN(const git_index_reuc_entry *) git_index_reuc_get_byindex(git_index *index, size_t n);
4c0b6a6d 529
3a42e0a3 530/**
f45d51ff 531 * Adds a resolve undo entry for a file based on the given parameters.
3a42e0a3 532 *
f45ec1a0
ET
533 * The resolve undo entry contains the OIDs of files that were involved
534 * in a merge conflict after the conflict has been resolved. This allows
535 * conflicts to be re-resolved later.
3a42e0a3 536 *
f45ec1a0
ET
537 * If there exists a resolve undo entry for the given path in the index,
538 * it will be removed.
3a42e0a3 539 *
f45ec1a0 540 * This method will fail in bare index instances.
599f2849
CB
541 *
542 * @param index an existing index object
f45ec1a0
ET
543 * @param path filename to add
544 * @param ancestor_mode mode of the ancestor file
f45d51ff 545 * @param ancestor_id oid of the ancestor file
f45ec1a0 546 * @param our_mode mode of our file
f45d51ff 547 * @param our_id oid of our file
f45ec1a0 548 * @param their_mode mode of their file
f45d51ff 549 * @param their_id oid of their file
e172cf08 550 * @return 0 or an error code
599f2849 551 */
f45ec1a0 552GIT_EXTERN(int) git_index_reuc_add(git_index *index, const char *path,
f45d51ff
BS
553 int ancestor_mode, git_oid *ancestor_id,
554 int our_mode, git_oid *our_id,
555 int their_mode, git_oid *their_id);
599f2849 556
b1be9dd0 557/**
f45ec1a0 558 * Remove an resolve undo entry from the index
b1be9dd0 559 *
f45ec1a0 560 * @param index an existing index object
f45d51ff 561 * @param n position of the resolve undo entry to remove
f45ec1a0 562 * @return 0 or an error code
b1be9dd0 563 */
f45d51ff 564GIT_EXTERN(int) git_index_reuc_remove(git_index *index, size_t n);
f45ec1a0
ET
565
566/**@}*/
b1be9dd0 567
68535125
VM
568/** @} */
569GIT_END_DECL
570#endif