@@ -56,9 +56,10 @@ module fpm_cmd_new
56
56
use fpm_command_line, only : fpm_new_settings
57
57
use fpm_environment, only : run, OS_LINUX, OS_MACOS, OS_WINDOWS
58
58
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
60
60
use fpm_strings, only : join, to_fortran_name
61
61
use fpm_error, only : fpm_stop
62
+
62
63
use ,intrinsic :: iso_fortran_env, only : stderr= >error_unit
63
64
implicit none
64
65
private
@@ -572,46 +573,57 @@ subroutine cmd_new(settings)
572
573
call create_verified_basic_manifest(join_path(settings% name, ' fpm.toml' ))
573
574
endif
574
575
! 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
576
579
contains
577
580
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
589
583
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
593
590
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
+
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= ' '
603
604
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
615
627
616
628
subroutine create_verified_basic_manifest (filename )
617
629
! > create a basic but verified default manifest file
@@ -641,9 +653,9 @@ subroutine create_verified_basic_manifest(filename)
641
653
call set_value(table, " name" , BNAME)
642
654
call set_value(table, " version" , " 0.1.0" )
643
655
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' ))
647
659
! continue building of manifest
648
660
! ...
649
661
call new_package(package, table, error= error)
0 commit comments