Skip to content

Commit

Permalink
GitReadOnlyCommandUtil: fix how we set username and email when not av…
Browse files Browse the repository at this point in the history
…ialable in git config
  • Loading branch information
rpalcolea committed Dec 5, 2023
1 parent 7d369f6 commit 4690920
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ abstract class GitReadCommand implements ValueSource<String, GitCommandParameter
}
}

abstract class UsernameFromLog extends GitReadCommand {
@Override
String obtain() {
try {
return executeGitCommand( "--no-pager", "log","--format=format:%an", "-n", "1")
.replaceAll("\n", "").trim()
} catch (Exception e) {
return null
}
}
}

abstract class EmailFromLog extends GitReadCommand {
@Override
String obtain() {
try {
return executeGitCommand( "--no-pager", "log","--format=format:%ae", "-n", "1")
.replaceAll("\n", "").trim()
} catch (Exception e) {
return null
}
}
}
/**
* Returns current branch name
* ex. git rev-parse --abbrev-ref HEAD -> configuration-cache-support
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class GitReadOnlyCommandUtil implements Serializable {
private static final Logger logger = LoggerFactory.getLogger(GitReadOnlyCommandUtil)
private final ProviderFactory providers

private Provider usernameFromLogProvider
private Provider emailFromLogProvider
private Provider currentBranchProvider
private Provider isGitRepoProvider
private Provider describeTagsProvider
Expand All @@ -33,6 +35,12 @@ class GitReadOnlyCommandUtil implements Serializable {
*/
void configure(File gitRoot) {
this.rootDir = gitRoot
usernameFromLogProvider = providers.of(UsernameFromLog.class) {
it.parameters.rootDir.set(rootDir)
}
emailFromLogProvider = providers.of(EmailFromLog.class) {
it.parameters.rootDir.set(rootDir)
}
configureCommitterIfNecessary()
currentBranchProvider = providers.of(CurrentBranch.class) {
it.parameters.rootDir.set(rootDir)
Expand Down Expand Up @@ -72,11 +80,13 @@ class GitReadOnlyCommandUtil implements Serializable {
String globalEmail = getGitConfig('--global', 'user.email')
String localUsername = getGitConfig('--local', 'user.name')
String localEmail = getGitConfig('--local', 'user.email')
if(!globalUsername && !localUsername) {
setGitConfig("user.name", "\"\$(git --no-pager log --format=format:'%an' -n 1)\"")
String usernameFromLog = usernameFromLogProvider.isPresent() ? usernameFromLogProvider.get() : null
if(!globalUsername && !localUsername && usernameFromLog) {
setGitConfig("user.name", usernameFromLog)
}
if(!globalEmail && !localEmail) {
setGitConfig("user.email", "\"\$(git --no-pager log --format=format:'%ae' -n 1)\"")
String emailFromLog = emailFromLogProvider.isPresent() ? emailFromLogProvider.get() : null
if(!globalEmail && !localEmail && emailFromLog) {
setGitConfig("user.email", emailFromLog)
}
}

Expand All @@ -88,6 +98,14 @@ class GitReadOnlyCommandUtil implements Serializable {
}
}

String getUsernameFromLog() {
try {
return currentBranchProvider.get().toString().replaceAll("\n", "").trim()
} catch (Exception e) {
return null
}
}

Boolean hasCommit() {
try {
String describe = describeTagsProvider.get().toString()
Expand Down

0 comments on commit 4690920

Please sign in to comment.