[Svnmerge] [PATCH] Indented log patch modified v2

Alan Barrett apb at cequrux.com
Wed Mar 1 13:44:39 PST 2006


On Wed, 01 Mar 2006, Raman Gupta wrote:
> >> +def prefix_lines(prefix, lines):
> >> +  """Given a string representing lines of text,
> >> +  insert the specified prefix at the begining of each line,
> >> +  and return the result."""
> >> +  
> >> +  if len(lines) > 0:
> >> +      return "\n".join([prefix + L for L in lines.split("\n")])
> >> +  else:
> >> +      return ""
> > 
> > This function assumes that the last line will not be terminated by "\n".
> > That assumption should be documented.
> 
> The function doesn't really assume anything -- each line that is passed
> in will be indented. That includes any zero length lines at the end.

We seem to have a disagreement about the definition of a "line".
In the string "one\ntwo\n", I say that there are two lines, each
terminated by "\n".  (You seem to think that there's a third
zero-length line.)  If you want to indent those two lines (per my
definition of lines) to get "  one\n  two\n", then you need to
remove a "\n" before calling prefix_lines, and add it back later,
like this: prefix_lines("  ","one\ntwo")+"\n".

> >> -        for match in LOG_SEPARATOR_RE.findall(message):
> >> -            sep = match[1]
> >> -            if len(sep) > len(longest_sep):
> >> -                longest_sep = sep
> >> +        if len(message) > 0:
> >> +            logs.append("\n" + prefix_lines(LOG_LINE_PREFIX, \
> >                            ^^^^
> >> +                                            rstrip(message, "\n")) + "\n")
> >> +            for match in LOG_SEPARATOR_RE.findall(message):
> >> +                sep = match[1]
> >> +                if len(sep) > len(longest_sep):
> >> +                    longest_sep = sep
> > 
> > I haven't run the code, but I think that the "\n" highlighted above will
> > put an unwanted blank line between the "........" and the first line of
> > the nested log message.
> 
> No, it doesn't. Try it out.

I tried it, and it does insert a blank line as I expected.

I append my attempt.  I made prefix_lines work with or without a "\n"
at the end of the last line (but if you really want a blank line at the
end, you can't omit the "\n" after it).

--apb (Alan Barrett)

--- svnmerge.py.orig	2006-03-01 23:10:08.000000000 +0200
+++ svnmerge.py	2006-03-01 23:38:47.000000000 +0200
@@ -66,6 +66,9 @@
 LOG_SEPARATOR_RE = re.compile('^((%s)+)' % re.escape(LOG_SEPARATOR),
                               re.MULTILINE)
 
+# Each line of the embedded log messages will be prefixed by LOG_LINE_PREFIX.
+LOG_LINE_PREFIX = 2 * ' '
+
 # We expect non-localized output from SVN
 os.environ["LC_MESSAGES"] = "C"
 
@@ -177,6 +180,19 @@
     if opts["verbose"]:
         print "%s: %s" % (NAME, s)
 
+def prefix_lines(prefix, lines):
+  """Given a string representing one or more lines of text, insert the
+  specified prefix at the beginning of each line, and return the result.
+  Each line of the result will be terminated by "\n", even if the "\n"
+  was missing from the last line of input."""
+  
+  lineslist = lines.split("\n")
+  if len(lineslist) > 1 and lineslist[-1] == "":
+      # remove unwanted extra element representing the nothingness
+      # after the last "\n" in the input string
+      pop(lineslist)
+  return "\n".join([prefix + L for L in lineslist]) + "\n"
+
 class LaunchError(Exception):
     """Signal a failure in execution of an external command. Parameters are the
     exit code of the process, the original command line, and the output of the
@@ -513,16 +529,19 @@
     svnmerge separator existing in the commit log messages and
     extending it by one more separator.  This results in a new commit
     log message that is clearer in describing merges that contain
-    other merges."""
+    other merges.  Trailing blank lines are stripped from embedded
+    commit messages."""
     logs = ['']
     longest_sep = ''
     for r in revnums.sorted():
         message = get_commit_log(opts["head_url"], r)
-        logs.append(message)
-        for match in LOG_SEPARATOR_RE.findall(message):
-            sep = match[1]
-            if len(sep) > len(longest_sep):
-                longest_sep = sep
+        if len(message) > 0:
+            logs.append(prefix_lines(LOG_LINE_PREFIX, \
+                                     rstrip(message, "\n")))
+            for match in LOG_SEPARATOR_RE.findall(message):
+                sep = match[1]
+                if len(sep) > len(longest_sep):
+                    longest_sep = sep
 
     longest_sep += LOG_SEPARATOR + "\n"
     logs.append('')



More information about the Svnmerge mailing list