From 4e94db3d78019f6341afdaef031a26d2e9439727 Mon Sep 17 00:00:00 2001 From: Anastasis Andronidis Date: Tue, 4 Sep 2012 16:12:39 +0300 Subject: [PATCH 1/5] Fixed a parsing HEAD problem, when ansible is checked out as a submodule Fixed a parsing HEAD problem, when ansible is checked out as a submodule in git --- lib/ansible/utils.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index ca0f1b455db..2f2f63196bd 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -324,6 +324,11 @@ def _gitinfo(): result = None repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git') if os.path.exists(repo_path): + ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' + if os.path.isfile(repo_path): + central_gitdir = yaml.load(open(repo_path))['gitdir'].split('.git')[0] + ''' There is a posibility the .git file to have an absolute path. ''' + repo_path = os.path.join(os.path.relpath(central_gitdir), '.git') f = open(os.path.join(repo_path, "HEAD")) branch = f.readline().split('/')[-1].rstrip("\n") f.close() From 6c8171f6f9ee1925c0462c3afc13e006ffd6d368 Mon Sep 17 00:00:00 2001 From: Anastasis Andronidis Date: Tue, 4 Sep 2012 16:28:57 +0300 Subject: [PATCH 2/5] Appended repo_path --- lib/ansible/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 2f2f63196bd..4a9c046248a 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -326,9 +326,10 @@ def _gitinfo(): if os.path.exists(repo_path): ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' if os.path.isfile(repo_path): + repo_path = repo_path.split('.git')[0] central_gitdir = yaml.load(open(repo_path))['gitdir'].split('.git')[0] ''' There is a posibility the .git file to have an absolute path. ''' - repo_path = os.path.join(os.path.relpath(central_gitdir), '.git') + repo_path = os.path.join(repo_path, os.path.relpath(central_gitdir), '.git') f = open(os.path.join(repo_path, "HEAD")) branch = f.readline().split('/')[-1].rstrip("\n") f.close() From 44a28838db633dc9275b3351656385f99e73ed00 Mon Sep 17 00:00:00 2001 From: Anastasis Andronidis Date: Tue, 4 Sep 2012 17:05:00 +0300 Subject: [PATCH 3/5] Fault handling for YAML file and gitdir value. --- lib/ansible/utils.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 4a9c046248a..5c6b4eba94d 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -323,13 +323,19 @@ def _gitinfo(): ''' returns a string containing git branch, commit id and commit date ''' result = None repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git') + ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' if os.path.exists(repo_path): ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' if os.path.isfile(repo_path): - repo_path = repo_path.split('.git')[0] - central_gitdir = yaml.load(open(repo_path))['gitdir'].split('.git')[0] - ''' There is a posibility the .git file to have an absolute path. ''' - repo_path = os.path.join(repo_path, os.path.relpath(central_gitdir), '.git') + try: + central_gitdir = yaml.load(open(repo_path)).get('gitdir').split('.git')[0] + repo_path = repo_path.split('.git')[0] + ''' There is a posibility the .git file to have an absolute path. ''' + repo_path = os.path.join(repo_path, os.path.relpath(central_gitdir), '.git') + except IOError: + exit("Could not load .git file.") + except AttributeError: + exit("There is no gitdir attribute in .git file.") f = open(os.path.join(repo_path, "HEAD")) branch = f.readline().split('/')[-1].rstrip("\n") f.close() From 63b4f8dd80d0e2a8b3a5194563cae628620f5fa6 Mon Sep 17 00:00:00 2001 From: Anastasis Andronidis Date: Tue, 4 Sep 2012 17:12:16 +0300 Subject: [PATCH 4/5] small typo --- lib/ansible/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 5c6b4eba94d..3374510e13b 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -323,7 +323,7 @@ def _gitinfo(): ''' returns a string containing git branch, commit id and commit date ''' result = None repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git') - ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' + if os.path.exists(repo_path): ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' if os.path.isfile(repo_path): From ba707f56c1a89eb915d13b1590abfc2411924a9c Mon Sep 17 00:00:00 2001 From: Anastasis Andronidis Date: Wed, 5 Sep 2012 12:40:14 +0300 Subject: [PATCH 5/5] except now returns a 'n/a' version. Some changes in comments --- lib/ansible/utils.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 3374510e13b..c2bbaaed47c 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -325,17 +325,15 @@ def _gitinfo(): repo_path = os.path.join(os.path.dirname(__file__), '..', '..', '.git') if os.path.exists(repo_path): - ''' Check if the .git is a file. If it is a file, it means that we are in a submodule structure. ''' + # Check if the .git is a file. If it is a file, it means that we are in a submodule structure. if os.path.isfile(repo_path): try: central_gitdir = yaml.load(open(repo_path)).get('gitdir').split('.git')[0] repo_path = repo_path.split('.git')[0] - ''' There is a posibility the .git file to have an absolute path. ''' + # There is a posibility the .git file to have an absolute path. repo_path = os.path.join(repo_path, os.path.relpath(central_gitdir), '.git') - except IOError: - exit("Could not load .git file.") - except AttributeError: - exit("There is no gitdir attribute in .git file.") + except (IOError, AttributeError): + return 'n/a' f = open(os.path.join(repo_path, "HEAD")) branch = f.readline().split('/')[-1].rstrip("\n") f.close()