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,37 @@ 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+ kci_msg ("git folder" )
58+ git_config_path = os .path .join (dot_git_folder , "config" )
59+ git_config = configparser .ConfigParser ()
60+ git_config .read (git_config_path )
61+ git_url = git_config .get ('remote "origin"' , "url" )
62+ # Get current branch name
63+ process = subprocess .Popen (
64+ ["git" , "branch" , "--show-current" ], stdout = subprocess .PIPE , text = True
65+ )
66+ branch_name , branch_error = process .communicate ()
67+ branch_name = branch_name .strip ()
68+
69+ # Get last commit hash
70+ last_commit_hash_path = os .path .join (
71+ dot_git_folder , "refs" , "heads" , branch_name
72+ )
73+ last_commit_hash = open (last_commit_hash_path , "r" ).read ()
74+
75+ return git_url , branch_name , last_commit_hash
76+ else :
77+ kci_err ("Not a GIT folder" )
78+ pass
79+
80+
4781def get_latest_commit (origin , giturl , branch ):
4882 trees = fetch_tree_fast (origin )
4983 for t in trees :
@@ -173,6 +207,11 @@ def cmd_builds(data, commit, download_logs, status):
173207 "--commit" ,
174208 help = "Commit or tag to get results for" ,
175209)
210+ @click .option (
211+ "--current-folder" ,
212+ is_flag = True ,
213+ help = "Get results for current folder git repository" ,
214+ )
176215@click .option (
177216 "--action" ,
178217 type = click .Choice (["summary" , "trees" , "builds" ], case_sensitive = True ),
@@ -195,23 +234,56 @@ def cmd_builds(data, commit, download_logs, status):
195234 default = "all" ,
196235)
197236@click .pass_context
198- def results (ctx , origin , giturl , branch , commit , action , download_logs , latest , status ):
237+ def results (
238+ ctx ,
239+ origin ,
240+ giturl ,
241+ branch ,
242+ commit ,
243+ current_folder ,
244+ action ,
245+ download_logs ,
246+ latest ,
247+ status ,
248+ ):
199249 if action == None or action == "summary" :
200- 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 ()
203- if latest :
204- commit = get_latest_commit (origin , giturl , branch )
250+ if not current_folder :
251+ if not giturl or not branch or not ((commit != None ) ^ latest ):
252+ kci_err (
253+ "--giturl AND --branch AND (--commit XOR --latest) are required"
254+ )
255+ raise click .Abort ()
256+ if latest :
257+ commit = get_latest_commit (origin , giturl , branch )
258+ if current_folder :
259+ git_url , branch_name , last_commit_hash = get_folder_repository ()
260+ kci_msg (git_url )
261+ kci_msg (branch_name )
262+ kci_msg (last_commit_hash )
263+ giturl = git_url
264+ branch = branch_name
265+ commit = last_commit_hash
205266 data = fetch_full_results (origin , giturl , branch , commit )
206267 cmd_summary (data )
207268 elif action == "trees" :
208269 cmd_list_trees (origin )
209270 elif action == "builds" :
210- 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 ()
213- if latest :
214- commit = get_latest_commit (origin , giturl , branch )
271+ if not current_folder :
272+ if not giturl or not branch or not ((commit != None ) ^ latest ):
273+ kci_err (
274+ "--giturl AND --branch AND (--commit XOR --latest) are required"
275+ )
276+ raise click .Abort ()
277+ if latest :
278+ commit = get_latest_commit (origin , giturl , branch )
279+ if current_folder :
280+ git_url , branch_name , last_commit_hash = get_folder_repository ()
281+ kci_msg (git_url )
282+ kci_msg (branch_name )
283+ kci_msg (last_commit_hash )
284+ giturl = git_url
285+ branch = branch_name
286+ commit = last_commit_hash
215287 data = fetch_full_results (origin , giturl , branch , commit )
216288 cmd_builds (data , commit , download_logs , status )
217289 else :
0 commit comments