Home | History | Annotate | Download | only in tools
      1 # Copyright 2013 ARM Limited
      2 # All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without
      5 # modification, are permitted provided that the following conditions are met:
      6 #
      7 #   * Redistributions of source code must retain the above copyright notice,
      8 #     this list of conditions and the following disclaimer.
      9 #   * Redistributions in binary form must reproduce the above copyright notice,
     10 #     this list of conditions and the following disclaimer in the documentation
     11 #     and/or other materials provided with the distribution.
     12 #   * Neither the name of ARM Limited nor the names of its contributors may be
     13 #     used to endorse or promote products derived from this software without
     14 #     specific prior written permission.
     15 #
     16 # THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND ANY
     17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 # DISCLAIMED. IN NO EVENT SHALL ARM LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT,
     20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     21 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
     22 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     23 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     24 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
     25 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 
     27 import re
     28 import util
     29 import os.path
     30 
     31 def is_git_repository_root():
     32   return os.path.isdir('.git')
     33 
     34 def get_current_branch():
     35   status, branches = util.getstatusoutput('git branch')
     36   if status != 0: util.abort('Failed to run git branch.')
     37   match = re.search("^\* (.*)$", branches, re.MULTILINE)
     38   if not match: util.abort('Failed to find the current branch.')
     39 
     40   branch = match.group(1);
     41 
     42   # If we are not on a named branch, return the hash of the HEAD commit.
     43   # This can occur (for example) if a specific revision is checked out by
     44   # commit hash, or during a rebase.
     45   if branch == '(no branch)':
     46     status, commit = util.getstatusoutput('git log -1 --pretty=format:"%H"')
     47     if status != 0: util.abort('Failed to run git log.')
     48     match = re.search('^[0-9a-fA-F]{40}$', commit, re.MULTILINE)
     49     if not match: util.abort('Failed to find the current revision.')
     50     branch = match.group(0)
     51 
     52   return branch
     53 
     54 
     55 def get_tracked_files():
     56   command = 'git ls-tree '
     57   branch = get_current_branch()
     58   options = ' -r --full-tree --name-only'
     59 
     60   status, tracked = util.getstatusoutput(command + branch + options)
     61   if status != 0: util.abort('Failed to list tracked files.')
     62 
     63   return tracked
     64 
     65 
     66 # Get untracked files in src/, test/, and tools/.
     67 def get_untracked_files():
     68   status, output = util.getstatusoutput('git status -s')
     69   if status != 0: util.abort('Failed to get git status.')
     70 
     71   untracked_regexp = re.compile('\?\?.*(src/|test/|tools/).*(.cc$|.h$)')
     72   files_in_watched_folder = lambda n: untracked_regexp.search(n) != None
     73   untracked_files = filter(files_in_watched_folder, output.split('\n'))
     74 
     75   return untracked_files
     76