Skip to content

Commit

Permalink
Use "git check-ref-format" to validate patch names.
Browse files Browse the repository at this point in the history
The valid_patchname now lets "git check-ref-format" do its job instead
of trying (and failing) to implement the same rules.  See
git-check-ref-format(1) for a list of the rules.  Re-implement rules
added to "git check-ref-format" after Git 1.5.0, so that guilt rejects
the same names no matter what version of Git we are using (but
versions prior to 1.5.0 are not supported).

Refer to the git-check-ref-format(1) man page in the error messages
produced when valid_patchname indicates that the name is bad.

Added testcases that breaks most of the rules in that man-page.

Signed-off-by: Per Cederqvist <[email protected]>
Signed-off-by: Josef 'Jeff' Sipek <[email protected]>
  • Loading branch information
Per Cederqvist committed Jan 22, 2015
1 parent b39a943 commit fd95877
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 19 deletions.
44 changes: 40 additions & 4 deletions guilt
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,50 @@ fi
# usage: valid_patchname <patchname>
valid_patchname()
{
# Once we only support Git 1.7.8 and newer, the command below
# could be replaced with:
#
# git check-ref-format --allow-onelevel "$1"
#
# Instead, we arbitrarily prepend one level. The result
# should be the same, and this is portable to all existing
# versions of Git.
git check-ref-format a/"$1"
if [ $? -ne 0 ]; then
return 1
fi

# We want to reject all names that Git 2.0.0 rejects. In case
# we are running an older version, we explicitly check some
# cases that were added to Git after version 1.5.0. This code
# aims to support Git version 1.5.0 and newer.

# Git 1.7.6.4 and newer rejects the DEL character.
if [ `echo "$1"|tr -d '\177'` != "$1" ]; then
return 1
fi

# Git 1.7.8 and newer rejects refs that start or end with
# slash or contain multiple adjacent slashes.
case "$1" in
/*|./*|../*|*/./*|*/../*|*/.|*/..|*/|*\ *|*\ *)
/*|*/|*//*)
return 1;;
*:*)
esac

# Git 1.7.8 and newer rejects refname components that end in
# .lock.
case "$1" in
*.lock/*|*.lock)
return 1;;
*)
return 0;;
esac

# Git 1.8.5 and newer rejects refnames that are made up of the
# single character "@".
if [ "$1" = "@" ]; then
return 1
fi

return 0
}

get_branch()
Expand Down
2 changes: 1 addition & 1 deletion guilt-fork
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ else
fi

if ! valid_patchname "$newpatch"; then
die "The specified patch name contains invalid characters (:)."
die "The specified patch name is invalid according to git-check-ref-format(1)."
fi

if [ -e "$GUILT_DIR/$branch/$newpatch" ]; then
Expand Down
2 changes: 1 addition & 1 deletion guilt-import
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if [ -e "$GUILT_DIR/$branch/$newname" ]; then
fi

if ! valid_patchname "$newname"; then
die "The specified patch name contains invalid characters (:)."
die "The specified patch name is invalid according to git-check-ref-format(1)."
fi

# create any directories as needed
Expand Down
2 changes: 1 addition & 1 deletion guilt-new
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fi

if ! valid_patchname "$patch"; then
disp "Patchname is invalid." >&2
die "it cannot begin with '/', './' or '../', or contain /./, /../, or whitespace"
die "It must follow the rules in git-check-ref-format(1)."
fi

# create any directories as needed
Expand Down
Loading

0 comments on commit fd95877

Please sign in to comment.