]>
git.proxmox.com Git - libgit2.git/blob - include/git2/index.h
2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
7 #ifndef INCLUDE_git_index_h__
8 #define INCLUDE_git_index_h__
16 * @brief Git index parsing and manipulation routines
17 * @defgroup git_index Git index parsing and manipulation routines
23 #define GIT_IDXENTRY_NAMEMASK (0x0fff)
24 #define GIT_IDXENTRY_STAGEMASK (0x3000)
25 #define GIT_IDXENTRY_EXTENDED (0x4000)
26 #define GIT_IDXENTRY_VALID (0x8000)
27 #define GIT_IDXENTRY_STAGESHIFT 12
30 * Flags are divided into two parts: in-memory flags and
31 * on-disk ones. Flags in GIT_IDXENTRY_EXTENDED_FLAGS
32 * will get saved on-disk.
34 * In-memory only flags:
36 #define GIT_IDXENTRY_UPDATE (1 << 0)
37 #define GIT_IDXENTRY_REMOVE (1 << 1)
38 #define GIT_IDXENTRY_UPTODATE (1 << 2)
39 #define GIT_IDXENTRY_ADDED (1 << 3)
41 #define GIT_IDXENTRY_HASHED (1 << 4)
42 #define GIT_IDXENTRY_UNHASHED (1 << 5)
43 #define GIT_IDXENTRY_WT_REMOVE (1 << 6) /* remove in work directory */
44 #define GIT_IDXENTRY_CONFLICTED (1 << 7)
46 #define GIT_IDXENTRY_UNPACKED (1 << 8)
47 #define GIT_IDXENTRY_NEW_SKIP_WORKTREE (1 << 9)
50 * Extended on-disk flags:
52 #define GIT_IDXENTRY_INTENT_TO_ADD (1 << 13)
53 #define GIT_IDXENTRY_SKIP_WORKTREE (1 << 14)
54 /* GIT_IDXENTRY_EXTENDED2 is for future extension */
55 #define GIT_IDXENTRY_EXTENDED2 (1 << 15)
57 #define GIT_IDXENTRY_EXTENDED_FLAGS (GIT_IDXENTRY_INTENT_TO_ADD | GIT_IDXENTRY_SKIP_WORKTREE)
59 /** Time used in a git index entry */
62 /* nsec should not be stored as time_t compatible */
63 unsigned int nanoseconds
;
66 /** Memory representation of a file entry in the index. */
67 typedef struct git_index_entry
{
81 unsigned short flags_extended
;
86 /** Representation of an unmerged file entry in the index. */
87 typedef struct git_index_entry_unmerged
{
91 } git_index_entry_unmerged
;
93 /** Capabilities of system that affect index actions. */
95 GIT_INDEXCAP_IGNORE_CASE
= 1,
96 GIT_INDEXCAP_NO_FILEMODE
= 2,
97 GIT_INDEXCAP_NO_SYMLINKS
= 4,
98 GIT_INDEXCAP_FROM_OWNER
= ~0u
102 * Create a new bare Git index object as a memory representation
103 * of the Git index file in 'index_path', without a repository
106 * Since there is no ODB or working directory behind this index,
107 * any Index methods which rely on these (e.g. index_add) will
108 * fail with the GIT_EBAREINDEX error code.
110 * If you need to access the index of an actual repository,
111 * use the `git_repository_index` wrapper.
113 * The index must be freed once it's no longer in use.
115 * @param index the pointer for the new index
116 * @param index_path the path to the index file in disk
117 * @return 0 or an error code
119 GIT_EXTERN(int) git_index_open(git_index
**index
, const char *index_path
);
122 * Clear the contents (all the entries) of an index object.
123 * This clears the index object in memory; changes must be manually
124 * written to disk for them to take effect.
126 * @param index an existing index object
128 GIT_EXTERN(void) git_index_clear(git_index
*index
);
131 * Free an existing index object.
133 * @param index an existing index object
135 GIT_EXTERN(void) git_index_free(git_index
*index
);
138 * Read index capabilities flags.
140 * @param index An existing index object
141 * @return A combination of GIT_INDEXCAP values
143 GIT_EXTERN(unsigned int) git_index_caps(const git_index
*index
);
146 * Set index capabilities flags.
148 * If you pass `GIT_INDEXCAP_FROM_OWNER` for the caps, then the
149 * capabilities will be read from the config of the owner object,
150 * looking at `core.ignorecase`, `core.filemode`, `core.symlinks`.
152 * @param index An existing index object
153 * @param caps A combination of GIT_INDEXCAP values
154 * @return 0 on success, -1 on failure
156 GIT_EXTERN(int) git_index_set_caps(git_index
*index
, unsigned int caps
);
159 * Update the contents of an existing index object in memory
160 * by reading from the hard disk.
162 * @param index an existing index object
163 * @return 0 or an error code
165 GIT_EXTERN(int) git_index_read(git_index
*index
);
168 * Write an existing index object from memory back to disk
169 * using an atomic file lock.
171 * @param index an existing index object
172 * @return 0 or an error code
174 GIT_EXTERN(int) git_index_write(git_index
*index
);
177 * Find the first index of any entries which point to given
178 * path in the Git index.
180 * @param index an existing index object
181 * @param path path to search
182 * @return an index >= 0 if found, -1 otherwise
184 GIT_EXTERN(int) git_index_find(git_index
*index
, const char *path
);
187 * Remove all entries with equal path except last added
189 * @param index an existing index object
191 GIT_EXTERN(void) git_index_uniq(git_index
*index
);
194 * Add or update an index entry from a file in disk
196 * The file `path` must be relative to the repository's
197 * working folder and must be readable.
199 * This method will fail in bare index instances.
201 * This forces the file to be added to the index, not looking
202 * at gitignore rules. Those rules can be evaluated through
203 * the git_status APIs (in status.h) before calling this.
205 * @param index an existing index object
206 * @param path filename to add
207 * @param stage stage for the entry
208 * @return 0 or an error code
210 GIT_EXTERN(int) git_index_add(git_index
*index
, const char *path
, int stage
);
213 * Add or update an index entry from an in-memory struct
215 * A full copy (including the 'path' string) of the given
216 * 'source_entry' will be inserted on the index.
218 * @param index an existing index object
219 * @param source_entry new entry object
220 * @return 0 or an error code
222 GIT_EXTERN(int) git_index_add2(git_index
*index
, const git_index_entry
*source_entry
);
225 * Add (append) an index entry from a file in disk
227 * A new entry will always be inserted into the index;
228 * if the index already contains an entry for such
229 * path, the old entry will **not** be replaced.
231 * The file `path` must be relative to the repository's
232 * working folder and must be readable.
234 * This method will fail in bare index instances.
236 * @param index an existing index object
237 * @param path filename to add
238 * @param stage stage for the entry
239 * @return 0 or an error code
241 GIT_EXTERN(int) git_index_append(git_index
*index
, const char *path
, int stage
);
244 * Add (append) an index entry from an in-memory struct
246 * A new entry will always be inserted into the index;
247 * if the index already contains an entry for the path
248 * in the `entry` struct, the old entry will **not** be
251 * A full copy (including the 'path' string) of the given
252 * 'source_entry' will be inserted on the index.
254 * @param index an existing index object
255 * @param source_entry new entry object
256 * @return 0 or an error code
258 GIT_EXTERN(int) git_index_append2(git_index
*index
, const git_index_entry
*source_entry
);
261 * Remove an entry from the index
263 * @param index an existing index object
264 * @param position position of the entry to remove
265 * @return 0 or an error code
267 GIT_EXTERN(int) git_index_remove(git_index
*index
, int position
);
271 * Get a pointer to one of the entries in the index
273 * This entry can be modified, and the changes will be written
274 * back to disk on the next write() call.
276 * The entry should not be freed by the caller.
278 * @param index an existing index object
279 * @param n the position of the entry
280 * @return a pointer to the entry; NULL if out of bounds
282 GIT_EXTERN(git_index_entry
*) git_index_get(git_index
*index
, size_t n
);
285 * Get the count of entries currently in the index
287 * @param index an existing index object
288 * @return integer of count of current entries
290 GIT_EXTERN(unsigned int) git_index_entrycount(git_index
*index
);
293 * Get the count of unmerged entries currently in the index
295 * @param index an existing index object
296 * @return integer of count of current unmerged entries
298 GIT_EXTERN(unsigned int) git_index_entrycount_unmerged(git_index
*index
);
301 * Get an unmerged entry from the index.
303 * The returned entry is read-only and should not be modified
304 * of freed by the caller.
306 * @param index an existing index object
307 * @param path path to search
308 * @return the unmerged entry; NULL if not found
310 GIT_EXTERN(const git_index_entry_unmerged
*) git_index_get_unmerged_bypath(git_index
*index
, const char *path
);
313 * Get an unmerged entry from the index.
315 * The returned entry is read-only and should not be modified
316 * of freed by the caller.
318 * @param index an existing index object
319 * @param n the position of the entry
320 * @return a pointer to the unmerged entry; NULL if out of bounds
322 GIT_EXTERN(const git_index_entry_unmerged
*) git_index_get_unmerged_byindex(git_index
*index
, size_t n
);
325 * Return the stage number from a git index entry
327 * This entry is calculated from the entry's flag
328 * attribute like this:
330 * (entry->flags & GIT_IDXENTRY_STAGEMASK) >> GIT_IDXENTRY_STAGESHIFT
332 * @param entry The entry
333 * @returns the stage number
335 GIT_EXTERN(int) git_index_entry_stage(const git_index_entry
*entry
);
338 * Read a tree into the index file
340 * The current index contents will be replaced by the specified tree.
342 * @param index an existing index object
343 * @param tree tree to read
344 * @return 0 or an error code
346 GIT_EXTERN(int) git_index_read_tree(git_index
*index
, git_tree
*tree
);