11#!/usr/bin/env python3
22# -*- coding: utf-8 -*-
33
4+ import configparser
45import gzip
56import json
7+ import os
8+ import subprocess
69import urllib
710
811import click
@@ -44,6 +47,36 @@ def fetch_tree_fast(origin):
4447 return fetch_from_api ("tree-fast" , params )
4548
4649
50+ def get_folder_repository ():
51+ current_folder = os .getcwd ()
52+ dot_git_folder = os .path .join (current_folder , ".git" )
53+
54+ # Check if we are in a git repository
55+ if os .path .exists (dot_git_folder ):
56+ # Get remote origin url
57+ git_config_path = os .path .join (dot_git_folder , "config" )
58+ git_config = configparser .ConfigParser ()
59+ git_config .read (git_config_path )
60+ git_url = git_config .get ('remote "origin"' , "url" )
61+ # Get current branch name
62+ process = subprocess .Popen (
63+ ["git" , "branch" , "--show-current" ], stdout = subprocess .PIPE , text = True
64+ )
65+ branch_name , branch_error = process .communicate ()
66+ branch_name = branch_name .strip ()
67+
68+ # Get last commit hash
69+ last_commit_hash_path = os .path .join (
70+ dot_git_folder , "refs" , "heads" , branch_name
71+ )
72+ last_commit_hash = open (last_commit_hash_path , "r" ).read ()
73+
74+ return git_url , branch_name , last_commit_hash
75+ else :
76+ kci_err ("Not a GIT folder" )
77+ pass
78+
79+
4780def get_latest_commit (origin , giturl , branch ):
4881 trees = fetch_tree_fast (origin )
4982 for t in trees :
@@ -169,6 +202,10 @@ def cmd_builds(data, commit, download_logs, status):
169202 "--branch" ,
170203 help = "Branch to get results for" ,
171204)
205+ @click .option (
206+ "--git-folder" ,
207+ help = "Path of git repository folder" ,
208+ )
172209@click .option (
173210 "--commit" ,
174211 help = "Commit or tag to get results for" ,
@@ -195,11 +232,28 @@ def cmd_builds(data, commit, download_logs, status):
195232 default = "all" ,
196233)
197234@click .pass_context
198- def results (ctx , origin , giturl , branch , commit , action , download_logs , latest , status ):
235+ def results (
236+ ctx ,
237+ origin ,
238+ git_folder ,
239+ giturl ,
240+ branch ,
241+ commit ,
242+ action ,
243+ download_logs ,
244+ latest ,
245+ status ,
246+ ):
199247 if action == None or action == "summary" :
200248 if not giturl or not branch or not ((commit != None ) ^ latest ):
201- kci_err ("--giturl AND --branch AND (--commit XOR --latest) are required" )
202- raise click .Abort ()
249+ git_url , branch_name , last_commit_hash = get_folder_repository ()
250+ kci_msg ("Getting git repository informations for the current folder" )
251+ kci_msg (git_url )
252+ kci_msg (branch_name )
253+ kci_msg (last_commit_hash )
254+ giturl = git_url
255+ branch = branch_name
256+ commit = last_commit_hash
203257 if latest :
204258 commit = get_latest_commit (origin , giturl , branch )
205259 data = fetch_full_results (origin , giturl , branch , commit )
@@ -208,8 +262,14 @@ def results(ctx, origin, giturl, branch, commit, action, download_logs, latest,
208262 cmd_list_trees (origin )
209263 elif action == "builds" :
210264 if not giturl or not branch or not ((commit != None ) ^ latest ):
211- kci_err ("--giturl AND --branch AND (--commit XOR --latest) are required" )
212- raise click .Abort ()
265+ git_url , branch_name , last_commit_hash = get_folder_repository ()
266+ kci_msg ("Getting git repository informations for the current folder" )
267+ kci_msg (git_url )
268+ kci_msg (branch_name )
269+ kci_msg (last_commit_hash )
270+ giturl = git_url
271+ branch = branch_name
272+ commit = last_commit_hash
213273 if latest :
214274 commit = get_latest_commit (origin , giturl , branch )
215275 data = fetch_full_results (origin , giturl , branch , commit )
0 commit comments