zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH 1/2] vcs_info quilt: Factor out a helper function.  No functional change.
@ 2016-06-04 16:57 Daniel Shahaf
  2016-06-04 16:57 ` [PATCH 2/2] vcs_info quilt: Extract a patch subject, 2.0 Daniel Shahaf
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Shahaf @ 2016-06-04 16:57 UTC (permalink / raw)
  To: zsh-workers

---
 Functions/VCS_Info/VCS_INFO_quilt | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/Functions/VCS_Info/VCS_INFO_quilt b/Functions/VCS_Info/VCS_INFO_quilt
index c3c3d86..e31deb0 100644
--- a/Functions/VCS_Info/VCS_INFO_quilt
+++ b/Functions/VCS_Info/VCS_INFO_quilt
@@ -80,6 +80,23 @@ function VCS_INFO_quilt-dirfind() {
     return ${ret}
 }
 
+# This function takes as an argument a filename of a patch and sets $REPLY to
+# a single-line "subject", or unsets it if no subject could be extracted.
+function VCS_INFO_quilt-patch2subject() {
+    local line
+    if [[ -f "$1" ]] && read -r line < "$1"; then
+        if [[ $line != (#b)(---|Index:)* ]]; then
+            REPLY=$line
+        else
+            unset REPLY
+            return 0
+        fi
+    else
+        unset REPLY
+        return 1
+    fi
+}
+
 function VCS_INFO_quilt() {
     emulate -L zsh
     setopt extendedglob
@@ -147,27 +164,19 @@ function VCS_INFO_quilt() {
 
     if [[ -n $patches ]]; then
       () {
-        local i line
+        local i
         for ((i=1; i<=$#applied; i++)); do
-          if [[ -f "$patches/$applied[$i]" ]] &&
-             read -r line < "$patches/$applied[$i]" &&
-             [[ $line != (#b)(---|Index:)* ]] &&
-             true
-            ;
+          if VCS_INFO_quilt-patch2subject "$patches/$applied[$i]" && (( $+REPLY ))
           then
-            applied[$i]+=" $line"
+            applied[$i]+=" $REPLY"
           else
             applied[$i]+=" ?"
           fi
         done
         for ((i=1; i<=$#unapplied; i++)); do
-          if [[ -f "$patches/$unapplied[$i]" ]] &&
-             read -r line < "$patches/$unapplied[$i]" &&
-             [[ $line != (#b)(---|Index:)* ]] &&
-             true
-            ;
+          if VCS_INFO_quilt-patch2subject "$patches/$unapplied[$i]" && (( $+REPLY ))
           then
-            unapplied[$i]+=" $line"
+            unapplied[$i]+=" $REPLY"
           else
             unapplied[$i]+=" ?"
           fi


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] vcs_info quilt: Extract a patch subject, 2.0.
  2016-06-04 16:57 [PATCH 1/2] vcs_info quilt: Factor out a helper function. No functional change Daniel Shahaf
@ 2016-06-04 16:57 ` Daniel Shahaf
  0 siblings, 0 replies; 2+ messages in thread
From: Daniel Shahaf @ 2016-06-04 16:57 UTC (permalink / raw)
  To: zsh-workers

---
 Functions/VCS_Info/VCS_INFO_quilt | 42 +++++++++++++++++++++++++++++++++++----
 1 file changed, 38 insertions(+), 4 deletions(-)

diff --git a/Functions/VCS_Info/VCS_INFO_quilt b/Functions/VCS_Info/VCS_INFO_quilt
index e31deb0..009c9d4 100644
--- a/Functions/VCS_Info/VCS_INFO_quilt
+++ b/Functions/VCS_Info/VCS_INFO_quilt
@@ -83,15 +83,49 @@ function VCS_INFO_quilt-dirfind() {
 # This function takes as an argument a filename of a patch and sets $REPLY to
 # a single-line "subject", or unsets it if no subject could be extracted.
 function VCS_INFO_quilt-patch2subject() {
-    local line
-    if [[ -f "$1" ]] && read -r line < "$1"; then
-        if [[ $line != (#b)(---|Index:)* ]]; then
-            REPLY=$line
+    integer i
+    integer -r LIMIT=10
+    local -a lines
+    local needle
+    if [[ -f "$1" ]]; then
+        # Extract the first LIMIT lines, or up to the first empty line or the start of the unidiffs,
+        # whichever comes first.
+        while (( i++ < LIMIT )); do
+            IFS= read -r "lines[$i]"
+            if [[ -z ${lines[$i]} ]] || [[ ${lines[$i]} == (#b)(---|Index:)* ]]; then
+                lines[$i]=()
+                break
+            fi
+        done < "$1"
+        
+        if needle=${lines[(i)Subject:*]}; (( needle <= $#lines )); then
+            # "Subject: foo" line, plus rfc822 whitespace unfolding.
+            #
+            # Example: 'git format-patch' patches.
+            REPLY=${lines[needle]}
+            REPLY=${REPLY#*: }
+            REPLY=${REPLY#\[PATCH\] }
+            while [[ ${${lines[++needle]}[1]} == ' ' ]]; do
+                REPLY+=${lines[needle]}
+            done
+        elif needle=${lines[(r)Description:*]}; [[ -n $needle ]]; then
+            # "Description: foo" line.
+            #
+            # Example: DEP-3 patches.
+            REPLY=${needle#*: }
+        elif [[ ${lines[1]} == '# HG changeset patch' ]] && { needle=${${lines:#([#]*)}[1]}; [[ -n $needle ]] }; then
+            # Mercurial patch
+            REPLY=$needle
+        elif (( $+lines[1] )); then
+            # The first line of the file is not part of the diff.
+            REPLY=${lines[1]}
         else
+            # The patch has no subject.
             unset REPLY
             return 0
         fi
     else
+        # The patch cannot be examined, or invalid arguments.
         unset REPLY
         return 1
     fi


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-06-04 16:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-04 16:57 [PATCH 1/2] vcs_info quilt: Factor out a helper function. No functional change Daniel Shahaf
2016-06-04 16:57 ` [PATCH 2/2] vcs_info quilt: Extract a patch subject, 2.0 Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).