Skip to content

Commit

Permalink
Merge branch 'bc/master-diff-hunk-header-fix'
Browse files Browse the repository at this point in the history
* bc/master-diff-hunk-header-fix:
  Clarify commit error message for unmerged files
  Use strchrnul() instead of strchr() plus manual workaround
  Use remove_path from dir.c instead of own implementation
  Add remove_path: a function to remove as much as possible of a path
  git-submodule: Fix "Unable to checkout" for the initial 'update'
  Clarify how the user can satisfy stash's 'dirty state' check.
  t4018-diff-funcname: test syntax of builtin xfuncname patterns
  t4018-diff-funcname: test syntax of builtin xfuncname patterns
  make "git remote" report multiple URLs
  diff hunk pattern: fix misconverted "\{" tex macro introducers
  diff: fix "multiple regexp" semantics to find hunk header comment
  diff: use extended regexp to find hunk headers
  diff: use extended regexp to find hunk headers
  diff.*.xfuncname which uses "extended" regex's for hunk header selection
  diff.c: associate a flag with each pattern and use it for compiling regex
  diff.c: return pattern entry pointer rather than just the hunk header pattern

Conflicts:
	builtin-merge-recursive.c
	t/t7201-co.sh
	xdiff-interface.h
  • Loading branch information
spearce committed Sep 29, 2008
2 parents 9ba929e + 5a139ba commit 9800c0d
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 120 deletions.
4 changes: 2 additions & 2 deletions Documentation/gitattributes.txt
Expand Up @@ -288,13 +288,13 @@ for paths.
*.tex diff=tex
------------------------

Then, you would define a "diff.tex.funcname" configuration to
Then, you would define a "diff.tex.xfuncname" configuration to
specify a regular expression that matches a line that you would
want to appear as the hunk header "TEXT", like this:

------------------------
[diff "tex"]
funcname = "^\\(\\\\\\(sub\\)*section{.*\\)$"
xfuncname = "^(\\\\(sub)*section\\{.*)$"
------------------------

Note. A single level of backslashes are eaten by the
Expand Down
11 changes: 2 additions & 9 deletions builtin-apply.c
Expand Up @@ -13,6 +13,7 @@
#include "delta.h"
#include "builtin.h"
#include "string-list.h"
#include "dir.h"

/*
* --check turns on checking that the working tree matches the
Expand Down Expand Up @@ -2735,15 +2736,7 @@ static void remove_file(struct patch *patch, int rmdir_empty)
warning("unable to remove submodule %s",
patch->old_name);
} else if (!unlink(patch->old_name) && rmdir_empty) {
char *name = xstrdup(patch->old_name);
char *end = strrchr(name, '/');
while (end) {
*end = 0;
if (rmdir(name))
break;
end = strrchr(name, '/');
}
free(name);
remove_path(patch->old_name);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion builtin-commit.c
Expand Up @@ -639,7 +639,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
active_cache_tree = cache_tree();
if (cache_tree_update(active_cache_tree,
active_cache, active_nr, 0, 0) < 0) {
error("Error building trees");
error("Error building trees; the index is unmerged?");
return 0;
}

Expand Down
4 changes: 1 addition & 3 deletions builtin-for-each-ref.c
Expand Up @@ -320,9 +320,7 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un

static const char *copy_line(const char *buf)
{
const char *eol = strchr(buf, '\n');
if (!eol) // simulate strchrnul()
eol = buf + strlen(buf);
const char *eol = strchrnul(buf, '\n');
return xmemdupz(buf, eol - buf);
}

Expand Down
23 changes: 15 additions & 8 deletions builtin-remote.c
Expand Up @@ -650,10 +650,13 @@ static int get_one_entry(struct remote *remote, void *priv)
{
struct string_list *list = priv;

string_list_append(remote->name, list)->util = remote->url_nr ?
(void *)remote->url[0] : NULL;
if (remote->url_nr > 1)
warning("Remote %s has more than one URL", remote->name);
if (remote->url_nr > 0) {
int i;

for (i = 0; i < remote->url_nr; i++)
string_list_append(remote->name, list)->util = (void *)remote->url[i];
} else
string_list_append(remote->name, list)->util = NULL;

return 0;
}
Expand All @@ -669,10 +672,14 @@ static int show_all(void)
sort_string_list(&list);
for (i = 0; i < list.nr; i++) {
struct string_list_item *item = list.items + i;
printf("%s%s%s\n", item->string,
verbose ? "\t" : "",
verbose && item->util ?
(const char *)item->util : "");
if (verbose)
printf("%s\t%s\n", item->string,
item->util ? (const char *)item->util : "");
else {
if (i && !strcmp((item - 1)->string, item->string))
continue;
printf("%s\n", item->string);
}
}
}
return result;
Expand Down
22 changes: 1 addition & 21 deletions builtin-rm.c
Expand Up @@ -29,26 +29,6 @@ static void add_list(const char *name)
list.name[list.nr++] = name;
}

static int remove_file(const char *name)
{
int ret;
char *slash;

ret = unlink(name);
if (ret && errno == ENOENT)
/* The user has removed it from the filesystem by hand */
ret = errno = 0;

if (!ret && (slash = strrchr(name, '/'))) {
char *n = xstrdup(name);
do {
n[slash - name] = 0;
name = n;
} while (!rmdir(name) && (slash = strrchr(name, '/')));
}
return ret;
}

static int check_local_mod(unsigned char *head, int index_only)
{
/* items in list are already sorted in the cache order,
Expand Down Expand Up @@ -239,7 +219,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
int removed = 0;
for (i = 0; i < list.nr; i++) {
const char *path = list.name[i];
if (!remove_file(path)) {
if (!remove_path(path)) {
removed = 1;
continue;
}
Expand Down
106 changes: 60 additions & 46 deletions diff.c
Expand Up @@ -96,32 +96,37 @@ static int parse_lldiff_command(const char *var, const char *ep, const char *val
* to define a customized regexp to find the beginning of a function to
* be used for hunk header lines of "diff -p" style output.
*/
static struct funcname_pattern {
struct funcname_pattern_entry {
char *name;
char *pattern;
struct funcname_pattern *next;
int cflags;
};
static struct funcname_pattern_list {
struct funcname_pattern_list *next;
struct funcname_pattern_entry e;
} *funcname_pattern_list;

static int parse_funcname_pattern(const char *var, const char *ep, const char *value)
static int parse_funcname_pattern(const char *var, const char *ep, const char *value, int cflags)
{
const char *name;
int namelen;
struct funcname_pattern *pp;
struct funcname_pattern_list *pp;

name = var + 5; /* "diff." */
namelen = ep - name;

for (pp = funcname_pattern_list; pp; pp = pp->next)
if (!strncmp(pp->name, name, namelen) && !pp->name[namelen])
if (!strncmp(pp->e.name, name, namelen) && !pp->e.name[namelen])
break;
if (!pp) {
pp = xcalloc(1, sizeof(*pp));
pp->name = xmemdupz(name, namelen);
pp->e.name = xmemdupz(name, namelen);
pp->next = funcname_pattern_list;
funcname_pattern_list = pp;
}
free(pp->pattern);
pp->pattern = xstrdup(value);
free(pp->e.pattern);
pp->e.pattern = xstrdup(value);
pp->e.cflags = cflags;
return 0;
}

Expand Down Expand Up @@ -194,7 +199,13 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
if (!strcmp(ep, ".funcname")) {
if (!value)
return config_error_nonbool(var);
return parse_funcname_pattern(var, ep, value);
return parse_funcname_pattern(var, ep, value,
0);
} else if (!strcmp(ep, ".xfuncname")) {
if (!value)
return config_error_nonbool(var);
return parse_funcname_pattern(var, ep, value,
REG_EXTENDED);
}
}
}
Expand Down Expand Up @@ -1400,42 +1411,45 @@ int diff_filespec_is_binary(struct diff_filespec *one)
return one->is_binary;
}

static const char *funcname_pattern(const char *ident)
static const struct funcname_pattern_entry *funcname_pattern(const char *ident)
{
struct funcname_pattern *pp;
struct funcname_pattern_list *pp;

for (pp = funcname_pattern_list; pp; pp = pp->next)
if (!strcmp(ident, pp->name))
return pp->pattern;
if (!strcmp(ident, pp->e.name))
return &pp->e;
return NULL;
}

static struct builtin_funcname_pattern {
const char *name;
const char *pattern;
} builtin_funcname_pattern[] = {
{ "bibtex", "\\(@[a-zA-Z]\\{1,\\}[ \t]*{\\{0,1\\}[ \t]*[^ \t\"@',\\#}{~%]*\\).*$" },
{ "html", "^\\s*\\(<[Hh][1-6]\\s.*>.*\\)$" },
{ "java", "!^[ ]*\\(catch\\|do\\|for\\|if\\|instanceof\\|"
"new\\|return\\|switch\\|throw\\|while\\)\n"
"^[ ]*\\(\\([ ]*"
"[A-Za-z_][A-Za-z_0-9]*\\)\\{2,\\}"
"[ ]*([^;]*\\)$" },
{ "pascal", "^\\(\\(procedure\\|function\\|constructor\\|"
"destructor\\|interface\\|implementation\\|"
"initialization\\|finalization\\)[ \t]*.*\\)$"
"\\|"
"^\\(.*=[ \t]*\\(class\\|record\\).*\\)$"
},
{ "php", "^[\t ]*\\(\\(function\\|class\\).*\\)" },
{ "python", "^\\s*\\(\\(class\\|def\\)\\s.*\\)$" },
{ "ruby", "^\\s*\\(\\(class\\|module\\|def\\)\\s.*\\)$" },
{ "tex", "^\\(\\\\\\(\\(sub\\)*section\\|chapter\\|part\\)\\*\\{0,1\\}{.*\\)$" },
static const struct funcname_pattern_entry builtin_funcname_pattern[] = {
{ "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
REG_EXTENDED },
{ "html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$", REG_EXTENDED },
{ "java",
"!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
"^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$",
REG_EXTENDED },
{ "pascal",
"^((procedure|function|constructor|destructor|interface|"
"implementation|initialization|finalization)[ \t]*.*)$"
"\n"
"^(.*=[ \t]*(class|record).*)$",
REG_EXTENDED },
{ "php", "^[\t ]*((function|class).*)", REG_EXTENDED },
{ "python", "^[ \t]*((class|def)[ \t].*)$", REG_EXTENDED },
{ "ruby", "^[ \t]*((class|module|def)[ \t].*)$",
REG_EXTENDED },
{ "bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
REG_EXTENDED },
{ "tex",
"^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
REG_EXTENDED },
};

static const char *diff_funcname_pattern(struct diff_filespec *one)
static const struct funcname_pattern_entry *diff_funcname_pattern(struct diff_filespec *one)
{
const char *ident, *pattern;
const char *ident;
const struct funcname_pattern_entry *pe;
int i;

diff_filespec_check_attr(one);
Expand All @@ -1450,17 +1464,17 @@ static const char *diff_funcname_pattern(struct diff_filespec *one)
return funcname_pattern("default");

/* Look up custom "funcname.$ident" regexp from config. */
pattern = funcname_pattern(ident);
if (pattern)
return pattern;
pe = funcname_pattern(ident);
if (pe)
return pe;

/*
* And define built-in fallback patterns here. Note that
* these can be overridden by the user's config settings.
*/
for (i = 0; i < ARRAY_SIZE(builtin_funcname_pattern); i++)
if (!strcmp(ident, builtin_funcname_pattern[i].name))
return builtin_funcname_pattern[i].pattern;
return &builtin_funcname_pattern[i];

return NULL;
}
Expand Down Expand Up @@ -1556,11 +1570,11 @@ static void builtin_diff(const char *name_a,
xdemitconf_t xecfg;
xdemitcb_t ecb;
struct emit_callback ecbdata;
const char *funcname_pattern;
const struct funcname_pattern_entry *pe;

funcname_pattern = diff_funcname_pattern(one);
if (!funcname_pattern)
funcname_pattern = diff_funcname_pattern(two);
pe = diff_funcname_pattern(one);
if (!pe)
pe = diff_funcname_pattern(two);

memset(&xecfg, 0, sizeof(xecfg));
memset(&ecbdata, 0, sizeof(ecbdata));
Expand All @@ -1572,8 +1586,8 @@ static void builtin_diff(const char *name_a,
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
xecfg.ctxlen = o->context;
xecfg.flags = XDL_EMIT_FUNCNAMES;
if (funcname_pattern)
xdiff_set_find_func(&xecfg, funcname_pattern);
if (pe)
xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
if (!diffopts)
;
else if (!prefixcmp(diffopts, "--unified="))
Expand Down
20 changes: 20 additions & 0 deletions dir.c
Expand Up @@ -831,3 +831,23 @@ void setup_standard_excludes(struct dir_struct *dir)
if (excludes_file && !access(excludes_file, R_OK))
add_excludes_from_file(dir, excludes_file);
}

int remove_path(const char *name)
{
char *slash;

if (unlink(name) && errno != ENOENT)
return -1;

slash = strrchr(name, '/');
if (slash) {
char *dirs = xstrdup(name);
slash = dirs + (slash - name);
do {
*slash = '\0';
} while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
free(dirs);
}
return 0;
}

3 changes: 3 additions & 0 deletions dir.h
Expand Up @@ -81,4 +81,7 @@ extern int is_inside_dir(const char *dir);
extern void setup_standard_excludes(struct dir_struct *dir);
extern int remove_dir_recursively(struct strbuf *path, int only_empty);

/* tries to remove the path with empty directories along it, ignores ENOENT */
extern int remove_path(const char *path);

#endif
2 changes: 1 addition & 1 deletion git-stash.sh
Expand Up @@ -161,7 +161,7 @@ show_stash () {
apply_stash () {
git update-index -q --refresh &&
git diff-files --quiet --ignore-submodules ||
die 'Cannot restore on top of a dirty state'
die 'Cannot apply to a dirty working tree, please stage your changes'

unstash_index=
case "$1" in
Expand Down
9 changes: 7 additions & 2 deletions git-submodule.sh
Expand Up @@ -194,7 +194,7 @@ cmd_add()
else

module_clone "$path" "$realrepo" || exit
(unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
(unset GIT_DIR; cd "$path" && git checkout -f -q ${branch:+-b "$branch" "origin/$branch"}) ||
die "Unable to checkout submodule '$path'"
fi

Expand Down Expand Up @@ -340,8 +340,13 @@ cmd_update()

if test "$subsha1" != "$sha1"
then
force=
if test -z "$subsha1"
then
force="-f"
fi
(unset GIT_DIR; cd "$path" && git-fetch &&
git-checkout -q "$sha1") ||
git-checkout $force -q "$sha1") ||
die "Unable to checkout '$sha1' in submodule path '$path'"

say "Submodule path '$path': checked out '$sha1'"
Expand Down

0 comments on commit 9800c0d

Please sign in to comment.