Skip to contentSkip to conversation

git-p4: RCS keyword handling

PATCH Superseded Luke Diamand 4 participants 7 messages Jun 15, 2016 Documentation

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 git-p4: RCS keyword handling

Updated patch to handle RCS keyword expansion performed by perforce in git-p4. This incorporates Pete Wyckoff's suggestions and test cases, and also adds a test for handling deletion of a file containing RCS keywords.

Eric - could I ask you to take a look at this and see if it solves your particular problem.

Thanks.

Luke Diamand (1):
git-p4: add initial support for RCS keywords

 Documentation/git-p4.txt   |    5 +
 contrib/fast-import/git-p4 |  116 +++++++++++++++--
 t/t9810-git-p4-rcs.sh      |  304 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 415 insertions(+), 10 deletions(-)
 create mode 100755 t/t9810-git-p4-rcs.sh
Signature · 1 line
1.7.9.259.ga92e
#1 lore raw

PATCH v3 1/1 git-p4: add initial support for RCS keywords

RCS keywords cause problems for git-p4 as perforce always expands them (if +k is set) and so when applying the patch, git reports that the files have been modified by both sides, when in fact they haven't.

This change means that when git-p4 detects a problem applying a patch, it will check to see if keyword expansion could be the culprit. If it is, it strips the keywords in the p4 repository so that they match what git is expecting. It then has another go at applying the patch.

This behaviour is enabled with a new git-p4 configuration option and is off by default.

---
 Documentation/git-p4.txt   |    5 +
 contrib/fast-import/git-p4 |  116 +++++++++++++++--
 t/t9810-git-p4-rcs.sh      |  304 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 415 insertions(+), 10 deletions(-)
 create mode 100755 t/t9810-git-p4-rcs.sh
Documentation/git-p4.txt +5 -0
index 8b92cc0..7fa47c8 100644
@@ -483,6 +483,11 @@ git-p4.skipUserNameCheck::
 	user map, 'git p4' exits.  This option can be used to force
 	submission regardless.
 
+git-p4.attemptRCSCleanup:
+    If enabled, 'git p4 submit' will attempt to sort cleanup RCS keywords
+    ($Header$, etc). These would otherwise cause merge conflicts and prevent
+    the submit going ahead. This option should be considered experimental at
+    present.
 
 IMPLEMENTATION DETAILS
 ----------------------
contrib/fast-import/git-p4 +106 -10

155 lines not on this page · raw message

t/t9810-git-p4-rcs.sh new file +304 -0

304 lines not on this page · raw message

Signature · 1 line
1.7.9.259.ga92e
#2 lore raw

Re: [PATCHv3] git-p4: add initial support for RCS keywords

It seems that this seems to have been further refactored by introducing a couple of new helper functions. I'll replae what was queued in 'pu' with this, with the following minor fix-ups.

Thanks.

 t/t9810-git-p4-rcs.sh |   10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
t/t9810-git-p4-rcs.sh +5 -5
index 77eebd2..3013bab 100755
@@ -201,7 +201,7 @@ test_expect_success 'cleanup after failure 2' '
 	)
 '
 
-create_kw_file() {
+create_kw_file () {
 	cat <<\EOF >"$1"
 /* A file
 	Id: $Id$
@@ -226,7 +226,7 @@ test_expect_success 'add kwfile' '
 	)
 '
 
-p4_append_to_file() {
+p4_append_to_file () {
 	f=$1 &&
 	p4 edit -t ktext "$f" &&
 	echo "/* $(date) */" >>"$f" &&
@@ -248,7 +248,7 @@ test_expect_success 'cope with rcs keyword expansion damage' '
 		old_lines=$(wc -l <kwfile1.c) &&
 		perl -n -i -e "print unless m/Revision:/" kwfile1.c &&
 		new_lines=$(wc -l <kwfile1.c) &&
-		expr $new_lines == $old_lines - 1 &&
+		test $new_lines = $(($old_lines - 1)) &&
 
 		git add kwfile1.c &&
 		git commit -m "Zap an RCS kw line" &&
@@ -278,12 +278,12 @@ test_expect_success 'cope with rcs keyword file deletion' '
 		p4 add -t ktext kwdelfile.c &&
 		p4 submit -d "Add file to be deleted" &&
 		cat kwdelfile.c &&
-		grep -q 1 kwdelfile.c
+		grep 1 kwdelfile.c
 	) &&
 	"$GITP4" clone --dest="$git" //depot &&
 	(
 		cd "$git" &&
-		grep -q Revision kwdelfile.c &&
+		grep Revision kwdelfile.c &&
 		git rm -f kwdelfile.c &&
 		git commit -m "Delete a file containing RCS keywords" &&
 		git config git-p4.skipSubmitEdit true &&
#3 lore raw

Re: [PATCHv3] git-p4: add initial support for RCS keywords

In reply to Luke Diamand

[email protected] wrote on Tue, 14 Feb 2012 22:33 +0000:

Quoted 1 line
Documentation/git-p4.txt +0 -0

[..]

Documentation/git-p4.txt git-p4.skipUserNameCheck:: · line 490

Quoted 5 lines
Documentation/git-p4.txt +5 -0
+git-p4.attemptRCSCleanup:
+    If enabled, 'git p4 submit' will attempt to sort cleanup RCS keywords
+    ($Header$, etc). These would otherwise cause merge conflicts and prevent
+    the submit going ahead. This option should be considered experimental at
+    present.

=> "attempt to cleanup"

Quoted 1 line
contrib/fast-import/git-p4 +0 -0

[..]

Quoted 4 lines
contrib/fast-import/git-p4 +4 -0
+#
+# return the raw p4 type of a file (text, text+ko, etc)
+#
+def p4_type(file):

contrib/fast-import/git-p4 def split_p4_type(p4type): · line 202

Quoted 10 lines
contrib/fast-import/git-p4 +10 -0
+    files = p4_read_pipe_lines(["files", file])
+    info = files[0]
+    m = re.search(r'\(([a-z0-9A-Z+]+)\)\s*$', info)
+    if m:
+        ret = m.group(1)
+        if verbose:
+            print "%s => %s" % (file, ret)
+        return ret
+    else:
+        die("Could not extract file type from '%s'" % info)

p4 fstat -T headType
gives just the type, no need to parse the info string

contrib/fast-import/git-p4 def split_p4_type(p4type): · line 213

Quoted 10 lines
contrib/fast-import/git-p4 +10 -0
+#
+# Given a type base and modifier, return a regexp matching
+# the keywords that can be expanded in the file
+#
+def p4_keywords_regexp_for_type(base, type_mods):
+    if base in ("text", "unicode", "binary"):
+        if "ko" in type_mods:
+            return r'\$(Id|Header)[^$]*\$'
+        elif "k" in type_mods:
+            return r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)[^$]*\$'

Still no ":"? Won't that match too much?

Quoted 1 line

+ def patchRCSKeywords(self, file, pattern):

Nice, clean.

Quoted 1 line

def p4UserForCommit(self,id):

[..]

contrib/fast-import/git-p4 class P4Submit(Command, P4UserMap): · line 1045

Quoted 5 lines
contrib/fast-import/git-p4 +5 -0
+                for line in read_pipe_lines(diffcmd):
+                    # read diff lines: for each file reported, if it can have
+                    # keywords expanded, and the diff contains keywords, then
+                    # try zapping the p4 file.
+                    m = re.match(r'^diff --git a/(.*)\s+b/(.*)', line)

I'm still sort of iffy on this. You have editedFiles, and could just use that directly. Grepping through the diff won't give you any more information.

contrib/fast-import/git-p4 class P4Submit(Command, P4UserMap): · line 1052

Quoted 2 lines
contrib/fast-import/git-p4 +2 -0
+                    if pattern:
+                        print line

Debugging? Maybe add a leading comment saying why this line.

Quoted 8 lines
contrib/fast-import/git-p4 +0 -4
@@ -1585,15 +1685,11 @@ class P4Sync(Command, P4UserMap):
 
         # Note that we do not try to de-mangle keywords on utf16 files,
         # even though in theory somebody may want that.
-        if type_base in ("text", "unicode", "binary"):
-            if "ko" in type_mods:
-                text = ''.join(contents)
-                text = re.sub(r'\$(Id|Header):[^$]*\$', r'$\1$', text)

contrib/fast-import/git-p4 class P4Sync(Command, P4UserMap): · line 1692

Quoted 10 lines
contrib/fast-import/git-p4 +5 -5
-                contents = [ text ]
-            elif "k" in type_mods:
-                text = ''.join(contents)
-                text = re.sub(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', text)
-                contents = [ text ]
+        pattern = p4_keywords_regexp_for_type(type_base, type_mods)
+        if pattern:
+            text = ''.join(contents)
+            text = re.sub(pattern, r'$\1$', text)
+            contents = [ text ]

Nice. Glad you refactored this.

Fix the colon thing at least, then happy to add my Acked-By.

Sorry for the delay.

		-- Pete
#4 lore raw

Re: [PATCHv3] git-p4: add initial support for RCS keywords

[Resent in plain-text. Apologies for dupe content to Pete and Luke.]

Quoted 17 lines · Pete Wyckoff

[email protected] wrote on Tue, 14 Feb 2012 22:33 +0000:

Quoted 11 lines
contrib/fast-import/git-p4 +10 -0
+#
+# Given a type base and modifier, return a regexp matching
+# the keywords that can be expanded in the file
+#
+def p4_keywords_regexp_for_type(base, type_mods):
+    if base in ("text", "unicode", "binary"):
+        if "ko" in type_mods:
+            return r'\$(Id|Header)[^$]*\$'
+        elif "k" in type_mods:
+            return
Quoted 1 line

r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)[^$]*\$'

Still no ":"?  Won't that match too much?

Quoted 1 line

Fix the colon thing at least, then happy to add my Acked-By.

No, that would be an incorrect change. The colon is added by P4 when it expands the keyword pattern, but it is *not* part of the pattern required by P4 to trigger a keyword expansion.

http://kb.perforce.com/article/54/using-rcs-keywords

-Eric

--
Eric Scouten :: software developer, photographer :: Poulsbo, WA (near Seattle)
http://ericscouten.com :: click for Flickr, Facebook, Twitter, LinkedIn links

#5 lore raw

Re: [PATCHv3] git-p4: add initial support for RCS keywords

Quoted 11 lines · Eric Scouten
Quoted 5 lines
Quoted 1 line

r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)[^$]*\$'

Still no ":"? Won't that match too much?

Fix the colon thing at least, then happy to add my Acked-By.

No, that would be an incorrect change. The colon is added by P4 when it expands the keyword pattern, but it is *not* part of the pattern required by P4 to trigger a keyword expansion.

http://kb.perforce.com/article/54/using-rcs-keywords

I have this suspicion that both Pete and your last sentence is correct, but the regexp in the patch and your "would be an incorrect change" are wrong.

I am not a P4 expert, but I would be very surprised if P4 expands "$Ida$" as if it is "$Id$" or "$Id: old expansion$", which the regexp would match.

Wouldn't it be more like this?

\$ # begins with a dollar, followed by...
( Id | Header | ... ) # one of these keywords, followed by ...
( :[^$]+ )? # possibly an old expansion, followed by
\$ # another dollar sign

#6 lore raw

Re: [PATCHv3] git-p4: add initial support for RCS keywords

Quoted 21 lines · Junio C Hamano
Quoted 11 lines · Eric Scouten
Quoted 5 lines
Quoted 1 line

r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)[^$]*\$'

Still no ":"?  Won't that match too much?

Fix the colon thing at least, then happy to add my Acked-By.

No, that would be an incorrect change. The colon is added by P4 when it expands the keyword pattern, but it is *not* part of the pattern required by P4 to trigger a keyword expansion.

http://kb.perforce.com/article/54/using-rcs-keywords

I have this suspicion that both Pete and your last sentence is correct, but the regexp in the patch and your "would be an incorrect change" are wrong.

I am not a P4 expert, but I would be very surprised if P4 expands "$Ida$" as if it is "$Id$" or "$Id: old expansion$", which the regexp would match.

Quoted 6 lines

Wouldn't it be more like this?

       \$                      # begins with a dollar, followed by... ( Id | Header | ... )   # one of these keywords, followed by ... ( :[^$]+ )?             # possibly an old expansion, followed by \$                      # another dollar sign

Good catch. Yes, you're probably right.

Signature · 2 lines
Eric Scouten :: software developer, photographer :: Poulsbo, WA (near Seattle)
http://ericscouten.com :: click for Flickr, Facebook, Twitter, LinkedIn links