From 35ffe7e82c96a81094ef7d42f18b50d0b8eb95fc Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Fri, 27 Jan 2012 18:26:40 +0400 Subject: [PATCH] Enhance git pre-commit user.name check for old Gits without %HOME% On Windows if %HOME% is not defined and Git is started via git.exe (not git.cmd), Git 1.7.0.2 and older doesn't define %HOME% properly. We used to set up %HOME% := %USERPROFILE%, but it is bad practice (IDEA-76150). Now just warn the user to let him update Git or set up %HOME% manually. --- .../checkin/GitCheckinHandlerFactory.java | 18 +++++++++++++++++- .../git4idea/config/GitVersionSpecialty.java | 11 +++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java index dcbe6948d172..733862975ef5 100644 --- a/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java +++ b/plugins/git4idea/src/git4idea/checkin/GitCheckinHandlerFactory.java @@ -30,6 +30,8 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.PairConsumer; import git4idea.GitVcs; import git4idea.config.GitConfigUtil; +import git4idea.config.GitVersion; +import git4idea.config.GitVersionSpecialty; import git4idea.i18n.GitBundle; import git4idea.repo.GitRepository; import git4idea.repo.GitRepositoryManager; @@ -82,10 +84,13 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory { private ReturnResult checkUserName() { Project project = myPanel.getProject(); + GitVcs vcs = GitVcs.getInstance(project); + assert vcs != null; + Collection notDefined = new ArrayList(); Map> defined = new HashMap>(); Collection allRoots = new ArrayList(Arrays.asList( - ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(GitVcs.getInstance(project)))); + ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(vcs))); Collection affectedRoots = myPanel.getRoots(); for (VirtualFile root : affectedRoots) { @@ -110,6 +115,17 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory { return ReturnResult.COMMIT; } + GitVersion version = vcs.getVersion(); + if (System.getenv("HOME") == null && GitVersionSpecialty.DOESNT_DEFINE_HOME_ENV_VAR.existsIn(version)) { + Messages.showErrorDialog(project, + "You are using Git " + version + " which doesn't define %HOME% environment variable properly.\n" + + "Consider updating Git to a newer version " + + "or define %HOME% to point to the place where the global .gitconfig is stored \n" + + "(it is usually %USERPROFILE% or %HOMEDRIVE%%HOMEPATH%).", + "HOME Variable Is Not Defined"); + return ReturnResult.CANCEL; + } + if (defined.isEmpty() && allRoots.size() > affectedRoots.size()) { allRoots.removeAll(affectedRoots); for (VirtualFile root : allRoots) { diff --git a/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java b/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java index b59570d461fd..cc3d67b84809 100644 --- a/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java +++ b/plugins/git4idea/src/git4idea/config/GitVersionSpecialty.java @@ -15,6 +15,7 @@ */ package git4idea.config; +import com.intellij.openapi.util.SystemInfo; import org.jetbrains.annotations.NotNull; /** @@ -74,10 +75,16 @@ public enum GitVersionSpecialty { */ KNOWS_STATUS_PORCELAIN { @Override - public boolean existsIn(@NotNull GitVersion - version) { + public boolean existsIn(@NotNull GitVersion version) { return version.isLaterOrEqual(new GitVersion(1, 7, 0, 0)); } + }, + + DOESNT_DEFINE_HOME_ENV_VAR { + @Override + public boolean existsIn(@NotNull GitVersion version) { + return SystemInfo.isWindows && version.isOlderOrEqual(new GitVersion(1, 7, 0, 2)); + } }; public abstract boolean existsIn(@NotNull GitVersion version);