From 45d3fd8afae5830b9c1eb13045e689078eff4d5a Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 08:50:18 -0600 Subject: get user name and email using git config if available else use defaults --- src/fpm/cmd/new.f90 | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index a402432..c0ec3b8 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -575,6 +575,48 @@ character(len=:,kind=tfc),allocatable :: littlefile(:) call run('git init ' // settings%name) contains +function default_user(what) result(user) + implicit none + character(len=5), intent(in) :: what + character(len=:), allocatable :: user + if (what=="uname") then + user = "Jane Doe" + else + user = "jane.doe@example.com" + end if + return +end function default_user + +function git_user(what) result(user) + use fpm_filesystem, only : get_temp_filename, getline + implicit none + character(len=5), intent(in) :: what + character(len=:), allocatable :: user + character(len=:), allocatable :: temp_user, iomsg + integer :: stat, unit + allocate(temp_user, source=get_temp_filename()) + if (what=="uname") then + user = "git config --get user.name > " // temp_user + else + user = "git config --get user.email > " // temp_user + end if + call execute_command_line(user, exitstat=stat) + if (stat /= 0) then + user = default_user(what) + return + end if + open(file=temp_user, newunit=unit) + call getline(unit, user, stat, iomsg) + if (stat /= 0) then + user = default_user(what) + end if + close(unit, status="delete") + if (len(user)==0) then + user = default_user(what) + end if + return +end function git_user + subroutine create_verified_basic_manifest(filename) !> create a basic but verified default manifest file use fpm_toml, only : toml_table, toml_serializer, set_value @@ -603,9 +645,9 @@ character(len=*),intent(in) :: filename call set_value(table, "name", BNAME) call set_value(table, "version", "0.1.0") call set_value(table, "license", "license") - call set_value(table, "author", "Jane Doe") - call set_value(table, "maintainer", "jane.doe@example.com") - call set_value(table, "copyright", 'Copyright '//date(1:4)//', Jane Doe') + call set_value(table, "author", git_user("uname")) + call set_value(table, "maintainer", git_user("email")) + call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_user("uname")) ! continue building of manifest ! ... call new_package(package, table, error=error) -- cgit v1.2.3 From 1a3d5cfee58075979774dfae2a0e3f68ba6031b2 Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:05:38 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index c0ec3b8..f5a3ea4 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -577,7 +577,7 @@ contains function default_user(what) result(user) implicit none - character(len=5), intent(in) :: what + character(len=*), intent(in) :: what character(len=:), allocatable :: user if (what=="uname") then user = "Jane Doe" -- cgit v1.2.3 From 679bc5d7c25f8160375e02f53f04caa8ffcaacbf Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:05:45 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index f5a3ea4..414623b 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -590,7 +590,7 @@ end function default_user function git_user(what) result(user) use fpm_filesystem, only : get_temp_filename, getline implicit none - character(len=5), intent(in) :: what + character(len=*), intent(in) :: what character(len=:), allocatable :: user character(len=:), allocatable :: temp_user, iomsg integer :: stat, unit -- cgit v1.2.3 From be201c9b80b839d90e8c4caf266e621664ff3f85 Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:05:55 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index 414623b..bb21d73 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -576,7 +576,6 @@ character(len=:,kind=tfc),allocatable :: littlefile(:) contains function default_user(what) result(user) - implicit none character(len=*), intent(in) :: what character(len=:), allocatable :: user if (what=="uname") then -- cgit v1.2.3 From f2ceb1664d8b637e1390f94a73ce2f53797185f1 Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:06:00 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index bb21d73..d69f6f2 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -588,7 +588,6 @@ end function default_user function git_user(what) result(user) use fpm_filesystem, only : get_temp_filename, getline - implicit none character(len=*), intent(in) :: what character(len=:), allocatable :: user character(len=:), allocatable :: temp_user, iomsg -- cgit v1.2.3 From 5143d1968f8299673c6712dc0074f1e40db16361 Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:06:09 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index d69f6f2..b00aa06 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -592,7 +592,7 @@ function git_user(what) result(user) character(len=:), allocatable :: user character(len=:), allocatable :: temp_user, iomsg integer :: stat, unit - allocate(temp_user, source=get_temp_filename()) + temp_user = get_temp_filename() if (what=="uname") then user = "git config --get user.name > " // temp_user else -- cgit v1.2.3 From 18e5f5941728bbeedf2a3a604aa82d9aea8689f9 Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:06:13 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index b00aa06..6b04336 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -612,7 +612,6 @@ function git_user(what) result(user) if (len(user)==0) then user = default_user(what) end if - return end function git_user subroutine create_verified_basic_manifest(filename) -- cgit v1.2.3 From fe93a5b868a37b9bb80f8573e80124d5a20afb8b Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Thu, 20 Jan 2022 12:06:19 -0600 Subject: Update src/fpm/cmd/new.f90 Co-authored-by: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> --- src/fpm/cmd/new.f90 | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index 6b04336..c14ee3c 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -583,7 +583,6 @@ function default_user(what) result(user) else user = "jane.doe@example.com" end if - return end function default_user function git_user(what) result(user) -- cgit v1.2.3 From 21a9261862df80ed071e3b5c9f4432c524c2a48a Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Mon, 24 Jan 2022 06:28:01 -0600 Subject: fix git_metadata function to handle edge cases --- src/fpm/cmd/new.f90 | 90 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index c14ee3c..0d5bf30 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -56,9 +56,10 @@ module fpm_cmd_new use fpm_command_line, only : fpm_new_settings use fpm_environment, only : run, OS_LINUX, OS_MACOS, OS_WINDOWS use fpm_filesystem, only : join_path, exists, basename, mkdir, is_dir -use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite +use fpm_filesystem, only : fileopen, fileclose, filewrite, warnwrite, which use fpm_strings, only : join, to_fortran_name use fpm_error, only : fpm_stop + use,intrinsic :: iso_fortran_env, only : stderr=>error_unit implicit none private @@ -572,46 +573,57 @@ character(len=:,kind=tfc),allocatable :: littlefile(:) call create_verified_basic_manifest(join_path(settings%name, 'fpm.toml')) endif ! assumes git(1) is installed and in path - call run('git init ' // settings%name) + if(which('git').ne.'')then + call run('git init ' // settings%name) + endif contains -function default_user(what) result(user) - character(len=*), intent(in) :: what - character(len=:), allocatable :: user - if (what=="uname") then - user = "Jane Doe" - else - user = "jane.doe@example.com" - end if -end function default_user - -function git_user(what) result(user) +function git_metadata(what) result(returned) +!> get metadata values such as email address and git name from git(1) or return appropriate default use fpm_filesystem, only : get_temp_filename, getline - character(len=*), intent(in) :: what - character(len=:), allocatable :: user - character(len=:), allocatable :: temp_user, iomsg + character(len=*), intent(in) :: what !> keyword designating what git metatdata to query + character(len=:), allocatable :: returned !> value to return for requested keyword + character(len=:), allocatable :: command + character(len=:), allocatable :: temp_filename + character(len=:), allocatable :: iomsg + character(len=:), allocatable :: temp_value integer :: stat, unit - temp_user = get_temp_filename() - if (what=="uname") then - user = "git config --get user.name > " // temp_user - else - user = "git config --get user.email > " // temp_user - end if - call execute_command_line(user, exitstat=stat) - if (stat /= 0) then - user = default_user(what) + temp_filename = get_temp_filename() + ! for known keywords set default value for RETURNED and associated git(1) command for query + select case(what) + case('uname') + returned = "Jane Doe" + command = "git config --get user.name > " // temp_filename + case('email') + returned = "jane.doe@example.com" + command = "git config --get user.email > " // temp_filename + case default + write(stderr,'(*(g0,1x))')& + & ' *git_metadata* unknown metadata name ',trim(what) + returned='' return - end if - open(file=temp_user, newunit=unit) - call getline(unit, user, stat, iomsg) - if (stat /= 0) then - user = default_user(what) - end if - close(unit, status="delete") - if (len(user)==0) then - user = default_user(what) - end if -end function git_user + end select + ! Execute command if git(1) is in command path + if(which('git')/='')then + call run(command, exitstat=stat) + if (stat /= 0) then ! If command failed just return default + return + else ! Command did not return an error so try to read expected output file + open(file=temp_filename, newunit=unit,iostat=stat) + if(stat == 0)then + ! Read file into a scratch variable until status of doing so is checked + call getline(unit, temp_value, stat, iomsg) + if (stat == 0 .and. temp_value /= '') then + ! Return output from successful command + returned=temp_value + endif + endif + ! Always do the CLOSE because a failed open has unpredictable results. + ! Add IOSTAT so a failed close does not cause program to stop + close(unit, status="delete",iostat=stat) + endif + endif +end function git_metadata subroutine create_verified_basic_manifest(filename) !> create a basic but verified default manifest file @@ -641,9 +653,9 @@ character(len=*),intent(in) :: filename call set_value(table, "name", BNAME) call set_value(table, "version", "0.1.0") call set_value(table, "license", "license") - call set_value(table, "author", git_user("uname")) - call set_value(table, "maintainer", git_user("email")) - call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_user("uname")) + call set_value(table, "author", git_metadata('uname')) + call set_value(table, "maintainer", git_metadata('email')) + call set_value(table, "copyright", 'Copyright '//date(1:4)//', '//git_metadata('uname')) ! continue building of manifest ! ... call new_package(package, table, error=error) -- cgit v1.2.3 From cb5d94e93ae3da4ececa46150feb17fdd78e6d6d Mon Sep 17 00:00:00 2001 From: Andre Smit Date: Mon, 24 Jan 2022 06:36:33 -0600 Subject: remove inline documentation lines --- src/fpm/cmd/new.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/fpm/cmd/new.f90 b/src/fpm/cmd/new.f90 index 0d5bf30..99aa4c5 100644 --- a/src/fpm/cmd/new.f90 +++ b/src/fpm/cmd/new.f90 @@ -581,8 +581,8 @@ contains function git_metadata(what) result(returned) !> get metadata values such as email address and git name from git(1) or return appropriate default use fpm_filesystem, only : get_temp_filename, getline - character(len=*), intent(in) :: what !> keyword designating what git metatdata to query - character(len=:), allocatable :: returned !> value to return for requested keyword + character(len=*), intent(in) :: what ! keyword designating what git metatdata to query + character(len=:), allocatable :: returned ! value to return for requested keyword character(len=:), allocatable :: command character(len=:), allocatable :: temp_filename character(len=:), allocatable :: iomsg -- cgit v1.2.3