Skip to content

Commit 21a9261

Browse files
committed
fix git_metadata function to handle edge cases
1 parent fe93a5b commit 21a9261

File tree

1 file changed

+51
-39
lines changed

1 file changed

+51
-39
lines changed

src/fpm/cmd/new.f90

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ module fpm_cmd_new
5656
use fpm_command_line, only : fpm_new_settings
5757
use fpm_environment, only : run, OS_LINUX, OS_MACOS, OS_WINDOWS
5858
use fpm_filesystem, only : join_path, exists, basename, mkdir, is_dir
59-
use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite
59+
use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite, which
6060
use fpm_strings, only : join, to_fortran_name
6161
use fpm_error, only : fpm_stop
62+
6263
use,intrinsic :: iso_fortran_env, only : stderr=>error_unit
6364
implicit none
6465
private
@@ -572,46 +573,57 @@ subroutine cmd_new(settings)
572573
call create_verified_basic_manifest(join_path(settings%name, 'fpm.toml'))
573574
endif
574575
! assumes git(1) is installed and in path
575-
call run('git init ' // settings%name)
576+
if(which('git').ne.'')then
577+
call run('git init ' // settings%name)
578+
endif
576579
contains
577580

578-
function default_user(what) result(user)
579-
character(len=*), intent(in) :: what
580-
character(len=:), allocatable :: user
581-
if (what=="uname") then
582-
user = "Jane Doe"
583-
else
584-
585-
end if
586-
end function default_user
587-
588-
function git_user(what) result(user)
581+
function git_metadata(what) result(returned)
582+
!> get metadata values such as email address and git name from git(1) or return appropriate default
589583
use fpm_filesystem, only : get_temp_filename, getline
590-
character(len=*), intent(in) :: what
591-
character(len=:), allocatable :: user
592-
character(len=:), allocatable :: temp_user, iomsg
584+
character(len=*), intent(in) :: what !> keyword designating what git metatdata to query
585+
character(len=:), allocatable :: returned !> value to return for requested keyword
586+
character(len=:), allocatable :: command
587+
character(len=:), allocatable :: temp_filename
588+
character(len=:), allocatable :: iomsg
589+
character(len=:), allocatable :: temp_value
593590
integer :: stat, unit
594-
temp_user = get_temp_filename()
595-
if (what=="uname") then
596-
user = "git config --get user.name > " // temp_user
597-
else
598-
user = "git config --get user.email > " // temp_user
599-
end if
600-
call execute_command_line(user, exitstat=stat)
601-
if (stat /= 0) then
602-
user = default_user(what)
591+
temp_filename = get_temp_filename()
592+
! for known keywords set default value for RETURNED and associated git(1) command for query
593+
select case(what)
594+
case('uname')
595+
returned = "Jane Doe"
596+
command = "git config --get user.name > " // temp_filename
597+
case('email')
598+
returned = "[email protected]"
599+
command = "git config --get user.email > " // temp_filename
600+
case default
601+
write(stderr,'(*(g0,1x))')&
602+
& '<ERROR> *git_metadata* unknown metadata name ',trim(what)
603+
returned=''
603604
return
604-
end if
605-
open(file=temp_user, newunit=unit)
606-
call getline(unit, user, stat, iomsg)
607-
if (stat /= 0) then
608-
user = default_user(what)
609-
end if
610-
close(unit, status="delete")
611-
if (len(user)==0) then
612-
user = default_user(what)
613-
end if
614-
end function git_user
605+
end select
606+
! Execute command if git(1) is in command path
607+
if(which('git')/='')then
608+
call run(command, exitstat=stat)
609+
if (stat /= 0) then ! If command failed just return default
610+
return
611+
else ! Command did not return an error so try to read expected output file
612+
open(file=temp_filename, newunit=unit,iostat=stat)
613+
if(stat == 0)then
614+
! Read file into a scratch variable until status of doing so is checked
615+
call getline(unit, temp_value, stat, iomsg)
616+
if (stat == 0 .and. temp_value /= '') then
617+
! Return output from successful command
618+
returned=temp_value
619+
endif
620+
endif
621+
! Always do the CLOSE because a failed open has unpredictable results.
622+
! Add IOSTAT so a failed close does not cause program to stop
623+
close(unit, status="delete",iostat=stat)
624+
endif
625+
endif
626+
end function git_metadata
615627

616628
subroutine create_verified_basic_manifest(filename)
617629
!> create a basic but verified default manifest file
@@ -641,9 +653,9 @@ subroutine create_verified_basic_manifest(filename)
641653
call set_value(table, "name", BNAME)
642654
call set_value(table, "version", "0.1.0")
643655
call set_value(table, "license", "license")
644-
call set_value(table, "author", git_user("uname"))
645-
call set_value(table, "maintainer", git_user("email"))
646-
call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_user("uname"))
656+
call set_value(table, "author", git_metadata('uname'))
657+
call set_value(table, "maintainer", git_metadata('email'))
658+
call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_metadata('uname'))
647659
! continue building of manifest
648660
! ...
649661
call new_package(package, table, error=error)

0 commit comments

Comments
 (0)