patman: Deal with git safe-directory warning

When running tests where the .git directory is not owned by the current
user, various warnings are produced and the tests fail. This happens in
CI.

For patman itself, modify the gitutil.get_top_level() function to return
None in this case. Ensure that the warning is not shown, since it creates
about 1000 lines of output.

For checkpatch, the same warning is produced even though --no-tree is
given. Suppress that as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/patman/checkpatch.py b/tools/patman/checkpatch.py
index 5df06b1..f9204a9 100644
--- a/tools/patman/checkpatch.py
+++ b/tools/patman/checkpatch.py
@@ -22,7 +22,7 @@
 
 
 def find_check_patch():
-    top_level = gitutil.get_top_level()
+    top_level = gitutil.get_top_level() or ''
     try_list = [
         os.getcwd(),
         os.path.join(os.getcwd(), '..', '..'),
@@ -219,8 +219,9 @@
         args.append('--no-tree')
     if show_types:
         args.append('--show-types')
-    output = command.output(*args, os.path.join(cwd or '', fname),
-                            raise_on_error=False)
+    output = command.output(
+        *args, os.path.join(cwd or '', fname), raise_on_error=False,
+        capture_stderr=not use_tree)
 
     return check_patch_parse(output, verbose)
 
diff --git a/tools/patman/cmdline.py b/tools/patman/cmdline.py
index 0ae92f8..108fa52 100644
--- a/tools/patman/cmdline.py
+++ b/tools/patman/cmdline.py
@@ -44,11 +44,15 @@
         '-m', '--no-maintainers', action='store_false',
         dest='add_maintainers', default=True,
         help="Don't cc the file maintainers automatically")
+    default_arg = None
+    top_level = gitutil.get_top_level()
+    if top_level:
+        default_arg = os.path.join(top_level, 'scripts',
+                                   'get_maintainer.pl') + ' --norolestats'
     par.add_argument(
         '--get-maintainer-script', dest='get_maintainer_script', type=str,
         action='store',
-        default=os.path.join(gitutil.get_top_level(), 'scripts',
-                             'get_maintainer.pl') + ' --norolestats',
+        default=default_arg,
         help='File name of the get_maintainer.pl (or compatible) script.')
     par.add_argument(
         '-r', '--in-reply-to', type=str, action='store',
diff --git a/tools/patman/get_maintainer.py b/tools/patman/get_maintainer.py
index 200ee96..1c8fa72 100644
--- a/tools/patman/get_maintainer.py
+++ b/tools/patman/get_maintainer.py
@@ -21,7 +21,7 @@
     if get_maintainer:
         return get_maintainer
 
-    git_relative_script = os.path.join(gitutil.get_top_level(),
+    git_relative_script = os.path.join(gitutil.get_top_level() or '',
                                        script_file_name)
     if os.path.exists(git_relative_script):
         return git_relative_script
@@ -46,11 +46,14 @@
     """
     # Expand `script_file_name` into a file name and its arguments, if
     # any.
-    cmd_args = shlex.split(script_file_name)
-    file_name = cmd_args[0]
-    arguments = cmd_args[1:]
+    get_maintainer = None
+    arguments = None
+    if script_file_name:
+        cmd_args = shlex.split(script_file_name)
+        file_name = cmd_args[0]
+        arguments = cmd_args[1:]
 
-    get_maintainer = find_get_maintainer(file_name)
+        get_maintainer = find_get_maintainer(file_name)
     if not get_maintainer:
         if verbose:
             print("WARNING: Couldn't find get_maintainer.pl")
diff --git a/tools/patman/project.py b/tools/patman/project.py
index d6143a6..e633401 100644
--- a/tools/patman/project.py
+++ b/tools/patman/project.py
@@ -18,7 +18,8 @@
     """
     top_level = gitutil.get_top_level()
 
-    if os.path.exists(os.path.join(top_level, "include", "u-boot")):
+    if (not top_level or
+            os.path.exists(os.path.join(top_level, "include", "u-boot"))):
         return "u-boot"
     elif os.path.exists(os.path.join(top_level, "kernel")):
         return "linux"
diff --git a/tools/patman/settings.py b/tools/patman/settings.py
index 7a0866c..def932d 100644
--- a/tools/patman/settings.py
+++ b/tools/patman/settings.py
@@ -364,7 +364,8 @@
 
     if config_fname is None:
         config_fname = '%s/.patman' % os.getenv('HOME')
-    git_local_config_fname = os.path.join(gitutil.get_top_level(), '.patman')
+    git_local_config_fname = os.path.join(gitutil.get_top_level() or '',
+                                          '.patman')
 
     has_config = False
     has_git_local_config = False