]> git.proxmox.com Git - libgit2.git/blame - include/git2/submodule.h
Add test for diffs with submodules and bug fixes
[libgit2.git] / include / git2 / submodule.h
CommitLineData
bfc9ca59
RB
1/*
2 * Copyright (C) 2012 the libgit2 contributors
3 *
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.
6 */
7#ifndef INCLUDE_git_submodule_h__
8#define INCLUDE_git_submodule_h__
9
10#include "common.h"
11#include "types.h"
12#include "oid.h"
13
14/**
15 * @file git2/submodule.h
16 * @brief Git submodule management utilities
17 * @defgroup git_submodule Git submodule management routines
18 * @ingroup Git
19 * @{
20 */
21GIT_BEGIN_DECL
22
aa13bf05
RB
23/**
24 * Opaque structure representing a submodule.
25 *
26 * Submodule support in libgit2 builds a list of known submodules and keeps
27 * it in the repository. The list is built from the .gitmodules file, the
28 * .git/config file, the index, and the HEAD tree. Items in the working
29 * directory that look like submodules (i.e. a git repo) but are not
30 * mentioned in those places won't be tracked.
31 */
32typedef struct git_submodule git_submodule;
33
34/**
35 * Values that could be specified for the update rule of a submodule.
36 *
37 * Use the DEFAULT value if you have altered the update value via
38 * `git_submodule_set_update()` and wish to reset to the original default.
39 */
bfc9ca59 40typedef enum {
aa13bf05 41 GIT_SUBMODULE_UPDATE_DEFAULT = -1,
bfc9ca59 42 GIT_SUBMODULE_UPDATE_CHECKOUT = 0,
95dfb031 43 GIT_SUBMODULE_UPDATE_REBASE = 1,
aa13bf05
RB
44 GIT_SUBMODULE_UPDATE_MERGE = 2,
45 GIT_SUBMODULE_UPDATE_NONE = 3
bfc9ca59
RB
46} git_submodule_update_t;
47
aa13bf05
RB
48/**
49 * Values that could be specified for how closely to examine the
50 * working directory when getting submodule status.
51 *
52 * Use the DEFUALT value if you have altered the ignore value via
53 * `git_submodule_set_ignore()` and wish to reset to the original value.
54 */
bfc9ca59 55typedef enum {
aa13bf05
RB
56 GIT_SUBMODULE_IGNORE_DEFAULT = -1, /* reset to default */
57 GIT_SUBMODULE_IGNORE_NONE = 0, /* any change or untracked == dirty */
58 GIT_SUBMODULE_IGNORE_UNTRACKED = 1, /* dirty if tracked files change */
59 GIT_SUBMODULE_IGNORE_DIRTY = 2, /* only dirty if HEAD moved */
60 GIT_SUBMODULE_IGNORE_ALL = 3 /* never dirty */
bfc9ca59
RB
61} git_submodule_ignore_t;
62
aa13bf05
RB
63/**
64 * Return codes for submodule status.
65 *
5f4a61ae
RB
66 * A combination of these flags will be returned to describe the status of a
67 * submodule. Depending on the "ignore" property of the submodule, some of
68 * the flags may never be returned because they indicate changes that are
69 * supposed to be ignored.
aa13bf05
RB
70 *
71 * Submodule info is contained in 4 places: the HEAD tree, the index, config
72 * files (both .git/config and .gitmodules), and the working directory. Any
73 * or all of those places might be missing information about the submodule
5f4a61ae
RB
74 * depending on what state the repo is in. We consider all four places to
75 * build the combination of status flags.
76 *
77 * There are four values that are not really status, but give basic info
78 * about what sources of submodule data are available. These will be
79 * returned even if ignore is set to "ALL".
80 *
81 * * IN_HEAD - superproject head contains submodule
82 * * IN_INDEX - superproject index contains submodule
83 * * IN_CONFIG - superproject gitmodules has submodule
84 * * IN_WD - superproject workdir has submodule
85 *
86 * The following values will be returned so long as ignore is not "ALL".
87 *
88 * * INDEX_ADDED - in index, not in head
89 * * INDEX_DELETED - in head, not in index
90 * * INDEX_MODIFIED - index and head don't match
91 * * WD_UNINITIALIZED - workdir contains empty directory
92 * * WD_ADDED - in workdir, not index
93 * * WD_DELETED - in index, not workdir
94 * * WD_MODIFIED - index and workdir head don't match
95 *
96 * The following can only be returned if ignore is "NONE" or "UNTRACKED".
97 *
98 * * WD_INDEX_MODIFIED - submodule workdir index is dirty
99 * * WD_WD_MODIFIED - submodule workdir has modified files
100 *
101 * Lastly, the following will only be returned for ignore "NONE".
102 *
103 * * WD_UNTRACKED - wd contains untracked files
aa13bf05 104 */
5f4a61ae
RB
105typedef enum {
106 GIT_SUBMODULE_STATUS_IN_HEAD = (1u << 0),
107 GIT_SUBMODULE_STATUS_IN_INDEX = (1u << 1),
108 GIT_SUBMODULE_STATUS_IN_CONFIG = (1u << 2),
109 GIT_SUBMODULE_STATUS_IN_WD = (1u << 3),
110 GIT_SUBMODULE_STATUS_INDEX_ADDED = (1u << 4),
111 GIT_SUBMODULE_STATUS_INDEX_DELETED = (1u << 5),
112 GIT_SUBMODULE_STATUS_INDEX_MODIFIED = (1u << 6),
113 GIT_SUBMODULE_STATUS_WD_UNINITIALIZED = (1u << 7),
114 GIT_SUBMODULE_STATUS_WD_ADDED = (1u << 8),
115 GIT_SUBMODULE_STATUS_WD_DELETED = (1u << 9),
116 GIT_SUBMODULE_STATUS_WD_MODIFIED = (1u << 10),
117 GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED = (1u << 11),
118 GIT_SUBMODULE_STATUS_WD_WD_MODIFIED = (1u << 12),
119 GIT_SUBMODULE_STATUS_WD_UNTRACKED = (1u << 13),
120} git_submodule_status_t;
121
122#define GIT_SUBMODULE_STATUS_IS_UNMODIFIED(S) \
5d1308f2
RB
123 (((S) & ~(GIT_SUBMODULE_STATUS_IN_HEAD | \
124 GIT_SUBMODULE_STATUS_IN_INDEX | \
125 GIT_SUBMODULE_STATUS_IN_CONFIG | \
126 GIT_SUBMODULE_STATUS_IN_WD)) == 0)
127
128#define GIT_SUBMODULE_STATUS_IS_WD_DIRTY(S) \
129 (((S) & (GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED | \
130 GIT_SUBMODULE_STATUS_WD_WD_MODIFIED | \
131 GIT_SUBMODULE_STATUS_WD_UNTRACKED)) != 0)
aa13bf05
RB
132
133/**
134 * Lookup submodule information by name or path.
135 *
136 * Given either the submodule name or path (they are usually the same), this
137 * returns a structure describing the submodule.
138 *
139 * There are two expected error scenarios:
bfc9ca59 140 *
aa13bf05
RB
141 * - The submodule is not mentioned in the HEAD, the index, and the config,
142 * but does "exist" in the working directory (i.e. there is a subdirectory
143 * that is a valid self-contained git repo). In this case, this function
144 * returns GIT_EEXISTS to indicate the the submodule exists but not in a
145 * state where a git_submodule can be instantiated.
146 * - The submodule is not mentioned in the HEAD, index, or config and the
147 * working directory doesn't contain a value git repo at that path.
148 * There may or may not be anything else at that path, but nothing that
149 * looks like a submodule. In this case, this returns GIT_ENOTFOUND.
bfc9ca59 150 *
aa13bf05
RB
151 * The submodule object is owned by the containing repo and will be freed
152 * when the repo is freed. The caller need not free the submodule.
bfc9ca59 153 *
aa13bf05
RB
154 * @param submodule Pointer to submodule description object pointer..
155 * @param repo The repository.
156 * @param name The name of the submodule. Trailing slashes will be ignored.
157 * @return 0 on success, GIT_ENOTFOUND if submodule does not exist,
158 * GIT_EEXISTS if submodule exists in working directory only, -1 on
159 * other errors.
bfc9ca59 160 */
aa13bf05
RB
161GIT_EXTERN(int) git_submodule_lookup(
162 git_submodule **submodule,
163 git_repository *repo,
164 const char *name);
bfc9ca59
RB
165
166/**
aa13bf05
RB
167 * Iterate over all tracked submodules of a repository.
168 *
169 * See the note on `git_submodule` above. This iterates over the tracked
170 * submodules as decribed therein.
171 *
172 * If you are concerned about items in the working directory that look like
173 * submodules but are not tracked, the diff API will generate a diff record
174 * for workdir items that look like submodules but are not tracked, showing
175 * them as added in the workdir. Also, the status API will treat the entire
176 * subdirectory of a contained git repo as a single GIT_STATUS_WT_NEW item.
bfc9ca59
RB
177 *
178 * @param repo The repository
179 * @param callback Function to be called with the name of each submodule.
180 * Return a non-zero value to terminate the iteration.
181 * @param payload Extra data to pass to callback
182 * @return 0 on success, -1 on error, or non-zero return value of callback
183 */
184GIT_EXTERN(int) git_submodule_foreach(
185 git_repository *repo,
aa13bf05 186 int (*callback)(git_submodule *sm, const char *name, void *payload),
bfc9ca59
RB
187 void *payload);
188
bfc9ca59 189/**
aa13bf05 190 * Set up a new git submodule for checkout.
bfc9ca59 191 *
aa13bf05
RB
192 * This does "git submodule add" up to the fetch and checkout of the
193 * submodule contents. It preps a new submodule, creates an entry in
194 * .gitmodules and creates an empty initialized repository either at the
195 * given path in the working directory or in .git/modules with a gitlink
196 * from the working directory to the new repo.
bfc9ca59 197 *
aa13bf05
RB
198 * To fully emulate "git submodule add" call this function, then open the
199 * submodule repo and perform the clone step as needed. Lastly, call
5f4a61ae 200 * `git_submodule_add_finalize()` to wrap up adding the new submodule and
aa13bf05
RB
201 * .gitmodules to the index to be ready to commit.
202 *
203 * @param submodule The newly created submodule ready to open for clone
204 * @param repo Superproject repository to contain the new submodule
205 * @param url URL for the submodules remote
206 * @param path Path at which the submodule should be created
207 * @param use_gitlink Should workdir contain a gitlink to the repo in
208 * .git/modules vs. repo directly in workdir.
209 * @return 0 on success, GIT_EEXISTS if submodule already exists,
210 * -1 on other errors.
bfc9ca59 211 */
aa13bf05 212GIT_EXTERN(int) git_submodule_add_setup(
bfc9ca59
RB
213 git_submodule **submodule,
214 git_repository *repo,
aa13bf05
RB
215 const char *url,
216 const char *path,
217 int use_gitlink);
218
219/**
220 * Resolve the setup of a new git submodule.
221 *
222 * This should be called on a submodule once you have called add setup
223 * and done the clone of the submodule. This adds the .gitmodules file
224 * and the newly cloned submodule to the index to be ready to be committed
225 * (but doesn't actually do the commit).
5f4a61ae
RB
226 *
227 * @param submodule The submodule to finish adding.
aa13bf05
RB
228 */
229GIT_EXTERN(int) git_submodule_add_finalize(git_submodule *submodule);
230
231/**
232 * Add current submodule HEAD commit to index of superproject.
5f4a61ae
RB
233 *
234 * @param submodule The submodule to add to the index
235 * @param write_index Boolean if this should immediately write the index
236 * file. If you pass this as false, you will have to get the
237 * git_index and explicitly call `git_index_write()` on it to
238 * save the change.
239 * @return 0 on success, <0 on failure
aa13bf05 240 */
5f4a61ae
RB
241GIT_EXTERN(int) git_submodule_add_to_index(
242 git_submodule *submodule,
243 int write_index);
aa13bf05
RB
244
245/**
246 * Write submodule settings to .gitmodules file.
247 *
248 * This commits any in-memory changes to the submodule to the gitmodules
5f4a61ae 249 * file on disk. You may also be interested in `git_submodule_init()` which
aa13bf05 250 * writes submodule info to ".git/config" (which is better for local changes
5f4a61ae
RB
251 * to submodule settings) and/or `git_submodule_sync()` which writes
252 * settings about remotes to the actual submodule repository.
aa13bf05
RB
253 *
254 * @param submodule The submodule to write.
255 * @return 0 on success, <0 on failure.
256 */
257GIT_EXTERN(int) git_submodule_save(git_submodule *submodule);
258
259/**
260 * Get the containing repository for a submodule.
261 *
262 * This returns a pointer to the repository that contains the submodule.
263 * This is a just a reference to the repository that was passed to the
5f4a61ae 264 * original `git_submodule_lookup()` call, so if that repository has been
aa13bf05
RB
265 * freed, then this may be a dangling reference.
266 *
267 * @param submodule Pointer to submodule object
268 * @return Pointer to `git_repository`
269 */
270GIT_EXTERN(git_repository *) git_submodule_owner(git_submodule *submodule);
271
272/**
273 * Get the name of submodule.
274 *
275 * @param submodule Pointer to submodule object
276 * @return Pointer to the submodule name
277 */
278GIT_EXTERN(const char *) git_submodule_name(git_submodule *submodule);
279
280/**
281 * Get the path to the submodule.
282 *
283 * The path is almost always the same as the submodule name, but the
284 * two are actually not required to match.
285 *
286 * @param submodule Pointer to submodule object
287 * @return Pointer to the submodule path
288 */
289GIT_EXTERN(const char *) git_submodule_path(git_submodule *submodule);
290
291/**
292 * Get the URL for the submodule.
293 *
294 * @param submodule Pointer to submodule object
295 * @return Pointer to the submodule url
296 */
297GIT_EXTERN(const char *) git_submodule_url(git_submodule *submodule);
298
299/**
300 * Set the URL for the submodule.
301 *
302 * This sets the URL in memory for the submodule. This will be used for
303 * any following submodule actions while this submodule data is in memory.
304 *
5f4a61ae
RB
305 * After calling this, you may wish to call `git_submodule_save()` to write
306 * the changes back to the ".gitmodules" file and `git_submodule_sync()` to
aa13bf05
RB
307 * write the changes to the checked out submodule repository.
308 *
309 * @param submodule Pointer to the submodule object
310 * @param url URL that should be used for the submodule
311 * @return 0 on success, <0 on failure
312 */
313GIT_EXTERN(int) git_submodule_set_url(git_submodule *submodule, const char *url);
314
315/**
316 * Get the OID for the submodule in the index.
317 *
318 * @param submodule Pointer to submodule object
319 * @return Pointer to git_oid or NULL if submodule is not in index.
320 */
321GIT_EXTERN(const git_oid *) git_submodule_index_oid(git_submodule *submodule);
322
323/**
324 * Get the OID for the submodule in the current HEAD tree.
325 *
326 * @param submodule Pointer to submodule object
327 * @return Pointer to git_oid or NULL if submodule is not in the HEAD.
328 */
329GIT_EXTERN(const git_oid *) git_submodule_head_oid(git_submodule *submodule);
330
331/**
332 * Get the OID for the submodule in the current working directory.
333 *
334 * This returns the OID that corresponds to looking up 'HEAD' in the checked
335 * out submodule. If there are pending changes in the index or anything
5f4a61ae
RB
336 * else, this won't notice that. You should call `git_submodule_status()`
337 * for a more complete picture about the state of the working directory.
aa13bf05
RB
338 *
339 * @param submodule Pointer to submodule object
340 * @return Pointer to git_oid or NULL if submodule is not checked out.
341 */
342GIT_EXTERN(const git_oid *) git_submodule_wd_oid(git_submodule *submodule);
343
344/**
345 * Get the ignore rule for the submodule.
346 *
347 * There are four ignore values:
348 *
349 * - **GIT_SUBMODULE_IGNORE_NONE** will consider any change to the contents
350 * of the submodule from a clean checkout to be dirty, including the
351 * addition of untracked files. This is the default if unspecified.
352 * - **GIT_SUBMODULE_IGNORE_UNTRACKED** examines the contents of the
5f4a61ae 353 * working tree (i.e. call `git_status_foreach()` on the submodule) but
aa13bf05
RB
354 * UNTRACKED files will not count as making the submodule dirty.
355 * - **GIT_SUBMODULE_IGNORE_DIRTY** means to only check if the HEAD of the
356 * submodule has moved for status. This is fast since it does not need to
357 * scan the working tree of the submodule at all.
358 * - **GIT_SUBMODULE_IGNORE_ALL** means not to open the submodule repo.
359 * The working directory will be consider clean so long as there is a
360 * checked out version present.
361 */
362GIT_EXTERN(git_submodule_ignore_t) git_submodule_ignore(
363 git_submodule *submodule);
364
365/**
366 * Set the ignore rule for the submodule.
367 *
368 * This sets the ignore rule in memory for the submodule. This will be used
5f4a61ae
RB
369 * for any following actions (such as `git_submodule_status()`) while the
370 * submodule is in memory. You should call `git_submodule_save()` if you
371 * want to persist the new ignore role.
aa13bf05
RB
372 *
373 * Calling this again with GIT_SUBMODULE_IGNORE_DEFAULT or calling
5f4a61ae 374 * `git_submodule_reload()` will revert the rule to the value that was in the
aa13bf05
RB
375 * original config.
376 *
377 * @return old value for ignore
378 */
379GIT_EXTERN(git_submodule_ignore_t) git_submodule_set_ignore(
380 git_submodule *submodule,
381 git_submodule_ignore_t ignore);
382
383/**
384 * Get the update rule for the submodule.
385 */
386GIT_EXTERN(git_submodule_update_t) git_submodule_update(
387 git_submodule *submodule);
388
389/**
390 * Set the update rule for the submodule.
391 *
392 * This sets the update rule in memory for the submodule. You should call
5f4a61ae 393 * `git_submodule_save()` if you want to persist the new update rule.
aa13bf05
RB
394 *
395 * Calling this again with GIT_SUBMODULE_UPDATE_DEFAULT or calling
5f4a61ae 396 * `git_submodule_reload()` will revert the rule to the value that was in the
aa13bf05
RB
397 * original config.
398 *
399 * @return old value for update
400 */
401GIT_EXTERN(git_submodule_update_t) git_submodule_set_update(
402 git_submodule *submodule,
403 git_submodule_update_t update);
404
17b06f4d
RB
405/**
406 * Read the fetchRecurseSubmodules rule for a submodule.
407 *
408 * This accesses the submodule.<name>.fetchRecurseSubmodules value for
409 * the submodule that controls fetching behavior for the submodule.
410 *
411 * Note that at this time, libgit2 does not honor this setting and the
412 * fetch functionality current ignores submodules.
413 *
414 * @return 0 if fetchRecurseSubmodules is false, 1 if true
415 */
416GIT_EXTERN(int) git_submodule_fetch_recurse_submodules(
417 git_submodule *submodule);
418
419/**
420 * Set the fetchRecurseSubmodules rule for a submodule.
421 *
422 * This sets the submodule.<name>.fetchRecurseSubmodules value for
423 * the submodule. You should call `git_submodule_save()` if you want
424 * to persist the new value.
425 *
426 * @param submodule The submodule to modify
427 * @param fetch_recurse_submodules Boolean value
428 * @return old value for fetchRecurseSubmodules
429 */
430GIT_EXTERN(int) git_submodule_set_fetch_recurse_submodules(
431 git_submodule *submodule,
432 int fetch_recurse_submodules);
433
aa13bf05
RB
434/**
435 * Copy submodule info into ".git/config" file.
436 *
437 * Just like "git submodule init", this copies information about the
438 * submodule into ".git/config". You can use the accessor functions
439 * above to alter the in-memory git_submodule object and control what
440 * is written to the config, overriding what is in .gitmodules.
441 *
442 * @param submodule The submodule to write into the superproject config
443 * @param overwrite By default, existing entries will not be overwritten,
444 * but setting this to true forces them to be updated.
445 * @return 0 on success, <0 on failure.
446 */
447GIT_EXTERN(int) git_submodule_init(git_submodule *submodule, int overwrite);
448
449/**
450 * Copy submodule remote info into submodule repo.
451 *
452 * This copies the information about the submodules URL into the checked out
453 * submodule config, acting like "git submodule sync". This is useful if
454 * you have altered the URL for the submodule (or it has been altered by a
455 * fetch of upstream changes) and you need to update your local repo.
456 */
457GIT_EXTERN(int) git_submodule_sync(git_submodule *submodule);
458
459/**
460 * Open the repository for a submodule.
461 *
462 * This is a newly opened repository object. The caller is responsible for
5f4a61ae 463 * calling `git_repository_free()` on it when done. Multiple calls to this
aa13bf05
RB
464 * function will return distinct `git_repository` objects. This will only
465 * work if the submodule is checked out into the working directory.
466 *
467 * @param subrepo Pointer to the submodule repo which was opened
468 * @param submodule Submodule to be opened
469 * @return 0 on success, <0 if submodule repo could not be opened.
470 */
471GIT_EXTERN(int) git_submodule_open(
472 git_repository **repo,
473 git_submodule *submodule);
474
475/**
476 * Reread submodule info from config, index, and HEAD.
477 *
478 * Call this to reread cached submodule information for this submodule if
479 * you have reason to believe that it has changed.
480 */
481GIT_EXTERN(int) git_submodule_reload(git_submodule *submodule);
482
483/**
484 * Reread all submodule info.
485 *
486 * Call this to reload all cached submodule information for the repo.
487 */
488GIT_EXTERN(int) git_submodule_reload_all(git_repository *repo);
489
490/**
491 * Get the status for a submodule.
492 *
493 * This looks at a submodule and tries to determine the status. It
494 * will return a combination of the `GIT_SUBMODULE_STATUS` values above.
495 * How deeply it examines the working directory to do this will depend
5f4a61ae
RB
496 * on the `git_submodule_ignore_t` value for the submodule - which can be
497 * set either temporarily or permanently with `git_submodule_set_ignore()`.
aa13bf05 498 *
5f4a61ae 499 * @param status Combination of `GIT_SUBMODULE_STATUS` flags
aa13bf05
RB
500 * @param submodule Submodule for which to get status
501 * @return 0 on success, <0 on error
502 */
503GIT_EXTERN(int) git_submodule_status(
504 unsigned int *status,
505 git_submodule *submodule);
bfc9ca59
RB
506
507/** @} */
508GIT_END_DECL
509#endif