From f8b3a20652ffdda7f3bf9a99fa56078be2095dc1 Mon Sep 17 00:00:00 2001
From: Adam Waldenberg <adam.waldenberg@ejwa.se>
Date: Mon, 7 May 2012 09:38:11 +0200
Subject: [PATCH] Added a new --checkout-missing option. Missing files are now
 pretty-printed.

If any missing files are detected during analysis, gitinspector reports them
in a list. If, on the other hand, the --checkout-missing option is supplied,
gitinspector will try to checkout any missing files instead.
---
 blame.py        | 18 +++++++++--------
 gitinspector.py | 11 ++++++++---
 help.py         |  1 +
 missing.py      | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 11 deletions(-)
 create mode 100644 missing.py

diff --git a/blame.py b/blame.py
index 6174a0f..df009e0 100644
--- a/blame.py
+++ b/blame.py
@@ -18,6 +18,7 @@
 # along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
 
 from changes import FileDiff
+import missing
 import re
 import sysrun
 import terminal
@@ -29,17 +30,18 @@ class Blame:
 
 		for i in ls_tree_r.readlines():
 			if FileDiff.is_valid_extension(i):
-				git_blame_r = sysrun.run(repo, "git blame -w {0} \"".format("-C -M" if hard else "") +
-				                               i.strip() + "\"")
+				if not missing.add(repo, i.strip()):
+					git_blame_r = sysrun.run(repo, "git blame -w {0} \"".format("-C -M" if hard else "") +
+					                               i.strip() + "\"")
 
-				for j in git_blame_r.readlines():
-					if Blame.is_blame_line(j):
-						author = Blame.get_author(j)
+					for j in git_blame_r.readlines():
+						if Blame.is_blame_line(j):
+							author = Blame.get_author(j)
 
-						if self.blames.get(author, None) == None:
-							self.blames[author] = 0
+							if self.blames.get(author, None) == None:
+								self.blames[author] = 0
 
-						self.blames[author] += 1
+							self.blames[author] += 1
 
 	@staticmethod
 	def is_blame_line(string):
diff --git a/gitinspector.py b/gitinspector.py
index b9ea046..dd863a4 100755
--- a/gitinspector.py
+++ b/gitinspector.py
@@ -23,6 +23,7 @@ import changes
 import extensions
 import getopt
 import help
+import missing
 import sys
 import terminal
 import timeline
@@ -47,6 +48,8 @@ class Runner:
 			if self.timeline == True:
 				timeline.output(changes.get(self.repo, self.hard), self.useweeks)
 
+			missing.output()
+
 			if self.list_file_types == True:
 				ex = extensions.get_located()
 				if (ex):
@@ -58,14 +61,16 @@ if __name__ == "__main__":
 	__run__ = Runner()
 
 	try:
-		__opts__, __args__ = getopt.gnu_getopt(sys.argv[1:], "f:hHlTw", ["file-types=", "hard", "help",
-		                                                     "list-file-types", "tda367", "timeline", "version"])
+		__opts__, __args__ = getopt.gnu_getopt(sys.argv[1:], "cf:hHlTw", ["checkout-missing", "file-types=", "hard",
+		                                                     "help", "list-file-types", "tda367", "timeline", "version"])
 	except getopt.error, msg:
 		print sys.argv[0], "\b:", msg
 		print "Try `", sys.argv[0], "--help' for more information."
 		sys.exit(2)
 	for o, a in __opts__:
-		if o in("-h", "--help"):
+		if o in("-c", "--checkout-missing"):
+			missing.set_checkout_missing(True)
+		elif o in("-h", "--help"):
 			help.output()
 			sys.exit(0)
 		elif o in("-f", "--file-types"):
diff --git a/help.py b/help.py
index d695af0..51db530 100644
--- a/help.py
+++ b/help.py
@@ -23,6 +23,7 @@ specified, the current directory is used. If multiple directories are
 given, information will be fetched from the last directory specified.
 
 Mandatory arguments to long options are mandatory for short options too.
+  -c, --checkout-missing       try to checkout any missing files
   -f, --file-types=EXTENSIONS  a comma separated list of file extensions to
                                  include when computing statistics. The
                                  default extensions used are:
diff --git a/missing.py b/missing.py
new file mode 100644
index 0000000..fc6d192
--- /dev/null
+++ b/missing.py
@@ -0,0 +1,51 @@
+# coding: utf-8
+#
+# Copyright © 2012 Ejwa Software. All rights reserved.
+#
+# This file is part of gitinspector.
+#
+# gitinspector is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# gitinspector is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import sysrun
+
+__checkout_missing__ = False
+__missing_files__ =  set()
+
+def add(repo, file_name):
+	previous_directory = os.getcwd()
+	os.chdir(repo)
+	exists = os.path.exists(file_name)
+	os.chdir(previous_directory)
+
+	if not exists:
+		if __checkout_missing__:
+			sysrun.run(repo, "git checkout \"" + file_name.strip() + "\"")
+		else:
+			__missing_files__.add(file_name)
+			return True
+	return False
+
+def set_checkout_missing(checkout):
+	global __checkout_missing__
+	__checkout_missing__ = checkout
+
+def output():
+	if __missing_files__:
+		print "\nThe following files were missing in the repository and were therefore not"
+		print "completely included in the statistical analysis. To include them, you can"
+		print "either checkout manually using git or use the -c option in gitinspector:"
+
+		for missing in __missing_files__:
+			print missing