Commit f9775a37 by Russell Belfer

Add ignore_submodules to diff options

This adds correct support for an equivalent to --ignore-submodules
in diff, where an actual ignore value can be passed to diff to
override the per submodule settings in the configuration.

This required tweaking the constants for ignore values so that
zero would not be used and could represent an unset option to the
diff.  This was an opportunity to move the submodule values into
include/git2/types.h and to rename the poorly named DEFAULT values
for ignore and update constants to RESET instead.

Now the GIT_DIFF_IGNORE_SUBMODULES flag is exactly the same as
setting the ignore_submodules option to GIT_SUBMODULE_IGNORE_ALL
(which is actually a minor change from the old behavior in that
submodules will now be treated as UNMODIFIED deltas instead of
being left out totally - if you set GIT_DIFF_INCLUDE_UNMODIFIED).

This includes tests for the various new settings.
parent 2e3e273e
...@@ -78,7 +78,7 @@ typedef enum { ...@@ -78,7 +78,7 @@ typedef enum {
GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3), GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3),
/** Ignore whitespace at end of line */ /** Ignore whitespace at end of line */
GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4), GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4),
/** Exclude submodules from the diff completely */ /** Treat all submodules as unmodified */
GIT_DIFF_IGNORE_SUBMODULES = (1 << 5), GIT_DIFF_IGNORE_SUBMODULES = (1 << 5),
/** Use the "patience diff" algorithm (currently unimplemented) */ /** Use the "patience diff" algorithm (currently unimplemented) */
GIT_DIFF_PATIENCE = (1 << 6), GIT_DIFF_PATIENCE = (1 << 6),
...@@ -314,6 +314,8 @@ typedef int (*git_diff_notify_cb)( ...@@ -314,6 +314,8 @@ typedef int (*git_diff_notify_cb)(
* - `notify_cb` is an optional callback function, notifying the consumer of * - `notify_cb` is an optional callback function, notifying the consumer of
* which files are being examined as the diff is generated * which files are being examined as the diff is generated
* - `notify_payload` is the payload data to pass to the `notify_cb` function * - `notify_payload` is the payload data to pass to the `notify_cb` function
* - `ignore_submodules` overrides the submodule ignore setting for all
* submodules in the diff.
*/ */
typedef struct { typedef struct {
unsigned int version; /**< version for the struct */ unsigned int version; /**< version for the struct */
...@@ -326,6 +328,7 @@ typedef struct { ...@@ -326,6 +328,7 @@ typedef struct {
git_off_t max_size; /**< defaults to 512MB */ git_off_t max_size; /**< defaults to 512MB */
git_diff_notify_cb notify_cb; git_diff_notify_cb notify_cb;
void *notify_payload; void *notify_payload;
git_submodule_ignore_t ignore_submodules; /** << submodule ignore rule */
} git_diff_options; } git_diff_options;
#define GIT_DIFF_OPTIONS_VERSION 1 #define GIT_DIFF_OPTIONS_VERSION 1
......
...@@ -14,51 +14,18 @@ ...@@ -14,51 +14,18 @@
/** /**
* @file git2/submodule.h * @file git2/submodule.h
* @brief Git submodule management utilities * @brief Git submodule management utilities
* @defgroup git_submodule Git submodule management routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Opaque structure representing a submodule.
* *
* Submodule support in libgit2 builds a list of known submodules and keeps * Submodule support in libgit2 builds a list of known submodules and keeps
* it in the repository. The list is built from the .gitmodules file, the * it in the repository. The list is built from the .gitmodules file, the
* .git/config file, the index, and the HEAD tree. Items in the working * .git/config file, the index, and the HEAD tree. Items in the working
* directory that look like submodules (i.e. a git repo) but are not * directory that look like submodules (i.e. a git repo) but are not
* mentioned in those places won't be tracked. * mentioned in those places won't be tracked.
*/
typedef struct git_submodule git_submodule;
/**
* Values that could be specified for the update rule of a submodule.
* *
* Use the DEFAULT value if you have altered the update value via * @defgroup git_submodule Git submodule management routines
* `git_submodule_set_update()` and wish to reset to the original default. * @ingroup Git
*/ * @{
typedef enum {
GIT_SUBMODULE_UPDATE_DEFAULT = -1,
GIT_SUBMODULE_UPDATE_CHECKOUT = 0,
GIT_SUBMODULE_UPDATE_REBASE = 1,
GIT_SUBMODULE_UPDATE_MERGE = 2,
GIT_SUBMODULE_UPDATE_NONE = 3
} git_submodule_update_t;
/**
* Values that could be specified for how closely to examine the
* working directory when getting submodule status.
*
* Use the DEFUALT value if you have altered the ignore value via
* `git_submodule_set_ignore()` and wish to reset to the original value.
*/ */
typedef enum { GIT_BEGIN_DECL
GIT_SUBMODULE_IGNORE_DEFAULT = -1, /* reset to default */
GIT_SUBMODULE_IGNORE_NONE = 0, /* any change or untracked == dirty */
GIT_SUBMODULE_IGNORE_UNTRACKED = 1, /* dirty if tracked files change */
GIT_SUBMODULE_IGNORE_DIRTY = 2, /* only dirty if HEAD moved */
GIT_SUBMODULE_IGNORE_ALL = 3 /* never dirty */
} git_submodule_ignore_t;
/** /**
* Return codes for submodule status. * Return codes for submodule status.
...@@ -377,9 +344,9 @@ GIT_EXTERN(git_submodule_ignore_t) git_submodule_ignore( ...@@ -377,9 +344,9 @@ GIT_EXTERN(git_submodule_ignore_t) git_submodule_ignore(
* submodule is in memory. You should call `git_submodule_save()` if you * submodule is in memory. You should call `git_submodule_save()` if you
* want to persist the new ignore role. * want to persist the new ignore role.
* *
* Calling this again with GIT_SUBMODULE_IGNORE_DEFAULT or calling * Calling this again with GIT_SUBMODULE_IGNORE_RESET or calling
* `git_submodule_reload()` will revert the rule to the value that was in the * `git_submodule_reload()` will revert the rule to the value that was in
* original config. * the original config.
* *
* @return old value for ignore * @return old value for ignore
*/ */
...@@ -399,9 +366,9 @@ GIT_EXTERN(git_submodule_update_t) git_submodule_update( ...@@ -399,9 +366,9 @@ GIT_EXTERN(git_submodule_update_t) git_submodule_update(
* This sets the update rule in memory for the submodule. You should call * This sets the update rule in memory for the submodule. You should call
* `git_submodule_save()` if you want to persist the new update rule. * `git_submodule_save()` if you want to persist the new update rule.
* *
* Calling this again with GIT_SUBMODULE_UPDATE_DEFAULT or calling * Calling this again with GIT_SUBMODULE_UPDATE_RESET or calling
* `git_submodule_reload()` will revert the rule to the value that was in the * `git_submodule_reload()` will revert the rule to the value that was in
* original config. * the original config.
* *
* @return old value for update * @return old value for update
*/ */
......
...@@ -229,6 +229,40 @@ typedef struct git_transfer_progress { ...@@ -229,6 +229,40 @@ typedef struct git_transfer_progress {
*/ */
typedef int (*git_transfer_progress_callback)(const git_transfer_progress *stats, void *payload); typedef int (*git_transfer_progress_callback)(const git_transfer_progress *stats, void *payload);
/**
* Opaque structure representing a submodule.
*/
typedef struct git_submodule git_submodule;
/**
* Values that could be specified for the update rule of a submodule.
*
* Use the RESET value if you have altered the in-memory update value via
* `git_submodule_set_update()` and wish to reset to the original default.
*/
typedef enum {
GIT_SUBMODULE_UPDATE_RESET = -1,
GIT_SUBMODULE_UPDATE_CHECKOUT = 1,
GIT_SUBMODULE_UPDATE_REBASE = 2,
GIT_SUBMODULE_UPDATE_MERGE = 3,
GIT_SUBMODULE_UPDATE_NONE = 4
} git_submodule_update_t;
/**
* Values that could be specified for how closely to examine the
* working directory when getting submodule status.
*
* Use the RESET value if you have altered the in-memory ignore value via
* `git_submodule_set_ignore()` and wish to reset to the original value.
*/
typedef enum {
GIT_SUBMODULE_IGNORE_RESET = -1, /* reset to on-disk value */
GIT_SUBMODULE_IGNORE_NONE = 1, /* any change or untracked == dirty */
GIT_SUBMODULE_IGNORE_UNTRACKED = 2, /* dirty if tracked files change */
GIT_SUBMODULE_IGNORE_DIRTY = 3, /* only dirty if HEAD moved */
GIT_SUBMODULE_IGNORE_ALL = 4 /* never dirty */
} git_submodule_ignore_t;
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -78,10 +78,6 @@ static int diff_delta__from_one( ...@@ -78,10 +78,6 @@ static int diff_delta__from_one(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED)) DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
return 0; return 0;
if (entry->mode == GIT_FILEMODE_COMMIT &&
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
return 0;
if (!git_pathspec__match( if (!git_pathspec__match(
&diff->pathspec, entry->path, &diff->pathspec, entry->path,
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH), DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
...@@ -141,11 +137,6 @@ static int diff_delta__from_two( ...@@ -141,11 +137,6 @@ static int diff_delta__from_two(
DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED)) DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
return 0; return 0;
if (old_entry->mode == GIT_FILEMODE_COMMIT &&
new_entry->mode == GIT_FILEMODE_COMMIT &&
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
return 0;
if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) { if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
uint32_t temp_mode = old_mode; uint32_t temp_mode = old_mode;
const git_index_entry *temp_entry = old_entry; const git_index_entry *temp_entry = old_entry;
...@@ -431,8 +422,18 @@ static int diff_list_apply_options( ...@@ -431,8 +422,18 @@ static int diff_list_apply_options(
if (!opts) { if (!opts) {
diff->opts.context_lines = config_int(cfg, "diff.context", 3); diff->opts.context_lines = config_int(cfg, "diff.context", 3);
if (config_bool(cfg, "diff.ignoreSubmodules", 0)) /* add other defaults here */
diff->opts.flags |= GIT_DIFF_IGNORE_SUBMODULES; }
/* if ignore_submodules not explicitly set, check diff config */
if (diff->opts.ignore_submodules <= 0) {
const char *str;
if (git_config_get_string(&str , cfg, "diff.ignoreSubmodules") < 0)
giterr_clear();
else if (str != NULL &&
git_submodule_parse_ignore(&diff->opts.ignore_submodules, str) < 0)
giterr_clear();
} }
/* if either prefix is not set, figure out appropriate value */ /* if either prefix is not set, figure out appropriate value */
...@@ -596,17 +597,32 @@ static int maybe_modified_submodule( ...@@ -596,17 +597,32 @@ static int maybe_modified_submodule(
int error = 0; int error = 0;
git_submodule *sub; git_submodule *sub;
unsigned int sm_status = 0; unsigned int sm_status = 0;
git_submodule_ignore_t ign = diff->opts.ignore_submodules;
*status = GIT_DELTA_UNMODIFIED; *status = GIT_DELTA_UNMODIFIED;
if (!DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) && if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES) ||
!(error = git_submodule_lookup( ign == GIT_SUBMODULE_IGNORE_ALL)
&sub, diff->repo, info->nitem->path)) && return 0;
git_submodule_ignore(sub) != GIT_SUBMODULE_IGNORE_ALL &&
!(error = git_submodule__status( if ((error = git_submodule_lookup(
&sm_status, NULL, NULL, found_oid, sub, &sub, diff->repo, info->nitem->path)) < 0) {
GIT_SUBMODULE_IGNORE_DEFAULT)))
{ /* GIT_EEXISTS means dir with .git in it was found - ignore it */
if (error == GIT_EEXISTS) {
giterr_clear();
error = 0;
}
return error;
}
if (ign <= 0 && git_submodule_ignore(sub) == GIT_SUBMODULE_IGNORE_ALL)
return 0;
if ((error = git_submodule__status(
&sm_status, NULL, NULL, found_oid, sub, ign)) < 0)
return error;
/* check IS_WD_UNMODIFIED because this case is only used /* check IS_WD_UNMODIFIED because this case is only used
* when the new side of the diff is the working directory * when the new side of the diff is the working directory
*/ */
...@@ -617,15 +633,8 @@ static int maybe_modified_submodule( ...@@ -617,15 +633,8 @@ static int maybe_modified_submodule(
if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 && if ((sm_status & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
!git_oid_equal(&info->oitem->oid, found_oid)) !git_oid_equal(&info->oitem->oid, found_oid))
*status = GIT_DELTA_MODIFIED; *status = GIT_DELTA_MODIFIED;
}
/* GIT_EEXISTS means a dir with .git in it was found - ignore it */
if (error == GIT_EEXISTS) {
giterr_clear();
error = 0;
}
return error; return 0;
} }
static int maybe_modified( static int maybe_modified(
......
...@@ -30,6 +30,8 @@ static git_cvar_map _sm_update_map[] = { ...@@ -30,6 +30,8 @@ static git_cvar_map _sm_update_map[] = {
{GIT_CVAR_STRING, "rebase", GIT_SUBMODULE_UPDATE_REBASE}, {GIT_CVAR_STRING, "rebase", GIT_SUBMODULE_UPDATE_REBASE},
{GIT_CVAR_STRING, "merge", GIT_SUBMODULE_UPDATE_MERGE}, {GIT_CVAR_STRING, "merge", GIT_SUBMODULE_UPDATE_MERGE},
{GIT_CVAR_STRING, "none", GIT_SUBMODULE_UPDATE_NONE}, {GIT_CVAR_STRING, "none", GIT_SUBMODULE_UPDATE_NONE},
{GIT_CVAR_FALSE, NULL, GIT_SUBMODULE_UPDATE_NONE},
{GIT_CVAR_TRUE, NULL, GIT_SUBMODULE_UPDATE_CHECKOUT},
}; };
static git_cvar_map _sm_ignore_map[] = { static git_cvar_map _sm_ignore_map[] = {
...@@ -37,6 +39,8 @@ static git_cvar_map _sm_ignore_map[] = { ...@@ -37,6 +39,8 @@ static git_cvar_map _sm_ignore_map[] = {
{GIT_CVAR_STRING, "untracked", GIT_SUBMODULE_IGNORE_UNTRACKED}, {GIT_CVAR_STRING, "untracked", GIT_SUBMODULE_IGNORE_UNTRACKED},
{GIT_CVAR_STRING, "dirty", GIT_SUBMODULE_IGNORE_DIRTY}, {GIT_CVAR_STRING, "dirty", GIT_SUBMODULE_IGNORE_DIRTY},
{GIT_CVAR_STRING, "all", GIT_SUBMODULE_IGNORE_ALL}, {GIT_CVAR_STRING, "all", GIT_SUBMODULE_IGNORE_ALL},
{GIT_CVAR_FALSE, NULL, GIT_SUBMODULE_IGNORE_NONE},
{GIT_CVAR_TRUE, NULL, GIT_SUBMODULE_IGNORE_ALL},
}; };
static kh_inline khint_t str_hash_no_trailing_slash(const char *s) static kh_inline khint_t str_hash_no_trailing_slash(const char *s)
...@@ -406,11 +410,30 @@ cleanup: ...@@ -406,11 +410,30 @@ cleanup:
return error; return error;
} }
const char *git_submodule_ignore_to_str(git_submodule_ignore_t ignore)
{
int i;
for (i = 0; i < (int)ARRAY_SIZE(_sm_ignore_map); ++i)
if (_sm_ignore_map[i].map_value == ignore)
return _sm_ignore_map[i].str_match;
return NULL;
}
const char *git_submodule_update_to_str(git_submodule_update_t update)
{
int i;
for (i = 0; i < (int)ARRAY_SIZE(_sm_update_map); ++i)
if (_sm_update_map[i].map_value == update)
return _sm_update_map[i].str_match;
return NULL;
}
int git_submodule_save(git_submodule *submodule) int git_submodule_save(git_submodule *submodule)
{ {
int error = 0; int error = 0;
git_config_backend *mods; git_config_backend *mods;
git_buf key = GIT_BUF_INIT; git_buf key = GIT_BUF_INIT;
const char *val;
assert(submodule); assert(submodule);
...@@ -435,22 +458,14 @@ int git_submodule_save(git_submodule *submodule) ...@@ -435,22 +458,14 @@ int git_submodule_save(git_submodule *submodule)
goto cleanup; goto cleanup;
if (!(error = submodule_config_key_trunc_puts(&key, "update")) && if (!(error = submodule_config_key_trunc_puts(&key, "update")) &&
submodule->update != GIT_SUBMODULE_UPDATE_DEFAULT) (val = git_submodule_update_to_str(submodule->update)) != NULL)
{
const char *val = (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ?
NULL : _sm_update_map[submodule->update].str_match;
error = git_config_file_set_string(mods, key.ptr, val); error = git_config_file_set_string(mods, key.ptr, val);
}
if (error < 0) if (error < 0)
goto cleanup; goto cleanup;
if (!(error = submodule_config_key_trunc_puts(&key, "ignore")) && if (!(error = submodule_config_key_trunc_puts(&key, "ignore")) &&
submodule->ignore != GIT_SUBMODULE_IGNORE_DEFAULT) (val = git_submodule_ignore_to_str(submodule->ignore)) != NULL)
{
const char *val = (submodule->ignore == GIT_SUBMODULE_IGNORE_NONE) ?
NULL : _sm_ignore_map[submodule->ignore].str_match;
error = git_config_file_set_string(mods, key.ptr, val); error = git_config_file_set_string(mods, key.ptr, val);
}
if (error < 0) if (error < 0)
goto cleanup; goto cleanup;
...@@ -554,7 +569,8 @@ const git_oid *git_submodule_wd_id(git_submodule *submodule) ...@@ -554,7 +569,8 @@ const git_oid *git_submodule_wd_id(git_submodule *submodule)
git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule) git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule)
{ {
assert(submodule); assert(submodule);
return submodule->ignore; return (submodule->ignore < GIT_SUBMODULE_IGNORE_NONE) ?
GIT_SUBMODULE_IGNORE_NONE : submodule->ignore;
} }
git_submodule_ignore_t git_submodule_set_ignore( git_submodule_ignore_t git_submodule_set_ignore(
...@@ -564,7 +580,7 @@ git_submodule_ignore_t git_submodule_set_ignore( ...@@ -564,7 +580,7 @@ git_submodule_ignore_t git_submodule_set_ignore(
assert(submodule); assert(submodule);
if (ignore == GIT_SUBMODULE_IGNORE_DEFAULT) if (ignore == GIT_SUBMODULE_IGNORE_RESET)
ignore = submodule->ignore_default; ignore = submodule->ignore_default;
old = submodule->ignore; old = submodule->ignore;
...@@ -575,7 +591,8 @@ git_submodule_ignore_t git_submodule_set_ignore( ...@@ -575,7 +591,8 @@ git_submodule_ignore_t git_submodule_set_ignore(
git_submodule_update_t git_submodule_update(git_submodule *submodule) git_submodule_update_t git_submodule_update(git_submodule *submodule)
{ {
assert(submodule); assert(submodule);
return submodule->update; return (submodule->update < GIT_SUBMODULE_UPDATE_CHECKOUT) ?
GIT_SUBMODULE_UPDATE_CHECKOUT : submodule->update;
} }
git_submodule_update_t git_submodule_set_update( git_submodule_update_t git_submodule_set_update(
...@@ -585,7 +602,7 @@ git_submodule_update_t git_submodule_set_update( ...@@ -585,7 +602,7 @@ git_submodule_update_t git_submodule_set_update(
assert(submodule); assert(submodule);
if (update == GIT_SUBMODULE_UPDATE_DEFAULT) if (update == GIT_SUBMODULE_UPDATE_RESET)
update = submodule->update_default; update = submodule->update_default;
old = submodule->update; old = submodule->update;
...@@ -616,6 +633,7 @@ int git_submodule_set_fetch_recurse_submodules( ...@@ -616,6 +633,7 @@ int git_submodule_set_fetch_recurse_submodules(
int git_submodule_init(git_submodule *submodule, int overwrite) int git_submodule_init(git_submodule *submodule, int overwrite)
{ {
int error; int error;
const char *val;
/* write "submodule.NAME.url" */ /* write "submodule.NAME.url" */
...@@ -632,14 +650,10 @@ int git_submodule_init(git_submodule *submodule, int overwrite) ...@@ -632,14 +650,10 @@ int git_submodule_init(git_submodule *submodule, int overwrite)
/* write "submodule.NAME.update" if not default */ /* write "submodule.NAME.update" if not default */
if (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) val = (submodule->update == GIT_SUBMODULE_UPDATE_CHECKOUT) ?
error = submodule_update_config( NULL : git_submodule_update_to_str(submodule->update);
submodule, "update", NULL, (overwrite != 0), false);
else if (submodule->update != GIT_SUBMODULE_UPDATE_DEFAULT)
error = submodule_update_config( error = submodule_update_config(
submodule, "update", submodule, "update", val, (overwrite != 0), false);
_sm_update_map[submodule->update].str_match,
(overwrite != 0), false);
return error; return error;
} }
...@@ -873,7 +887,7 @@ int git_submodule__status( ...@@ -873,7 +887,7 @@ int git_submodule__status(
unsigned int status; unsigned int status;
git_repository *smrepo = NULL; git_repository *smrepo = NULL;
if (ign == GIT_SUBMODULE_IGNORE_DEFAULT) if (ign < GIT_SUBMODULE_IGNORE_NONE)
ign = sm->ignore; ign = sm->ignore;
/* only return location info if ignore == all */ /* only return location info if ignore == all */
...@@ -926,8 +940,7 @@ int git_submodule_status(unsigned int *status, git_submodule *sm) ...@@ -926,8 +940,7 @@ int git_submodule_status(unsigned int *status, git_submodule *sm)
{ {
assert(status && sm); assert(status && sm);
return git_submodule__status( return git_submodule__status(status, NULL, NULL, NULL, sm, 0);
status, NULL, NULL, NULL, sm, GIT_SUBMODULE_IGNORE_DEFAULT);
} }
int git_submodule_location(unsigned int *location, git_submodule *sm) int git_submodule_location(unsigned int *location, git_submodule *sm)
...@@ -964,7 +977,10 @@ static git_submodule *submodule_alloc(git_repository *repo, const char *name) ...@@ -964,7 +977,10 @@ static git_submodule *submodule_alloc(git_repository *repo, const char *name)
} }
GIT_REFCOUNT_INC(sm); GIT_REFCOUNT_INC(sm);
sm->ignore = sm->ignore_default = GIT_SUBMODULE_IGNORE_NONE;
sm->update = sm->update_default = GIT_SUBMODULE_UPDATE_CHECKOUT;
sm->repo = repo; sm->repo = repo;
return sm; return sm;
} }
...@@ -1039,6 +1055,34 @@ static int submodule_config_error(const char *property, const char *value) ...@@ -1039,6 +1055,34 @@ static int submodule_config_error(const char *property, const char *value)
return -1; return -1;
} }
int git_submodule_parse_ignore(git_submodule_ignore_t *out, const char *value)
{
int val;
if (git_config_lookup_map_value(
&val, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), value) < 0) {
*out = GIT_SUBMODULE_IGNORE_NONE;
return submodule_config_error("ignore", value);
}
*out = (git_submodule_ignore_t)val;
return 0;
}
int git_submodule_parse_update(git_submodule_update_t *out, const char *value)
{
int val;
if (git_config_lookup_map_value(
&val, _sm_update_map, ARRAY_SIZE(_sm_update_map), value) < 0) {
*out = GIT_SUBMODULE_UPDATE_CHECKOUT;
return submodule_config_error("update", value);
}
*out = (git_submodule_update_t)val;
return 0;
}
static int submodule_load_from_config( static int submodule_load_from_config(
const git_config_entry *entry, void *data) const git_config_entry *entry, void *data)
{ {
...@@ -1120,22 +1164,18 @@ static int submodule_load_from_config( ...@@ -1120,22 +1164,18 @@ static int submodule_load_from_config(
return -1; return -1;
} }
else if (strcasecmp(property, "update") == 0) { else if (strcasecmp(property, "update") == 0) {
int val; if (git_submodule_parse_update(&sm->update, value) < 0)
if (git_config_lookup_map_value( return -1;
&val, _sm_update_map, ARRAY_SIZE(_sm_update_map), value) < 0) sm->update_default = sm->update;
return submodule_config_error("update", value);
sm->update_default = sm->update = (git_submodule_update_t)val;
} }
else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) { else if (strcasecmp(property, "fetchRecurseSubmodules") == 0) {
if (git__parse_bool(&sm->fetch_recurse, value) < 0) if (git__parse_bool(&sm->fetch_recurse, value) < 0)
return submodule_config_error("fetchRecurseSubmodules", value); return submodule_config_error("fetchRecurseSubmodules", value);
} }
else if (strcasecmp(property, "ignore") == 0) { else if (strcasecmp(property, "ignore") == 0) {
int val; if (git_submodule_parse_ignore(&sm->ignore, value) < 0)
if (git_config_lookup_map_value( return -1;
&val, _sm_ignore_map, ARRAY_SIZE(_sm_ignore_map), value) < 0) sm->ignore_default = sm->ignore;
return submodule_config_error("ignore", value);
sm->ignore_default = sm->ignore = (git_submodule_ignore_t)val;
} }
/* ignore other unknown submodule properties */ /* ignore other unknown submodule properties */
......
...@@ -127,4 +127,12 @@ extern int git_submodule_open_bare( ...@@ -127,4 +127,12 @@ extern int git_submodule_open_bare(
/* Release reference to submodule object - not currently for external use */ /* Release reference to submodule object - not currently for external use */
extern void git_submodule_free(git_submodule *sm); extern void git_submodule_free(git_submodule *sm);
extern int git_submodule_parse_ignore(
git_submodule_ignore_t *out, const char *value);
extern int git_submodule_parse_update(
git_submodule_update_t *out, const char *value);
extern const char *git_submodule_ignore_to_str(git_submodule_ignore_t);
extern const char *git_submodule_update_to_str(git_submodule_update_t);
#endif #endif
...@@ -29,6 +29,9 @@ ...@@ -29,6 +29,9 @@
void cl_git_report_failure(int, const char *, int, const char *); void cl_git_report_failure(int, const char *, int, const char *);
#define cl_assert_at_line(expr,file,line) \
clar__assert((expr) != 0, file, line, "Expression is not true: " #expr, NULL, 1)
#define cl_assert_equal_sz(sz1,sz2) cl_assert_equal_i((int)sz1, (int)(sz2)) #define cl_assert_equal_sz(sz1,sz2) cl_assert_equal_i((int)sz1, (int)(sz2))
/* /*
......
...@@ -49,7 +49,7 @@ static void check_diff_patches_at_line( ...@@ -49,7 +49,7 @@ static void check_diff_patches_at_line(
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d)); cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d));
if (delta->status == GIT_DELTA_UNMODIFIED) { if (delta->status == GIT_DELTA_UNMODIFIED) {
clar__assert(expected[d] == NULL, file, line, "found UNMODIFIED delta where modified was expected", NULL, 1); cl_assert_at_line(expected[d] == NULL, file, line);
continue; continue;
} }
...@@ -57,7 +57,7 @@ static void check_diff_patches_at_line( ...@@ -57,7 +57,7 @@ static void check_diff_patches_at_line(
continue; continue;
if (expected[d] && !strcmp(expected[d], "<END>")) { if (expected[d] && !strcmp(expected[d], "<END>")) {
cl_git_pass(git_diff_patch_to_str(&patch_text, patch)); cl_git_pass(git_diff_patch_to_str(&patch_text, patch));
clar__assert(0, file, line, "expected end of deltas, but found more", patch_text, 1); cl_assert_at_line(!strcmp(expected[d], "<END>"), file, line);
} }
cl_git_pass(git_diff_patch_to_str(&patch_text, patch)); cl_git_pass(git_diff_patch_to_str(&patch_text, patch));
...@@ -67,7 +67,7 @@ static void check_diff_patches_at_line( ...@@ -67,7 +67,7 @@ static void check_diff_patches_at_line(
git__free(patch_text); git__free(patch_text);
} }
clar__assert(expected[d] && !strcmp(expected[d], "<END>"), file, line, "found fewer deltas than expected", expected[d], 1); cl_assert_at_line(expected[d] && !strcmp(expected[d], "<END>"), file, line);
} }
#define check_diff_patches(diff, exp) \ #define check_diff_patches(diff, exp) \
...@@ -382,3 +382,62 @@ void test_diff_submodules__invalid_cache(void) ...@@ -382,3 +382,62 @@ void test_diff_submodules__invalid_cache(void)
git_index_free(smindex); git_index_free(smindex);
git_repository_free(smrepo); git_repository_free(smrepo);
} }
void test_diff_submodules__diff_ignore_options(void)
{
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
git_diff_list *diff = NULL;
static const char *expected_normal[] = {
"<SKIP>", /* .gitmodules */
NULL, /* not-submodule */
NULL, /* not */
"diff --git a/sm_changed_file b/sm_changed_file\nindex 4800958..4800958 160000\n--- a/sm_changed_file\n+++ b/sm_changed_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_file */
"diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */
"diff --git a/sm_changed_index b/sm_changed_index\nindex 4800958..4800958 160000\n--- a/sm_changed_index\n+++ b/sm_changed_index\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_index */
"diff --git a/sm_changed_untracked_file b/sm_changed_untracked_file\nindex 4800958..4800958 160000\n--- a/sm_changed_untracked_file\n+++ b/sm_changed_untracked_file\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0-dirty\n", /* sm_changed_untracked_file */
"diff --git a/sm_missing_commits b/sm_missing_commits\nindex 4800958..5e49635 160000\n--- a/sm_missing_commits\n+++ b/sm_missing_commits\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 5e4963595a9774b90524d35a807169049de8ccad\n", /* sm_missing_commits */
"<END>"
};
static const char *expected_ignore_all[] = {
"<SKIP>", /* .gitmodules */
NULL, /* not-submodule */
NULL, /* not */
"<END>"
};
static const char *expected_ignore_dirty[] = {
"<SKIP>", /* .gitmodules */
NULL, /* not-submodule */
NULL, /* not */
"diff --git a/sm_changed_head b/sm_changed_head\nindex 4800958..3d9386c 160000\n--- a/sm_changed_head\n+++ b/sm_changed_head\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 3d9386c507f6b093471a3e324085657a3c2b4247\n", /* sm_changed_head */
"diff --git a/sm_missing_commits b/sm_missing_commits\nindex 4800958..5e49635 160000\n--- a/sm_missing_commits\n+++ b/sm_missing_commits\n@@ -1 +1 @@\n-Subproject commit 480095882d281ed676fe5b863569520e54a7d5c0\n+Subproject commit 5e4963595a9774b90524d35a807169049de8ccad\n", /* sm_missing_commits */
"<END>"
};
setup_submodules2();
opts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
opts.old_prefix = "a"; opts.new_prefix = "b";
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
check_diff_patches(diff, expected_normal);
git_diff_list_free(diff);
opts.flags |= GIT_DIFF_IGNORE_SUBMODULES;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
check_diff_patches(diff, expected_ignore_all);
git_diff_list_free(diff);
opts.flags &= ~GIT_DIFF_IGNORE_SUBMODULES;
opts.ignore_submodules = GIT_SUBMODULE_IGNORE_ALL;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
check_diff_patches(diff, expected_ignore_all);
git_diff_list_free(diff);
opts.ignore_submodules = GIT_SUBMODULE_IGNORE_DIRTY;
cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));
check_diff_patches(diff, expected_ignore_dirty);
git_diff_list_free(diff);
}
...@@ -204,10 +204,10 @@ void test_submodule_modify__edit_and_save(void) ...@@ -204,10 +204,10 @@ void test_submodule_modify__edit_and_save(void)
cl_git_pass(git_submodule_set_url(sm1, old_url)); cl_git_pass(git_submodule_set_url(sm1, old_url));
cl_assert_equal_i( cl_assert_equal_i(
(int)GIT_SUBMODULE_IGNORE_UNTRACKED, (int)GIT_SUBMODULE_IGNORE_UNTRACKED,
(int)git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT)); (int)git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET));
cl_assert_equal_i( cl_assert_equal_i(
(int)GIT_SUBMODULE_UPDATE_REBASE, (int)GIT_SUBMODULE_UPDATE_REBASE,
(int)git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT)); (int)git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
cl_assert_equal_i( cl_assert_equal_i(
1, git_submodule_set_fetch_recurse_submodules(sm1, old_fetchrecurse)); 1, git_submodule_set_fetch_recurse_submodules(sm1, old_fetchrecurse));
...@@ -228,10 +228,10 @@ void test_submodule_modify__edit_and_save(void) ...@@ -228,10 +228,10 @@ void test_submodule_modify__edit_and_save(void)
cl_git_pass(git_submodule_save(sm1)); cl_git_pass(git_submodule_save(sm1));
/* attempt to "revert" values */ /* attempt to "revert" values */
git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_DEFAULT); git_submodule_set_ignore(sm1, GIT_SUBMODULE_IGNORE_RESET);
git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_DEFAULT); git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);
/* but ignore and update should NOT revert because the DEFAULT /* but ignore and update should NOT revert because the RESET
* should now be the newly saved value... * should now be the newly saved value...
*/ */
cl_assert_equal_i( cl_assert_equal_i(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment