make file merging respect permissions
PATCH 1/1 make file merging respect permissions
I noticed when playing about with merging that executable scripts lose their permissions after a merge. This was because unpack-file just unpacks to whatever the current umask is.
I also noticed that the merge-one-file-script thinks that if the file has been removed in both branches, then it should simply be removed. This isn't correct. The file could have been renamed to something different in both branches, in which case we have an unflagged rename conflict.
The attached fixes both issues
James
git-merge-one-file-script +52 -10
@@ -20,23 +20,47 @@ mkdir -p "$dir" case "${1:-.}${2:-.}${3:-.}" in # -# deleted in both, or deleted in one and unchanged in the other +# deleted in both +# +"$1..") + echo "ERROR: $4 is removed in both branches" + echo "ERROR: This is a potential rename conflict" + exit 1;; +# +# deleted in one and unchanged in the other # "$1.." | "$1.$1" | "$1$1.") rm -f -- "$4" + echo "Removing $4" update-cache --remove -- "$4" exit 0 ;; # -# added in one, or added identically in both +# added in one # -".$2." | "..$3" | ".$2$2") - mv $(unpack-file "${2:-$3}") $4 +".$2." | "..$3" ) + echo "Adding $4 with perm $6$7" + mv $(unpack-file "$2$3") $4 + chmod "$6$7" $4 update-cache --add -- $4 exit 0 ;; - +# +# Added in both (check for same permissions) +# +".$2$2") + if [ "$6" != "$7" ]; then + echo "ERROR:" + echo "ERROR: File $4 added in both branches, permissions conflict $6->$7" + echo "ERROR:" + exit 1 + fi + echo "Adding $4 with perm $6" + mv $(unpack-file "$2") $4 + chmod "$6" $4 + update-cache --add -- $4 + exit 0;; # # Modified in both, but differently ;( #
@@ -46,12 +70,21 @@ case "${1:-.}${2:-.}${3:-.}" in src1=$(unpack-file $2) src2=$(unpack-file $3) merge "$src2" "$orig" "$src1" - if [ $? -ne 0 ]; then - echo Leaving conflict merge in $src2 + ret=$? + if [ "$6" != "$7" ]; then + echo "ERROR: Permissions $5->$6->$7" don't match merging $src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" + fi + exit 1 + fi + chmod -- "$6" "$src2" + if [ $ -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" exit 1 fi - cp "$src2" "$4" && update-cache --add -- "$4" && exit 0 + cp -- "$src2" "$4" && chmod -- "$6" "$4" && update-cache --add -- "$4" && exit 0 ;; *) --- a/merge-cache.c +++ b/merge-cache.c
@@ -4,7 +4,7 @@ #include "cache.h" static const char *pgm = NULL; -static const char *arguments[5]; +static const char *arguments[8]; static void run_program(void) {
@@ -18,6 +18,9 @@ static void run_program(void) arguments[2], arguments[3], arguments[4], + arguments[5], + arguments[6], + arguments[7], NULL); die("unable to execute '%s'", pgm); }
@@ -36,9 +39,13 @@ static int merge_entry(int pos, const ch arguments[2] = ""; arguments[3] = ""; arguments[4] = path; + arguments[5] = ""; + arguments[6] = ""; + arguments[7] = ""; found = 0; do { static char hexbuf[4][60]; + static char ownbuf[4][60]; struct cache_entry *ce = active_cache[pos]; int stage = ce_stage(ce);
@@ -46,7 +53,9 @@ static int merge_entry(int pos, const ch break; found++; strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); + sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT)); arguments[stage] = hexbuf[stage]; + arguments[stage + 4] = ownbuf[stage]; } while (++pos < active_nr); if (!found) die("merge-cache: %s not in the cache", path);
Quoted 2 lines · James Bottomley
The attached fixes both issues
The attached patch was seriously corrupt. Did you edit it by hand? The second chunk of the git-merge-one-file-script patch claims to have 12 source lines, but only has 11, causing patch no end of confusion.
I fixed up the line counts by hand, and the end result looked almost sane, but when looking closer, I noticed the
+ if [ $ -ne 0 ]; then
+ echo "ERROR: Leaving conflict merge in $src2"
exit 1
which just can't be right either ("$" never equals 0, so the above will always report an error), so I assume that whatever editing you did was really quite corrupt. Yeah, yeah, you obviously meant "$?", but the fact is, your patch is bogus, and I don't trust it. Can you re-send a valid one (and sign off on it).
Linus
Quoted 3 lines · Linus Torvalds
The attached patch was seriously corrupt. Did you edit it by hand? The second chunk of the git-merge-one-file-script patch claims to have 12 source lines, but only has 11, causing patch no end of confusion.
OK ... yes I admit it ... the shame ... I noticed a problem after I generated it and then edited it in source
Quoted 7 lines
I fixed up the line counts by hand, and the end result looked almost sane, but when looking closer, I noticed the
+ if [ $ -ne 0 ]; then
+ echo "ERROR: Leaving conflict merge in $src2"
exit 1
Quoted 5 lines
which just can't be right either ("$" never equals 0, so the above will always report an error), so I assume that whatever editing you did was really quite corrupt. Yeah, yeah, you obviously meant "$?", but the fact is, your patch is bogus, and I don't trust it. Can you re-send a valid one (and sign off on it).
Yes boss ... Just getting on a 'plane now. Should be able to send it out in an hour or so.
James
Quoted 4 lines · Linus Torvalds
Yeah, yeah, you obviously meant "$?", but the fact
is, your patch is bogus, and I don't trust it. Can you re-send a valid one
(and sign off on it).]
OK, here it is .. I have an excuse, honest ... and a note from my mum.
---
This patch fixes two problems
1) permissions aren't respected in the merge script (primarily because they're never passed in to it in the first place). Fix that and also check for permission conflicts in the merge
2) the delete of a file in both branches may indeed be just that, but it could also be the indicator of a rename conflict (file moved to different locations in both branches), so error out and ask the committer for guidance.
git-merge-one-file-script +52 -10
@@ -20,23 +20,47 @@ mkdir -p "$dir" case "${1:-.}${2:-.}${3:-.}" in # -# deleted in both, or deleted in one and unchanged in the other +# deleted in both +# +"$1..") + echo "ERROR: $4 is removed in both branches" + echo "ERROR: This is a potential rename conflict" + exit 1;; +# +# deleted in one and unchanged in the other # "$1.." | "$1.$1" | "$1$1.") rm -f -- "$4" + echo "Removing $4" update-cache --remove -- "$4" exit 0 ;; # -# added in one, or added identically in both +# added in one # -".$2." | "..$3" | ".$2$2") - mv $(unpack-file "${2:-$3}") $4 +".$2." | "..$3" ) + echo "Adding $4 with perm $6$7" + mv $(unpack-file "$2$3") $4 + chmod "$6$7" $4 update-cache --add -- $4 exit 0 ;; - +# +# Added in both (check for same permissions) +# +".$2$2") + if [ "$6" != "$7" ]; then + echo "ERROR:" + echo "ERROR: File $4 added in both branches, permissions conflict $6->$7" + echo "ERROR:" + exit 1 + fi + echo "Adding $4 with perm $6" + mv $(unpack-file "$2") $4 + chmod "$6" $4 + update-cache --add -- $4 + exit 0;; # # Modified in both, but differently ;( #
@@ -46,12 +70,21 @@ case "${1:-.}${2:-.}${3:-.}" in src1=$(unpack-file $2) src2=$(unpack-file $3) merge "$src2" "$orig" "$src1" - if [ $? -ne 0 ]; then - echo Leaving conflict merge in $src2 + ret=$? + if [ "$6" != "$7" ]; then + echo "ERROR: Permissions $5->$6->$7 don't match merging $src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" + fi + exit 1 + fi + chmod -- "$6" "$src2" + if [ $ -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" exit 1 fi - cp "$src2" "$4" && update-cache --add -- "$4" && exit 0 + cp -- "$src2" "$4" && chmod -- "$6" "$4" && update-cache --add -- "$4" && exit 0 ;; *) --- a/merge-cache.c +++ b/merge-cache.c
@@ -4,7 +4,7 @@ #include "cache.h" static const char *pgm = NULL; -static const char *arguments[5]; +static const char *arguments[8]; static void run_program(void) {
@@ -18,6 +18,9 @@ static void run_program(void) arguments[2], arguments[3], arguments[4], + arguments[5], + arguments[6], + arguments[7], NULL); die("unable to execute '%s'", pgm); }
@@ -36,9 +39,13 @@ static int merge_entry(int pos, const ch arguments[2] = ""; arguments[3] = ""; arguments[4] = path; + arguments[5] = ""; + arguments[6] = ""; + arguments[7] = ""; found = 0; do { static char hexbuf[4][60]; + static char ownbuf[4][60]; struct cache_entry *ce = active_cache[pos]; int stage = ce_stage(ce);
@@ -46,7 +53,9 @@ static int merge_entry(int pos, const ch break; found++; strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); + sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT)); arguments[stage] = hexbuf[stage]; + arguments[stage + 4] = ownbuf[stage]; } while (++pos < active_nr); if (!found) die("merge-cache: %s not in the cache", path);
Dear diary, on Sat, Apr 23, 2005 at 11:44:40PM CEST, I got a letter where James Bottomley <[email protected]> told me that...
Quoted 8 lines · Linus Torvalds
Quoted 4 lines
Yeah, yeah, you obviously meant "$?", but the fact
is, your patch is bogus, and I don't trust it. Can you re-send a valid one
(and sign off on it).]OK, here it is .. I have an excuse, honest ... and a note from my mum.
git-merge-one-file-script case "${1:-.}${2:-.}${3:-.}" in · line 82
Quoted 10 lines
git-merge-one-file-script +10
-0+ ret=$? + if [ "$6" != "$7" ]; then + echo "ERROR: Permissions $5->$6->$7 don't match merging $src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" + fi + exit 1 + fi + chmod -- "$6" "$src2" + if [ $ -ne 0 ]; then
*cough*
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Quoted 3 lines · Petr Baudis
git-merge-one-file-script case "${1:-.}${2:-.}${3:-.}" in · line 82
Quoted 1 line
git-merge-one-file-script +1
-0+ if [ $ -ne 0 ]; then*cough*
You're kidding me?
Well ... it's correct in my repository ... I'll investigate my diff script
James
Quoted 1 line · Petr Baudis
*cough*
OK, dirty file in the local tree, sorry.
This is the actual diff
---
1) permissions aren't respected in the merge script (primarily because they're never passed in to it in the first place). Fix that and also check for permission conflicts in the merge
2) the delete of a file in both branches may indeed be just that, but it could also be the indicator of a rename conflict (file moved to different locations in both branches), so error out and ask the committer for guidance.
git-merge-one-file-script +50 -10
@@ -20,23 +20,45 @@ mkdir -p "$dir" case "${1:-.}${2:-.}${3:-.}" in # -# deleted in both, or deleted in one and unchanged in the other +# deleted in both +# +"$1..") + echo "ERROR: $4 is removed in both branches" + echo "ERROR: This is a potential rename conflict" + exit 1;; +# +# deleted in one and unchanged in the other # "$1.." | "$1.$1" | "$1$1.") rm -f -- "$4" + echo "Removing $4" update-cache --remove -- "$4" exit 0 ;; # -# added in one, or added identically in both +# added in one # -".$2." | "..$3" | ".$2$2") - mv $(unpack-file "${2:-$3}") $4 +".$2." | "..$3" ) + echo "Adding $4 with perm $6$7" + mv $(unpack-file "$2$3") $4 + chmod "$6$7" $4 update-cache --add -- $4 exit 0 ;; - +# +# Added in both (check for same permissions) +# +".$2$2") + if [ "$6" != "$7" ]; then + echo "ERROR: File $4 added in both branches, permissions conflict $6->$7" + exit 1 + fi + echo "Adding $4 with perm $6" + mv $(unpack-file "$2") $4 + chmod "$6" $4 + update-cache --add -- $4 + exit 0;; # # Modified in both, but differently ;( #
@@ -46,12 +68,21 @@ case "${1:-.}${2:-.}${3:-.}" in src1=$(unpack-file $2) src2=$(unpack-file $3) merge "$src2" "$orig" "$src1" - if [ $? -ne 0 ]; then - echo Leaving conflict merge in $src2 + ret=$? + if [ "$6" != "$7" ]; then + echo "ERROR: Permissions $5->$6->$7 don't match merging $src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" + fi + exit 1 + fi + chmod -- "$6" "$src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" exit 1 fi - cp "$src2" "$4" && update-cache --add -- "$4" && exit 0 + cp -- "$src2" "$4" && chmod -- "$6" "$4" && update-cache --add -- "$4" && exit 0 ;; *) --- a/merge-cache.c +++ b/merge-cache.c
@@ -4,7 +4,7 @@ #include "cache.h" static const char *pgm = NULL; -static const char *arguments[5]; +static const char *arguments[8]; static void run_program(void) {
@@ -18,6 +18,9 @@ static void run_program(void) arguments[2], arguments[3], arguments[4], + arguments[5], + arguments[6], + arguments[7], NULL); die("unable to execute '%s'", pgm); }
@@ -36,9 +39,13 @@ static int merge_entry(int pos, const ch arguments[2] = ""; arguments[3] = ""; arguments[4] = path; + arguments[5] = ""; + arguments[6] = ""; + arguments[7] = ""; found = 0; do { static char hexbuf[4][60]; + static char ownbuf[4][60]; struct cache_entry *ce = active_cache[pos]; int stage = ce_stage(ce);
@@ -46,7 +53,9 @@ static int merge_entry(int pos, const ch break; found++; strcpy(hexbuf[stage], sha1_to_hex(ce->sha1)); + sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT)); arguments[stage] = hexbuf[stage]; + arguments[stage + 4] = ownbuf[stage]; } while (++pos < active_nr); if (!found) die("merge-cache: %s not in the cache", path);
Quoted 2 lines · James Bottomley
This is the actual diff
This is _still_ corrupted.
Are you editing your diffs by hand without understanding how the diffs work?
The second chunk of the "git-merge-one-file" diff _still_ claims to change twelve lines, and that diff _still_ only changes eleven lines. My "patch" isn't happy, and I can count the lines in the diff myself and verify that it's not patch that is wrong, it's your diff.
Please please _please_ don't edit diffs by hand if you don't know what you're doing. Generate the diff from a clean source instead. Or ask me to fix it up, I'm so used to editing diffs that I can do it in my sleep.
Linus
Quoted 9 lines · Linus Torvalds
Quoted 2 lines · James Bottomley
This is the actual diff
This is _still_ corrupted.
Are you editing your diffs by hand without understanding how the diffs work?
Sort of: I'm taking a diff between my tree and yours but stripping out the extraneous pieces (which are whole file diffs, nothing in the actual diffs I send you).
Quoted 8 lines
The second chunk of the "git-merge-one-file" diff _still_ claims to change twelve lines, and that diff _still_ only changes eleven lines. My "patch" isn't happy, and I can count the lines in the diff myself and verify that it's not patch that is wrong, it's your diff.
Please please _please_ don't edit diffs by hand if you don't know what you're doing. Generate the diff from a clean source instead. Or ask me to fix it up, I'm so used to editing diffs that I can do it in my sleep.
OK ... this is done by reapplying to the top of your git tree and then doing a show-diff. I've checked that it actually applies without rejections to a pristine git tree.
James
git-merge-one-file-script: b2adc5fc4583e6089f4b38a47e1a73caad183f20
git-merge-one-file-script +39 -8
@@ -20,23 +20,45 @@ mkdir -p "$dir" case "${1:-.}${2:-.}${3:-.}" in # -# deleted in both, or deleted in one and unchanged in the other +# deleted in both +# +"$1..") + echo "ERROR: $4 is removed in both branches" + echo "ERROR: This is a potential rename conflict" + exit 1;; +# +# deleted in one and unchanged in the other # "$1.." | "$1.$1" | "$1$1.") rm -f -- "$4" + echo "Removing $4" update-cache --remove -- "$4" exit 0 ;; # -# added in one, or added identically in both +# added in one # -".$2." | "..$3" | ".$2$2") - mv $(unpack-file "${2:-$3}") $4 +".$2." | "..$3" ) + echo "Adding $4 with perm $6$7" + mv $(unpack-file "$2$3") $4 + chmod "$6$7" $4 update-cache --add -- $4 exit 0 ;; - +# +# Added in both (check for same permissions) +# +".$2$2") + if [ "$6" != "$7" ]; then + echo "ERROR: File $4 added in both branches, permissions conflict $6->$7" + exit 1 + fi + echo "Adding $4 with perm $6" + mv $(unpack-file "$2") $4 + chmod "$6" $4 + update-cache --add -- $4 + exit 0;; # # Modified in both, but differently ;( #
@@ -46,11 +68,20 @@ case "${1:-.}${2:-.}${3:-.}" in src1=$(unpack-file $2) src2=$(unpack-file $3) merge "$src2" "$orig" "$src1" - if [ $? -ne 0 ]; then - echo Leaving conflict merge in $src2 + ret=$? + if [ "$6" != "$7" ]; then + echo "ERROR: Permissions $5->$6->$7 don't match merging $src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" + fi + exit 1 + fi + chmod -- "$6" "$src2" + if [ $ret -ne 0 ]; then + echo "ERROR: Leaving conflict merge in $src2" exit 1 fi - cp "$src2" "$4" && update-cache --add -- "$4" && exit 0 + cp -- "$src2" "$4" && chmod -- "$6" "$4" && update-cache --add -- "$4" && exit 0 ;; *)
merge-cache.c: 68ac4ab2d926005a5a729017c1bcb70a30634bd8
merge-cache.c +10 -1
@@ -4,7 +4,7 @@#include "cache.h" static const char *pgm =NULL ; -static const char *arguments[5 ]; +static const char *arguments[8 ];static void run_program (void ) {
@@ -18,6 +18,9 @@ static void run_program(void) arguments[2 ], arguments[3 ], arguments[4 ], + arguments[5 ], + arguments[6 ], + arguments[7 ], NULL);die ("unable to execute '%s'" , pgm); }
@@ -36,9 +39,13 @@ static int merge_entry(int pos, const ch arguments[2 ] ="" ; arguments[3 ] ="" ; arguments[4 ] = path; + arguments[5 ] ="" ; + arguments[6 ] ="" ; + arguments[7 ] ="" ; found =0 ; do { static char hexbuf[4][60]; + static char ownbuf[4][60]; struct cache_entry *ce = active_cache[pos]; int stage = ce_stage(ce);
@@ -46,7 +53,9 @@ static int merge_entry(int pos, const chbreak ; found++;strcpy (hexbuf[stage],sha1_to_hex (ce->sha1)); +sprintf (ownbuf[stage],"%o" ,ntohl (ce->ce_mode) & (~S_IFMT )); arguments[stage] = hexbuf[stage]; + arguments[stage +4 ] = ownbuf[stage]; }while (++pos < active_nr);if (!found)die ("merge-cache: %s not in the cache" , path);
Quoted 4 lines · James Bottomley
OK ... this is done by reapplying to the top of your git tree and then doing a show-diff. I've checked that it actually applies without rejections to a pristine git tree.
Well, I actually ended up editing the line numbers manually and already pushed out the previous one, so you might want to double-check that my current tree matches yours.
Linus
Quoted 3 lines · Linus Torvalds
Well, I actually ended up editing the line numbers manually and already pushed out the previous one, so you might want to double-check that my current tree matches yours.
Yes, the sha1 of my changed file and the one you merged match, so everything looks sane.
Sorry for the confusion,
James
Quoted 3 lines · Linus Torvalds
Well, I actually ended up editing the line numbers manually and already pushed out the previous one, so you might want to double-check that my current tree matches yours.
Just to get closure on this, I went over all the parts of patch generation, and the culprit turns out to be emacs. Apparently its diff mode (which I never thought to investigate how to use) does these re- writes ... no idea why, but the problem goes away if I strip out the extraneous pieces in fundamental-mode. It's never given me this trouble before ... I think the diff I'm producing must be confusing it somehow.
James
[PATH] fix segfault in fsck-cache
In reply to Linus TorvaldsI somehow got some corrupted object files in my repository that zlib refuses to decompress. This patch makes sure we abort early before the memcpy a few lines downtream segfaults (due to total_out == 0).
Andreas
sha1_file.c +2 -0
@@ -155,6 +155,8 @@inflateInit (&stream); ret =inflate (&stream,0 ); +if (ret !=Z_OK ) +return NULL ;if (sscanf (buffer,"%10s %lu" , type, size) !=2 )return NULL ;
Re: [PATH] fix segfault in fsck-cache
This isn't working right either, depending on your ZLIB version. Do not apply.
Andreas
Quoted 26 lines · Andreas Gal
I somehow got some corrupted object files in my repository that zlib refuses to decompress. This patch makes sure we abort early before the memcpy a few lines downtream segfaults (due to
total_out== 0).Andreas
Signed-off-by Andreas Gal <[email protected]>sha1_file.c +2
-1@@ -155,6 +155,8 @@inflateInit (&stream); ret =inflate (&stream,0 ); +if (ret !=Z_OK ) +return NULL ;if (sscanf (buffer,"%10s %lu" , type, size) !=2 )return NULL ; -To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html