Skip to content

Commit

Permalink
Init git user info if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
agateau committed Jan 11, 2016
1 parent 0a56c12 commit 0fadd50
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
31 changes: 31 additions & 0 deletions yokadi/sync/gitvcsimpl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import subprocess


Expand All @@ -14,6 +15,7 @@ def isValidVcsDir(self):

def init(self):
self._run("init")
self._ensureUserInfoIsSet()

def isWorkTreeClean(self):
return len(list(self.getStatus())) == 0
Expand All @@ -28,6 +30,7 @@ def clone(self, remoteUrl):
parentDir = os.path.dirname(self._srcDir)
cloneDir = os.path.basename(self._srcDir)
self._run("clone", "--quiet", remoteUrl, cloneDir, cwd=parentDir)
self._ensureUserInfoIsSet()

def pull(self):
self._run("fetch", "--quiet")
Expand Down Expand Up @@ -72,6 +75,34 @@ def _run(self, *args, **kwargs):
cmd.extend(args)
return subprocess.check_output(cmd)

def _ensureUserInfoIsSet(self):
username = self._getConfig("user", "name")
email = self._getConfig("user", "email")
if username and email:
return

hostname = platform.node()
if not hostname:
hostname = "example.com"
if not username:
username = os.environ.get("USER", "user")
self._setConfig("user", "name", username)
if not email:
email = username + "@" + hostname
self._setConfig("user", "email", email)

def _getConfig(self, section, key):
try:
return self._run("config", section + "." + key)
except subprocess.CalledProcessError as exc:
if exc.returncode == 1:
# Key does not exist
return None
raise exc

def _setConfig(self, section, key, value):
self._run("config", section + "." + key, value)

def getStatus(self):
output = self._run("status", "--porcelain")
for line in output.splitlines():
Expand Down
12 changes: 11 additions & 1 deletion yokadi/tests/gitvcsimpltestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def testClone(self):
self.assertTrue(os.path.exists(fooPath))

def testCloneNoGitUserInfo(self):
self._deleteGitConfig()
with TemporaryDirectory() as tmpDir:
remoteRepoDir = join(tmpDir, "remote")
createGitRepository(remoteRepoDir)
Expand All @@ -172,6 +171,17 @@ def testCloneNoGitUserInfo(self):
impl.setDir(remoteRepoDir)
impl.commitAll()

self._deleteGitConfig()

repoDir = join(tmpDir, "repo")
impl = GitVcsImpl()
impl.setDir(repoDir)
impl.clone(remoteRepoDir)

touch(repoDir, "bar")
impl.commitAll()


def testPull(self):
with TemporaryDirectory() as tmpDir:
remoteRepoDir = join(tmpDir, "remote")
Expand Down

0 comments on commit 0fadd50

Please sign in to comment.