threads / patch / 41045

patchgit-p4: ignore P4 changelists that only touch files

Subject: [PATCH v1] git-p4: ignore P4 changelists that only touch files

## tl;dr

4 messages between Dec 20, 2015 and Dec 22, 2015. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

larsxschneider@gmail.com· Dec 20, 2015, 14:59 UTC · lore
From: Lars Schneider <larsxschneider@gmail.com>
Hi,

this patch improves "git-p4: add option to keep empty commits" (4ae048e) and therefore should be applied on master.

Thanks, Lars

Lars Schneider (1):
  git-p4: ignore P4 changelists that only touch files
 git-p4.py                            | 54 ++++++++++++++++++++++++++++++++++--
 t/t9826-git-p4-keep-empty-commits.sh | 48 ++++++++++++++++++++++++++++++--
 2 files changed, 97 insertions(+), 5 deletions(-)

-- 2.5.1

larsxschneider@gmail.com· Dec 20, 2015, 14:59 UTC · re: larsxschneider@gmail.com · lore
From: Lars Schneider <larsxschneider@gmail.com>

Detect if a P4 changelists only changes the creation or modification date of its files. Ignore these kind of changed files on import to avoid empty Git commits. This would happen because Git does not track these properties.

Signed-off-by: Lars Schneider <larsxschneider@gmail.com>
---
 git-p4.py                            | 54 ++++++++++++++++++++++++++++++++++--
 t/t9826-git-p4-keep-empty-commits.sh | 48 ++++++++++++++++++++++++++++++--
 2 files changed, 97 insertions(+), 5 deletions(-)
Show changes to 2 files +97 −5

git-p4.py, t/t9826-git-p4-keep-empty-commits.sh

diff --git a/git-p4.py b/git-p4.py
index 7a9dd6a..c38a8f7 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -328,6 +328,15 @@ def split_p4_type(p4type):
     return (base, mods)
 
 #
+# converts P4 mods to Git file modes
+#
+def convert_to_git_mode(p4_mods):
+    if 'x' in p4_mods:
+        return '100755'
+    else:
+        return '100644'
+
+#
 # return the raw p4 type of a file (text, text+ko, etc)
 #
 def p4_type(f):
@@ -335,6 +344,35 @@ def p4_type(f):
     return results[0]['headType']
 
 #
+# Returns true if Git would mark the diff of a file `f` in `changelist1`
+# and `changelist2` as modified. The change detection is very conservative
+# as a P4 file type change would usually not change the Git content.
+# However, in case of symlinks or UTF-16 files that might happen.
+#
+def p4_file_changed(f, changelist1, changelist2):
+    if not changelist1 or not changelist2:
+        return True
+    results = p4CmdList([
+        'diff2',
+        '{}@{}'.format(wildcard_encode(f), changelist1),
+        '{}@{}'.format(wildcard_encode(f), changelist2)
+    ])
+    if len(results) != 1:
+        return True
+    elif ('status' not in results[0] or
+          'type' not in results[0] or
+          'type2' not in results[0]):
+        return True
+    else:
+        (type1, mods1) = split_p4_type(results[0]['type'])
+        (type2, mods2) = split_p4_type(results[0]['type2'])
+        return (
+            results[0]['status'] != 'identical' or
+            convert_to_git_mode(mods1) != convert_to_git_mode(mods2) or
+            type1 != type2
+        )
+
+#
 # Given a type base and modifier, return a regexp matching
 # the keywords that can be expanded in the file
 #
@@ -2394,10 +2432,8 @@ class P4Sync(Command, P4UserMap):
             sys.stdout.flush()
 
         (type_base, type_mods) = split_p4_type(file["type"])
+        git_mode = convert_to_git_mode(type_mods)
 
-        git_mode = "100644"
-        if "x" in type_mods:
-            git_mode = "100755"
         if type_base == "symlink":
             git_mode = "120000"
             # p4 print on a symlink sometimes contains "target\n";
@@ -2656,6 +2692,14 @@ class P4Sync(Command, P4UserMap):
         files = [f for f in files
             if self.inClientSpec(f['path']) and self.hasBranchPrefix(f['path'])]
 
+        files = [f for f in files
+            if not f['action'] == 'edit' or p4_file_changed(
+                f['path'],
+                details.get('previous_change', None),
+                details['change']
+            )
+        ]
+
         if not files and not gitConfigBool('git-p4.keepEmptyCommits'):
             print('Ignoring revision {0} as it would produce an empty commit.'
                 .format(details['change']))
@@ -2993,8 +3037,10 @@ class P4Sync(Command, P4UserMap):
 
     def importChanges(self, changes):
         cnt = 1
+        previous_change = None
         for change in changes:
             description = p4_describe(change)
+            description['previous_change'] = previous_change
             self.updateOptionDict(description)
 
             if not self.silent:
@@ -3069,6 +3115,8 @@ class P4Sync(Command, P4UserMap):
                                 self.initialParent)
                     # only needed once, to connect to the previous commit
                     self.initialParent = ""
+
+                previous_change = change
             except IOError:
                 print self.gitError.read()
                 sys.exit(1)
diff --git a/t/t9826-git-p4-keep-empty-commits.sh b/t/t9826-git-p4-keep-empty-commits.sh
index be12960..23a300a 100755
--- a/t/t9826-git-p4-keep-empty-commits.sh
+++ b/t/t9826-git-p4-keep-empty-commits.sh
@@ -15,7 +15,7 @@ test_expect_success 'Create a repo' '
 
 		mkdir -p subdir &&
 
-		>subdir/file1.txt &&
+		echo "content1" >subdir/file1.txt &&
 		p4 add subdir/file1.txt &&
 		p4 submit -d "Add file 1" &&
 
@@ -35,7 +35,21 @@ test_expect_success 'Create a repo' '
 		p4 submit -d "Remove file 3" &&
 
 		p4 delete file4.txt &&
-		p4 submit -d "Remove file 4"
+		p4 submit -d "Remove file 4" &&
+
+		p4 edit subdir/file1.txt &&
+		touch subdir/file1.txt &&
+		p4 submit -d "Touch file1 - no changes" subdir/file1.txt
+
+		p4 edit -t text+x subdir/file1.txt &&
+		p4 submit -d "Change execution bit" subdir/file1.txt
+
+		p4 edit -t binary+x subdir/file1.txt &&
+		p4 submit -d "Change filetype" subdir/file1.txt
+
+		p4 edit subdir/file1.txt &&
+		echo "content1 changed" >subdir/file1.txt &&
+		p4 submit -d "Change content" subdir/file1.txt
 	)
 '
 
@@ -47,6 +61,15 @@ test_expect_success 'Clone repo root path with all history' '
 		git init . &&
 		git p4 clone --use-client-spec --destination="$git" //depot@all &&
 		cat >expect <<-\EOF &&
+Change content
+[git-p4: depot-paths = "//depot/": change = 10]
+
+Change filetype
+[git-p4: depot-paths = "//depot/": change = 9]
+
+Change execution bit
+[git-p4: depot-paths = "//depot/": change = 8]
+
 Remove file 4
 [git-p4: depot-paths = "//depot/": change = 6]
 
@@ -80,6 +103,18 @@ test_expect_success 'Clone repo subdir with all history but keep empty commits'
 		git config git-p4.keepEmptyCommits true &&
 		git p4 clone --use-client-spec --destination="$git" //depot@all &&
 		cat >expect <<-\EOF &&
+Change content
+[git-p4: depot-paths = "//depot/": change = 10]
+
+Change filetype
+[git-p4: depot-paths = "//depot/": change = 9]
+
+Change execution bit
+[git-p4: depot-paths = "//depot/": change = 8]
+
+Touch file1 - no changes
+[git-p4: depot-paths = "//depot/": change = 7]
+
 Remove file 4
 [git-p4: depot-paths = "//depot/": change = 6]
 
@@ -112,6 +147,15 @@ test_expect_success 'Clone repo subdir with all history' '
 		git init . &&
 		git p4 clone --use-client-spec --destination="$git" --verbose //depot@all &&
 		cat >expect <<-\EOF &&
+Change content
+[git-p4: depot-paths = "//depot/": change = 10]
+
+Change filetype
+[git-p4: depot-paths = "//depot/": change = 9]
+
+Change execution bit
+[git-p4: depot-paths = "//depot/": change = 8]
+
 Remove file 3
 [git-p4: depot-paths = "//depot/": change = 5]
 
-- 
2.5.1
Lars Schneider· Dec 22, 2015, 09:18 UTC · re: larsxschneider@gmail.com · lore

Re: [PATCH v1] git-p4: ignore P4 changelists that only touch files

On 20 Dec 2015, at 15:59, larsxschneider@gmail.com wrote:
Show 9 quoted lines
> From: Lars Schneider <larsxschneider@gmail.com>
> 
> Hi,
> 
> this patch improves "git-p4: add option to keep empty commits" (4ae048e)
> and therefore should be applied on master.
> 
> Thanks,
> Lars

Junio just made me aware of the inappropriate phrasing here. This is what I wanted to express:

This patch is based on master as it depends on "git-p4: add option to keep empty commits" (4ae048e)"

Sorry, Lars

Junio C Hamano· Dec 22, 2015, 23:30 UTC · re: Lars Schneider · lore

Re: [PATCH v1] git-p4: ignore P4 changelists that only touch files

Lars Schneider <larsxschneider@gmail.com> writes:
> Junio just made me aware of the inappropriate phrasing here.
> This is what I wanted to express:
>
> This patch is based on master as it depends on "git-p4: add option to keep empty commits" (4ae048e)"
Thanks.

It wasn't "inappropriate", but was merely prone to be misunderstood. And I did misunderstand it. Sorry for the noise.

← back to recent threads