|
| 1 | +!> Implementation for interacting with git repositories. |
| 2 | +module fpm_git |
| 3 | + implicit none |
| 4 | + |
| 5 | + public :: git_target_t |
| 6 | + public :: git_target_default, git_target_branch, git_target_tag, & |
| 7 | + & git_target_revision |
| 8 | + |
| 9 | + |
| 10 | + !> Possible git target |
| 11 | + type :: enum_descriptor |
| 12 | + |
| 13 | + !> Default target |
| 14 | + integer :: default = 200 |
| 15 | + |
| 16 | + !> Branch in git repository |
| 17 | + integer :: branch = 201 |
| 18 | + |
| 19 | + !> Tag in git repository |
| 20 | + integer :: tag = 202 |
| 21 | + |
| 22 | + !> Commit hash |
| 23 | + integer :: revision = 203 |
| 24 | + |
| 25 | + end type enum_descriptor |
| 26 | + |
| 27 | + !> Actual enumerator for descriptors |
| 28 | + type(enum_descriptor), parameter :: git_descriptor = enum_descriptor() |
| 29 | + |
| 30 | + |
| 31 | + !> Description of an git target |
| 32 | + type :: git_target_t |
| 33 | + private |
| 34 | + |
| 35 | + !> Kind of the git target |
| 36 | + integer :: descriptor = git_descriptor%default |
| 37 | + |
| 38 | + !> Target URL of the git repository |
| 39 | + character(len=:), allocatable :: url |
| 40 | + |
| 41 | + !> Additional descriptor of the git object |
| 42 | + character(len=:), allocatable :: object |
| 43 | + |
| 44 | + contains |
| 45 | + |
| 46 | + !> Show information on instance |
| 47 | + procedure :: info |
| 48 | + |
| 49 | + end type git_target_t |
| 50 | + |
| 51 | + |
| 52 | +contains |
| 53 | + |
| 54 | + |
| 55 | + !> Default target |
| 56 | + function git_target_default(url) result(self) |
| 57 | + |
| 58 | + !> Target URL of the git repository |
| 59 | + character(len=*), intent(in) :: url |
| 60 | + |
| 61 | + !> New git target |
| 62 | + type(git_target_t) :: self |
| 63 | + |
| 64 | + self%descriptor = git_descriptor%default |
| 65 | + self%url = url |
| 66 | + |
| 67 | + end function git_target_default |
| 68 | + |
| 69 | + |
| 70 | + !> Target a branch in the git repository |
| 71 | + function git_target_branch(url, branch) result(self) |
| 72 | + |
| 73 | + !> Target URL of the git repository |
| 74 | + character(len=*), intent(in) :: url |
| 75 | + |
| 76 | + !> Name of the branch of interest |
| 77 | + character(len=*), intent(in) :: branch |
| 78 | + |
| 79 | + !> New git target |
| 80 | + type(git_target_t) :: self |
| 81 | + |
| 82 | + self%descriptor = git_descriptor%branch |
| 83 | + self%url = url |
| 84 | + self%object = branch |
| 85 | + |
| 86 | + end function git_target_branch |
| 87 | + |
| 88 | + |
| 89 | + !> Target a specific git revision |
| 90 | + function git_target_revision(url, sha1) result(self) |
| 91 | + |
| 92 | + !> Target URL of the git repository |
| 93 | + character(len=*), intent(in) :: url |
| 94 | + |
| 95 | + !> Commit hash of interest |
| 96 | + character(len=*), intent(in) :: sha1 |
| 97 | + |
| 98 | + !> New git target |
| 99 | + type(git_target_t) :: self |
| 100 | + |
| 101 | + self%descriptor = git_descriptor%revision |
| 102 | + self%url = url |
| 103 | + self%object = sha1 |
| 104 | + |
| 105 | + end function git_target_revision |
| 106 | + |
| 107 | + |
| 108 | + !> Target a git tag |
| 109 | + function git_target_tag(url, tag) result(self) |
| 110 | + |
| 111 | + !> Target URL of the git repository |
| 112 | + character(len=*), intent(in) :: url |
| 113 | + |
| 114 | + !> Tag name of interest |
| 115 | + character(len=*), intent(in) :: tag |
| 116 | + |
| 117 | + !> New git target |
| 118 | + type(git_target_t) :: self |
| 119 | + |
| 120 | + self%descriptor = git_descriptor%tag |
| 121 | + self%url = url |
| 122 | + self%object = tag |
| 123 | + |
| 124 | + end function git_target_tag |
| 125 | + |
| 126 | + |
| 127 | + !> Show information on git target |
| 128 | + subroutine info(self, unit, verbosity) |
| 129 | + |
| 130 | + !> Instance of the git target |
| 131 | + class(git_target_t), intent(in) :: self |
| 132 | + |
| 133 | + !> Unit for IO |
| 134 | + integer, intent(in) :: unit |
| 135 | + |
| 136 | + !> Verbosity of the printout |
| 137 | + integer, intent(in), optional :: verbosity |
| 138 | + |
| 139 | + integer :: pr |
| 140 | + character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)' |
| 141 | + |
| 142 | + if (present(verbosity)) then |
| 143 | + pr = verbosity |
| 144 | + else |
| 145 | + pr = 1 |
| 146 | + end if |
| 147 | + |
| 148 | + if (pr < 1) return |
| 149 | + |
| 150 | + write(unit, fmt) "Git target" |
| 151 | + if (allocated(self%url)) then |
| 152 | + write(unit, fmt) "- URL", self%url |
| 153 | + end if |
| 154 | + if (allocated(self%object)) then |
| 155 | + select case(self%descriptor) |
| 156 | + case default |
| 157 | + write(unit, fmt) "- object", self%object |
| 158 | + case(git_descriptor%tag) |
| 159 | + write(unit, fmt) "- tag", self%object |
| 160 | + case(git_descriptor%branch) |
| 161 | + write(unit, fmt) "- branch", self%object |
| 162 | + case(git_descriptor%revision) |
| 163 | + write(unit, fmt) "- sha1", self%object |
| 164 | + end select |
| 165 | + end if |
| 166 | + |
| 167 | + end subroutine info |
| 168 | + |
| 169 | + |
| 170 | +end module fpm_git |
0 commit comments