Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Linus Torvalds committed May 6, 2005
2 parents d5f415e + 211232b commit 74c7cfa
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 67 deletions.
23 changes: 19 additions & 4 deletions diff-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ static void show_new_file(struct cache_entry *new)
show_file("+", new, sha1, mode);
}

static int show_modified(struct cache_entry *old, struct cache_entry *new)
static int show_modified(struct cache_entry *old,
struct cache_entry *new,
int report_missing)
{
unsigned int mode, oldmode;
unsigned char *sha1;
unsigned char old_sha1_hex[60];

if (get_stat_data(new, &sha1, &mode) < 0) {
show_file("-", old, old->sha1, old->ce_mode);
if (report_missing)
show_file("-", old, old->sha1, old->ce_mode);
return -1;
}

Expand Down Expand Up @@ -101,15 +104,27 @@ static int diff_cache(struct cache_entry **ac, int entries)
break;
}
/* Show difference between old and new */
show_modified(ac[1], ce);
show_modified(ac[1], ce, 1);
break;
case 1:
/* No stage 3 (merge) entry? That means it's been deleted */
if (!same) {
show_file("-", ce, ce->sha1, ce->ce_mode);
break;
}
/* Otherwise we fall through to the "unmerged" case */
/* We come here with ce pointing at stage 1
* (original tree) and ac[1] pointing at stage
* 3 (unmerged). show-modified with
* report-mising set to false does not say the
* file is deleted but reports true if work
* tree does not have it, in which case we
* fall through to report the unmerged state.
* Otherwise, we show the differences between
* the original tree and the work tree.
*/
if (!cached_only && !show_modified(ce, ac[1], 0))
break;
/* fallthru */
case 3:
if (generate_patch)
diff_unmerge(ce->name);
Expand Down
78 changes: 56 additions & 22 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,16 @@ static void builtin_diff(const char *name,
printf("Created: %s (mode:%s)\n", name, temp[1].mode);
else if (!path1[1][0])
printf("Deleted: %s\n", name);
else if (strcmp(temp[0].mode, temp[1].mode))
else if (strcmp(temp[0].mode, temp[1].mode)) {
printf("Mode changed: %s (%s->%s)\n", name,
temp[0].mode, temp[1].mode);
/* Be careful. We do not want to diff between
* symlink and a file.
*/
if (strncmp(temp[0].mode, "120", 3) !=
strncmp(temp[1].mode, "120", 3))
exit(0);
}
fflush(NULL);
execlp("/bin/sh","sh", "-c", cmd, NULL);
}
Expand Down Expand Up @@ -163,13 +170,35 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
if (pos < 0)
return 0;
ce = active_cache[pos];
if ((stat(name, &st) < 0) ||
if ((lstat(name, &st) < 0) ||
!S_ISREG(st.st_mode) ||
cache_match_stat(ce, &st) ||
memcmp(sha1, ce->sha1, 20))
return 0;
return 1;
}

static void prep_temp_blob(struct diff_tempfile *temp,
void *blob,
unsigned long size,
unsigned char *sha1,
int mode)
{
int fd;

strcpy(temp->tmp_path, ".diff_XXXXXX");
fd = mkstemp(temp->tmp_path);
if (fd < 0)
die("unable to create temp-file");
if (write(fd, blob, size) != size)
die("unable to write temp-file");
close(fd);
temp->name = temp->tmp_path;
strcpy(temp->hex, sha1_to_hex(sha1));
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", mode);
}

static void prepare_temp_file(const char *name,
struct diff_tempfile *temp,
struct diff_spec *one)
Expand All @@ -196,20 +225,36 @@ static void prepare_temp_file(const char *name,
if (!one->sha1_valid || use_work_tree) {
struct stat st;
temp->name = name;
if (stat(temp->name, &st) < 0) {
if (lstat(temp->name, &st) < 0) {
if (errno == ENOENT)
goto not_a_valid_file;
die("stat(%s): %s", temp->name, strerror(errno));
}
if (!one->sha1_valid)
strcpy(temp->hex, sha1_to_hex(null_sha1));
else
strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
sprintf(temp->mode, "%06o",
S_IFREG |ce_permissions(st.st_mode));
if (S_ISLNK(st.st_mode)) {
int ret;
char *buf, buf_[1024];
buf = ((sizeof(buf_) < st.st_size) ?
xmalloc(st.st_size) : buf_);
ret = readlink(name, buf, st.st_size);
if (ret < 0)
die("readlink(%s)", name);
prep_temp_blob(temp, buf, st.st_size,
(one->sha1_valid ?
one->blob_sha1 : null_sha1),
(one->sha1_valid ?
one->mode : S_IFLNK));
}
else {
if (!one->sha1_valid)
strcpy(temp->hex, sha1_to_hex(null_sha1));
else
strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
sprintf(temp->mode, "%06o",
S_IFREG |ce_permissions(st.st_mode));
}
return;
}
else {
int fd;
void *blob;
char type[20];
unsigned long size;
Expand All @@ -218,19 +263,8 @@ static void prepare_temp_file(const char *name,
if (!blob || strcmp(type, "blob"))
die("unable to read blob object for %s (%s)",
name, sha1_to_hex(one->blob_sha1));

strcpy(temp->tmp_path, ".diff_XXXXXX");
fd = mkstemp(temp->tmp_path);
if (fd < 0)
die("unable to create temp-file");
if (write(fd, blob, size) != size)
die("unable to write temp-file");
close(fd);
prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
free(blob);
temp->name = temp->tmp_path;
strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
temp->hex[40] = 0;
sprintf(temp->mode, "%06o", one->mode);
}
}

Expand Down
139 changes: 101 additions & 38 deletions git-apply-patch-script
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,134 @@
#

case "$#" in
2) exit 1 ;; # do not feed unmerged diff to me!
2)
echo >&2 "cannot handle unmerged diff on path $1."
exit 1 ;;
esac
name="$1" tmp1="$2" hex1="$3" mode1="$4" tmp2="$5" hex2="$6" mode2="$7"
case "$mode1" in *7??) mode1=+x ;; *6??) mode1=-x ;; esac
case "$mode2" in *7??) mode2=+x ;; *6??) mode2=-x ;; esac

if test -f "$name.orig" || test -f "$name.rej"
then
echo >&2 "Unresolved patch conflicts in the previous run found."
exit 1
fi
type1=f
case "$mode1" in
*120???) type1=l ;;
*1007??) mode1=+x ;;
*1006??) mode1=-x ;;
.) type1=- ;;
esac

type2=f
case "$mode2" in
*120???) type2=l ;;
*1007??) mode2=+x ;;
*1006??) mode2=-x ;;
.) type2=- ;;
esac

case "$type1,$type2" in

case "$mode1,$mode2" in
.,?x)
# newly created
-,?)
dir=$(dirname "$name")
case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
case "$dir" in '' | .) ;; *) mkdir -p "$dir" ;; esac || {
echo >&2 "cannot create leading path for $name."
exit 1
}
if test -f "$name"
if test -e "$name"
then
echo >&2 "file $name to be created already exists."
echo >&2 "path $name to be created already exists."
exit 1
fi
cat "$tmp2" >"$name" || {
echo >&2 "cannot create $name."
exit 1
}
case "$mode2" in
+x)
echo >&2 "created $name with mode +x."
chmod "$mode2" "$name"
;;
-x)
echo >&2 "created $name."
case "$type2" in
f)
# creating a regular file
cat "$tmp2" >"$name" || {
echo >&2 "cannot create a regular file $name."
exit 1
}
case "$mode2" in
+x)
echo >&2 "created a regular file $name with mode +x."
chmod "$mode2" "$name"
;;
-x)
echo >&2 "created a regular file $name."
;;
esac
;;
l)
# creating a symlink
ln -s "$(cat "$tmp2")" "$name" || {
echo >&2 "cannot create a symbolic link $name."
exit 1
}
echo >&2 "created a symbolic link $name."
;;
*)
echo >&2 "do not know how to create $name of type $type2."
exit 1
esac
git-update-cache --add -- "$name"
;;
?x,.)
# deleted
echo >&2 "deleted $name."
git-update-cache --add -- "$name" ;;

?,-)
rm -f "$name" || {
echo >&2 "cannot remove $name";
echo >&2 "cannot remove $name"
exit 1
}
echo >&2 "deleted $name."
git-update-cache --remove -- "$name" ;;

l,f|f,l)
echo >&2 "cannot change a regular file $name and a symbolic link $name."
exit 1 ;;

l,l)
# symlink to symlink
current=$(readlink "$name") || {
echo >&2 "cannot read the target of the symbolic link $name."
exit 1
}
git-update-cache --remove -- "$name"
;;
*)
original=$(cat "$tmp1")
next=$(cat "$tmp2")
test "$original" != "$current" || {
echo >&2 "cannot apply symbolic link target change ($original->$next) to $name which points to $current."
exit 1
}
if test "$next" != "$current"
then
rm -f "$name" && ln -s "$next" "$name" || {
echo >&2 "cannot create symbolic link $name."
exit 1
}
echo >&2 "changed symbolic target of $name."
git-update-cache -- "$name"
fi ;;

f,f)
# changed
test -e "$name" || {
echo >&2 "regular file $name to be patched does not exist."
exit 1
}
dir=$(dirname "$name")
case "$dir" in '' | .) ;; *) mkdir -p "$dir" esac || {
case "$dir" in '' | .) ;; *) mkdir -p "$dir";; esac || {
echo >&2 "cannot create leading path for $name."
exit 1
}
# This will say "patching ..." so we do not say anything outselves.
diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" | patch -p1 || exit
tmp=.git-apply-patch-$$
trap "rm -f $tmp-*" 0 1 2 3 15

# Be careful, in case "$tmp2" is borrowed path from the work tree
# we are looking at...
diff -u -L "a/$name" -L "b/$name" "$tmp1" "$tmp2" >$tmp-patch

# This will say "patching ..." so we do not say anything outselves.
patch -p1 <$tmp-patch || exit
rm -f $tmp-patch
case "$mode1,$mode2" in
"$mode2,$mode1") ;;
*)
echo >&2 "changing mode from $mode1 to $mode2."
chmod "$mode2" "$name"
echo >&2 "changed mode from $mode1 to $mode2."
;;
esac
git-update-cache -- "$name"

esac
Empty file modified git-prune-script
100644 → 100755
Empty file.
Loading

0 comments on commit 74c7cfa

Please sign in to comment.