[Frugalware-darcs] vmexam: dg: implemented a darcs-like revert command

VMiklos vmiklos at frugalware.org
Wed Jun 13 22:31:19 CEST 2007


Darcsweb-Url: http://darcs.frugalware.org/darcsweb/darcsweb.cgi?r=vmexam;a=darcs_commitdiff;h=20070613202929-e2957-7de2b719ac734e58491a12eee4d60520cdc0fccd.gz;

[dg: implemented a darcs-like revert command
VMiklos <vmiklos at frugalware.org>**20070613202929] {
hunk ./python/dg.py 51
-def scan_dir(files=""):
-	ret = []
+def get_diff(files = ""):
hunk ./python/dg.py 55
+	return lines
+
+def scan_dir(files=""):
+	ret = []
+	lines = get_diff(files)
hunk ./python/dg.py 108
-def askhunks(hunks, preans=None):
+def askhunks(hunks, preans=None, action="record"):
hunk ./python/dg.py 116
-				ret = ask("Shall I record this change? (%d/%d)  [ynqad], or ? for help:" % (hunknum+1, total))
+				ret = ask("Shall I %s this change? (%d/%d)  [ynqad], or ? for help:" % (action, hunknum+1, total))
hunk ./python/dg.py 135
-					print """How to use record...
-y: record this patch
-n: don't record it
+					print """How to use %(action)s...
+y: %(action)s this patch
+n: don't %(action)s it
hunk ./python/dg.py 139
-d: record selected patches, skipping all the remaining patches
-a: record all the remaining patches
-q: cancel record
+d: %(action)s selected patches, skipping all the remaining patches
+a: %(action)s all the remaining patches
+q: cancel %(action)s
hunk ./python/dg.py 143
-h or ?: show this help"""
+h or ?: show this help""" % { 'action': action }
hunk ./python/dg.py 155
+def diff2filename(diff):
+	return re.sub(r".* a/([^ ]+) .*", r"\1", diff)
hunk ./python/dg.py 230
-				newlist.append(re.sub(r".* a/([^ ]+) .*", r"\1", lines[0]))
+				newlist.append(diff2filename(lines[0]))
hunk ./python/dg.py 238
+def revert_stale():
+	"""revert changes when only the modification date is changed. returns
+	True if we did something"""
+	ret = False
+	lines = get_diff()
+	prevdiff = False
+	linenum = 0
+	for i in lines:
+		if i.startswith("diff "):
+			if prevdiff:
+				os.system("git checkout %s" % diff2filename(lines[linenum-1]))
+				ret = True
+			prevdiff = True
+		else:
+			prevdiff = False
+		linenum += 1
+	if prevdiff:
+		os.system("git checkout %s" % diff2filename(lines[linenum-1]))
+		ret = True
+	return ret
+
+def revert(argv):
+	def usage(ret):
+		print """Usage: darcs-git revert [OPTION]... [FILE or DIRECTORY]...
+Revert to the committed version (you may loose your work).
+
+Options:
+  -a            --all                    answer yes to all hunks
+  -h            --help                   shows brief description of command and its arguments"""
+		sys.exit(ret)
+
+	class Options:
+		def __init__(self):
+			self.all = None
+			self.help = False
+			self.files = ""
+	options = Options()
+
+	try:
+		opts, args = getopt.getopt(argv, "ah", ["all", "help"])
+	except getopt.GetoptError:
+		usage(1)
+	optind = 0
+	for opt, arg in opts:
+		if opt in ("-a", "--all"):
+			options.all = True
+		elif opt in ("-h", "--help"):
+			options.help = True
+		optind += 1
+	if optind < len(argv):
+		options.files = " ".join(argv[optind:])
+	if options.help:
+		usage(0)
+	# check if we have anything to revert
+	lines = get_diff(options.files)
+	if not len(lines):
+		print "There are no changes to revert!"
+		sys.exit(0)
+	if options.all:
+		os.system("git checkout -f")
+		print "Finished reverting."
+		sys.exit(0)
+	status = scan_dir(options.files)
+	status.hunks = askhunks(status.hunks, action="revert")
+	if not status.hunks:
+		if revert_stale():
+			print "Finished reverting."
+		else:
+			print "Ok, if you don't want to revert anything, that's fine!"
+		sys.exit(0)
+	for i in status.hunks:
+		p = []
+		if i.picked == True:
+			p.append(i.text)
+		sock = os.popen("patch -p1 -R >/dev/null", "w")
+		sock.write("".join(p))
+		sock.close()
+	revert_stale()
+	print "Finished reverting."
+
hunk ./python/dg.py 324
+		elif sys.argv[1][:3] == "rev":
+			revert(argv[1:])
}


More information about the Frugalware-darcs mailing list