From e23c0e9493273fb69cf95f46e584c1c1b72f5fc9 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Sun, 11 Mar 2018 10:32:51 -0700 Subject: [PATCH] Util.pm: add read_HEAD_ref function The read_HEAD_symref function returns undef for both a detached HEAD and for an invalid or missing HEAD. Sometimes it's necessary to deal with a detached HEAD. Rework read_HEAD_symref into read_HEAD_ref so that it will also return a valid detached HEAD (as an all lowercased hex string). Reimplement a new read_HEAD_symref in terms of read_HEAD_ref to retain the old semantics for all existing callers. Additionally, incorporate some of the basic checks for invalid ref name characters mentioned in `git help check-ref-format` to provide some extra validation. Signed-off-by: Kyle J. McKay --- Girocco/Util.pm | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Girocco/Util.pm b/Girocco/Util.pm index 4acb1e1..166c0bd 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -23,7 +23,7 @@ BEGIN { noFatalsToBrowser calc_redeltathreshold clean_email_multi read_HEAD_symref read_config_file read_config_file_hash is_git_dir git_bool util_path - is_shellish); + is_shellish read_HEAD_ref); } my $encoder; @@ -1013,12 +1013,13 @@ sub noFatalsToBrowser { } # mimics Git's symref reading but only for HEAD -# returns undef on failure or if HEAD is not a symbolic ref -sub read_HEAD_symref { +# returns undef on failure otherwise an string that is +# either an all-hex (lowercase) value or starts with "refs/" +sub read_HEAD_ref { my $headpath = $_[0] . "/HEAD"; if (-l $headpath) { my $rl = readlink($headpath); - return defined($rl) && $rl =~ m,^refs/., ? $rl : undef; + return defined($rl) && $rl =~ m,^refs/[^\x00-\x1f \x7f~^:\\*?[]+$, ? $rl : undef; } open my $fd, '<', $headpath or return undef; my $hv; @@ -1029,7 +1030,16 @@ sub read_HEAD_symref { close $fd; defined($hv) or return undef; chomp $hv; - return $hv =~ m,^ref:\s*(refs/.+)$, ? $1 : undef; + $hv =~ m,^ref:\s*(refs/[^\x00-\x1f \x7f~^:\\*?[]+)$, and return $1; + $hv =~ m/^[0-9a-fA-F]{40,}$/ and return lc($hv); + return undef; +} + +# same as read_HEAD_ref but returns undef +# unless the result starts with "refs/" +sub read_HEAD_symref { + my $hv = read_HEAD_ref; + return defined($hv) && $hv =~ m,^refs/., ? $hv : undef; } my $cf_unesc; -- 2.11.4.GIT