10000 Add example program to emulate 'git log' and create pathspec matching API by arrbee · Pull Request #1711 · libgit2/libgit2 · GitHub
[go: up one dir, main page]

Skip to content

Add example program to emulate 'git log' and create pathspec matching API #1711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add public API for pathspec matching
This
10000
 adds a new public API for compiling pathspecs and matching
them against the working directory, the index, or a tree from the
repository.  This also reworks the pathspec internals to allow the
sharing of code between the existing internal usage of pathspec
matching and the new external API.

While this is working and the new API is ready for discussion, I
think there is still an incorrect behavior in which patterns are
always matched against the full path of an entry without taking
the subdirectories into account (so "s*" will match "subdir/file"
even though it wouldn't with core Git).  Further enhancements are
coming, but this was a good place to take a functional snapshot.
  • Loading branch information
arrbee committed Jul 3, 2013
commit f8569d358275b71249b0a4b0a562ceb22efd132b
8 changes: 8 additions & 0 deletions include/git2/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ typedef enum {
GIT_INDEX_ADD_CHECK_PATHSPEC = (1u << 2),
} git_index_add_option_t;

/**
* Match any index stage.
*
* Some index APIs take a stage to match; pass this value to match
* any entry matching the path regardless of stage.
*/
#define GIT_INDEX_STAGE_ANY -1

/** @name Index File Functions
*
* These functions work on the index file itself.
Expand Down
202 changes: 202 additions & 0 deletions include/git2/pathspec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_git_pathspec_h__
#define INCLUDE_git_pathspec_h__

#include "common.h"
#include "types.h"
#include "strarray.h"

/**
* Compiled pathspec
*/
typedef struct git_pathspec git_pathspec;

/**
* List of filenames matching a pathspec
*/
typedef struct git_pathspec_match_list git_pathspec_match_list;

/**
* Options controlling how pathspec match should be executed
*
* - GIT_PATHSPEC_IGNORE_CASE forces match to ignore case; otherwise
* match will use native case sensitivity of platform
* - GIT_PATHSPEC_USE_CASE forces case sensitive match; otherwise
* match will use native case sensitivity of platform
* - GIT_PATHSPEC_NO_GLOB disables glob patterns and just uses simple
* string comparison for matching
* - GIT_PATHSPEC_NO_MATCH_ERROR means the match function will return
* GIT_ENOTFOUND if no matches are found; otherwise it will return 0
* for success and `git_pathspec_match_list_entrycount` will be 0.
* - GIT_PATHSPEC_FIND_FAILURES only applies to a git_pathspec_match_list;
* it means to check file names against all unmatched patterns so that
* at the end of a match we can identify patterns that did not match any
* files.
* - GIT_PATHSPEC_FAILURES_ONLY only applies to a git_pathspec_match_list;
* it means to only check for mismatches and not record matched paths.
10000 */
typedef enum {
GIT_PATHSPEC_DEFAULT = 0,
GIT_PATHSPEC_IGNORE_CASE = (1u << 0),
GIT_PATHSPEC_USE_CASE = (1u << 1),
GIT_PATHSPEC_NO_GLOB = (1u << 2),
GIT_PATHSPEC_NO_MATCH_ERROR = (1u << 3),
GIT_PATHSPEC_FIND_FAILURES = (1u << 4),
GIT_PATHSPEC_FAILURES_ONLY = (1u << 5),
} git_pathspec_flag_t;

/**
* Compile a pathspec
*
* @param out Output of the compiled pathspec
* @param flags Combination of git_pathspec_flag_t values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused parameter?

* @param pathspec A git_strarray of the paths to match
* @return 0 on success, <0 on failure
*/
GIT_EXTERN(int) git_pathspec_new(
git_pathspec **out, const git_strarray *pathspec);

/**
* Free a pathspec
*
* @param ps The compiled pathspec
*/
GIT_EXTERN(void) git_pathspec_free(git_pathspec *ps);

/**
* Try to match a path against a pathspec
*
* Unlike most of the other pathspec matching functions, this will not
* fall back on the native case-sensitivity for your platform. You must
* explicitly pass flags to control case sensitivity or else this will
* fall back on being case sensitive.
*
* @param ps The compiled pathspec
* @param flags Match flags to influence matching behavior
* @param path The pathname to attempt to match
* @return 1 is path matches spec, 0 if it does not
*/
GIT_EXTERN(int) git_pathspec_matches_path(
const git_pathspec *ps, uint32_t flags, const char *path);

/**
* Match a pathspec against the working directory of a repository.
*
* This returns a `git_patchspec_match` object that contains the list of
* all files matching the given pathspec in the working directory of the
* repository. This handles git ignores (i.e. ignored files will not be
* considered to match the `pathspec` unless the file is tracked in the
* index).
*
* @param out Object with list of matching items
* @param repo The repository in which to match; bare repo is an error
* @param flags Options to control matching behavior
* @param ps Pathspec to be matched
* @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and
* the GIT_PATHSPEC_NO_MATCH_ERROR flag is used
*/
GIT_EXTERN(int) git_pathspec_match_workdir(
git_pathspec_match_list **out,
git_repository *repo,
uint32_t flags,
git_pathspec *ps);

/**
* Match a pathspec against entries in an index.
*
* This returns a `git_patchspec_match` object that contains the list of
* all files matching the given pathspec in the index.
*
* NOTE: At the moment, the case sensitivity of this match is controlled
* by the current case-sensitivity of the index object itself and the
* USE_CASE and IGNORE_CASE flags will have no effect. This behavior will
* be corrected in a future release.
*
* @param out Object with list of matching items
* @param inex The index in which to match
* @param flags Options to control matching behavior
* @param ps Pathspec to be matched
* @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and
* the GIT_PATHSPEC_NO_MATCH_ERROR flag is used
*/
GIT_EXTERN(int) git_pathspec_match_index(
git_pathspec_match_list **out,
git_index *index,
uint32_t flags,
git_pathspec *ps);

/**
* Match a pathspec against files in a tree.
*
* This returns a `git_patchspec_match` object that contains the list of
* all files matching the given pathspec in the given tree.
*
* @param out Object with list of matching D7AE items
* @param inex The index in which to match
* @param flags Options to control matching behavior
* @param ps Pathspec to be matched
* @return 0 on success, -1 on error, GIT_ENOTFOUND if no matches and
* the GIT_PATHSPEC_NO_MATCH_ERROR flag is used
*/
GIT_EXTERN(int) git_pathspec_match_tree(
git_pathspec_match_list **out,
git_tree *tree,
uint32_t flags,
git_pathspec *ps);

/**
* Free memory associates with a git_pathspec_match_list
*
* @param m The git_pathspec_match_list to be freed
*/
GIT_EXTERN(void) git_pathspec_match_list_free(git_pathspec_match_list *m);

/**
* Get the number of items in a match list.
*
* @param m The git_pathspec_match_list object
* @return Number of items in match list
*/
GIT_EXTERN(size_t) git_pathspec_match_list_entrycount(
const git_pathspec_match_list *m);

/**
* Get a matching filename by position.
*
* @param m The git_pathspec_match_list object
* @param pos The index into the list
* @return The filename of the match
*/
GIT_EXTERN(const char *) git_pathspec_match_list_entry(
const git_pathspec_match_list *m, size_t pos);

/**
* Get the number of pathspec items that did not match.
*
* This will be zero unless you passed GIT_PATHSPEC_FIND_FAILURES when
* generating the git_pathspec_match_list.
*
* @param m The git_pathspec_match_list object
* @return Number of items in original pathspec that had no matches
*/
GIT_EXTERN(size_t) git_pathspec_match_list_failed_entrycount(
const git_pathspec_match_list *m);

/**
* Get an original pathspec string that had no matches.
*
* This will be return NULL for positions out of range.
*
* @param m The git_pathspec_match_list object
* @param pos The index into the failed items
* @return The pathspec pattern that didn't match anything
*/
GIT_EXTERN(const char *) git_pathspec_match_list_failed_entry(
const git_pathspec_match_list *m, size_t pos);

#endif
10 changes: 5 additions & 5 deletions src/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ static int checkout_action_wd_only(
bool remove = false;
git_checkout_notify_t notify = GIT_CHECKOUT_NOTIFY_NONE;

if (!git_pathspec_match_path(
if (!git_pathspec__match(
pathspec, wd->path,
(data->strategy & GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH) != 0,
git_iterator_ignore_case(workdir), NULL))
git_iterator_ignore_case(workdir), NULL, NULL))
return 0;

/* check if item is tracked in the index but not in the checkout diff */
Expand Down Expand Up @@ -607,7 +607,7 @@ static int checkout_get_actions(
uint32_t *actions = NULL;

if (data->opts.paths.count > 0 &&
git_pathspec_init(&pathspec, &data->opts.paths, &pathpool) < 0)
git_pathspec__vinit(&pathspec, &data->opts.paths, &pathpool) < 0)
return -1;

if ((error = git_iterator_current(&wditem, workdir)) < 0 &&
Expand Down Expand Up @@ -659,7 +659,7 @@ static int checkout_get_actions(
goto fail;
}

git_pathspec_free(&pathspec);
git_pathspec__vfree(&pathspec);
git_pool_clear(&pathpool);

return 0;
Expand All @@ -670,7 +670,7 @@ static int checkout_get_actions(
*actions_ptr = NULL;
git__free(actions);

git_pathspec_free(&pathspec);
git_pathspec__vfree(&pathspec);
git_pool_clear(&pathpool);

return error;
Expand Down
12 changes: 6 additions & 6 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ static int diff_delta__from_one(
DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
return 0;

if (!git_pathspec_match_path(
if (!git_pathspec__match(
&diff->pathspec, entry->path,
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE),
&matched_pathspec))
&matched_pathspec, NULL))
return 0;

delta = diff_delta__alloc(diff, status, entry->path);
Expand Down Expand Up @@ -387,7 +387,7 @@ static int diff_list_apply_options(
DIFF_FLAG_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE, icase);

/* initialize pathspec from options */
if (git_pathspec_init(&diff->pathspec, &opts->pathspec, pool) < 0)
if (git_pathspec__vinit(&diff->pathspec, &opts->pathspec, pool) < 0)
return -1;
}

Expand Down Expand Up @@ -473,7 +473,7 @@ static void diff_list_free(git_diff_list *diff)
}
git_vector_free(&diff->deltas);

git_pathspec_free(&diff->pathspec);
git_pathspec__vfree(&diff->pathspec);
git_pool_clear(&diff->pool);

git__memzero(diff, sizeof(*diff));
Expand Down Expand Up @@ -634,11 +634,11 @@ static int maybe_modified(
bool new_is_workdir = (info->new_iter->type == GIT_ITERATOR_TYPE_WORKDIR);
const char *matched_pathspec;

if (!git_pathspec_match_path(
if (!git_pathspec__match(
&diff->pathspec, oitem->path,
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE),
&matched_pathspec))
&matched_pathspec, NULL))
return 0;

memset(&noid, 0, sizeof(noid));
Expand Down
Loading
0