Skip to contentSkip to conversation

updates for git-pull-script

RFC In review James Bottomley 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

RFC 1/1 updates for git-pull-script

The current git-pull-script has several behaviours that always trip me up

1) when pulling from a local tree, you have to remember to append the .git/ directory to the base
2) It always tries to checkout the files ... this drives me nuts since I don't have a lot of space on my laptop so I keep most trees empty.
3) It doesn't attempt a more sophisticated merge. The SCSI tree seems to be the one that always has added or removed files. Since I think git-merge-one-file-script is reasonably trustworthy, I made automatic merges using it one of the pull features.
4) It doesn't remember the old head. This is useful for unpulling (pruning back to if the merge misfires) or simply seeing the changes between the old and new heads.

The attached addresses all these points. It's what I use, but since others may prefer the original behaviour, I'm sending it as a straw horse.

James

git-pull-script +21 -8
@@ -3,9 +3,17 @@
 # use "$1" or something in a real script, this 
 # just hard-codes it.
 #
-merge_repo=$1
+merge_repo="$1"
+
+[  -d .git ] || die
+
+if [ -d "$merge_repo" -a -d "$merge_repo/.git" ]; then
+	merge_repo="$merge_repo/.git"
+fi
 
 rm -f .git/MERGE_HEAD
+rm -f .git/OLD_HEAD
+cp .git/HEAD .git/OLD_HEAD
 
 echo "Getting object database"
 rsync -avz --ignore-existing $merge_repo/objects/. .git/objects/.
@@ -32,17 +40,22 @@ if [ "$common" == "$merge_head" ]; then
 fi
 if [ "$common" == "$head" ]; then
 	echo "Updating from $head to $merge_head."
-	echo "Destroying all noncommitted data!"
-	echo "Kill me within 3 seconds.."
-	sleep 3
-	read-tree -m $merge_tree && checkout-cache -f -a && update-cache --refresh
+	echo "Reading the current tree"
+	read-tree -m $merge_tree || exit 1
 	echo $merge_head > .git/HEAD
 	exit 0
 fi
 echo "Trying to merge $merge_head into $head"
 read-tree -m $common_tree $head_tree $merge_tree
-result_tree=$(write-tree) || exit 1
-result_commit=$(echo "Merge $merge_repo" | commit-tree $result_tree -p $head -p $merge_head)
+merge_msg="Merge $merge_repo"
+result_tree=$(write-tree 2>/dev/null)
+if [ $? -ne 0 ]; then
+	echo "Simple merge failed, trying Automatic merge"
+	merge-cache git-merge-one-file-script -a
+	merge_msg="Automatic merge of $merge_repo"
+	result_tree=$(write-tree) || exit 1
+fi
+result_commit=$(echo $merge_msg | commit-tree $result_tree -p $head -p $merge_head)
 echo "Committed merge $result_commit"
 echo $result_commit > .git/HEAD
-checkout-cache -f -a && update-cache --refresh
+#checkout-cache -f -a && update-cache --refresh
#1 lore raw
Quoted 4 lines · James Bottomley

The attached addresses all these points. It's what I use, but since others may prefer the original behaviour, I'm sending it as a straw horse.

I don't think anybody preferes the original behaviour - the reason git-pull-script punted with any non-trivial merge was that when I wrote the damn thing, I was still just testing out the merges, and I definitely didn't trust the automated script.

However, when you remove the "checkout-cache -f -a" thing, it means that you are leaving all teh checked-out files in a state where it's _very_ easy to mess up later, and doign so silently really is very bad.

So at the _very_ least you should do an "update-cache --refresh", and _tell_ the user about the files that are checked-out but not up-to-date.

And it really sounds like the whole and only reason you don't like checkout-cache is that you normally work with an empty tree, so I actually think that the _right_ answer for you is to add a new flag to "checkout-cache" that only updates files that already exist. Something like "-n" for "don't create new files".

Linus

#2 lore raw
Quoted 4 lines · Linus Torvalds

I don't think anybody preferes the original behaviour - the reason git-pull-script punted with any non-trivial merge was that when I wrote the damn thing, I was still just testing out the merges, and I definitely didn't trust the automated script.

Well ... I kept checking it against BK for a while ... however it seems to do the right thing, so I've been happy ...

Quoted 3 lines

However, when you remove the "checkout-cache -f -a" thing, it means that you are leaving all teh checked-out files in a state where it's _very_ easy to mess up later, and doign so silently really is very bad.

Yes ... I really want the BK behaviour back where it would abort the update if a change has to be made to a file that I've already touched. I'm still thinking about how best to do this

Quoted 8 lines

So at the _very_ least you should do an "update-cache --refresh", and _tell_ the user about the files that are checked-out but not up-to-date.

And it really sounds like the whole and only reason you don't like checkout-cache is that you normally work with an empty tree, so I actually think that the _right_ answer for you is to add a new flag to "checkout-cache" that only updates files that already exist. Something like "-n" for "don't create new files".

OK, look over the attached. I added the -n option to checkout-cache and an --ignore-missing to update-cache. Now if I add

checkout-cache -n -f -a && update-cache --ignore-missing --refresh

it should do the right thing.

James

checkout-cache.c: f65be62b4abad184bd755884fb72681fc28c8b3b

checkout-cache.c +7 -2
@@ -34,7 +34,7 @@
  */
 #include "cache.h"
 
-static int force = 0, quiet = 0;
+static int force = 0, quiet = 0, not_new = 0;
 
 static void create_directories(const char *path)
 {
@@ -118,7 +118,8 @@ static int checkout_entry(struct cache_e
 		 * just do the right thing)
 		 */
 		unlink(path);
-	}
+	} else if (not_new) 
+		return 0;
 	return write_entry(ce, path);
 }
 
@@ -182,6 +183,10 @@ int main(int argc, char **argv)
 				quiet = 1;
 				continue;
 			}
+			if (!strcmp(arg, "-n")) {
+				not_new = 1;
+				continue;
+			}
 			if (!memcmp(arg, "--prefix=", 9)) {
 				base_dir = arg+9;
 				continue;

update-cache.c: 4353b80890ba2afbe22248a4dc25060aa4a429b2

update-cache.c +27 -6
@@ -12,7 +12,23 @@
  * like "update-cache *" and suddenly having all the object
  * files be revision controlled.
  */
-static int allow_add = 0, allow_remove = 0;
+static int allow_add = 0, allow_remove = 0, not_new = 0;
+
+/* Three functions to allow overloaded pointer return; see linux/err.h */
+static inline void *ERR_PTR(long error)
+{
+	return (void *) error;
+}
+
+static inline long PTR_ERR(const void *ptr)
+{
+	return (long) ptr;
+}
+
+static inline long IS_ERR(const void *ptr)
+{
+	return (unsigned long)ptr > (unsigned long)-1000L;
+}
 
 static int index_fd(unsigned char *sha1, int fd, struct stat *st)
 {
@@ -172,7 +188,7 @@ static struct cache_entry *refresh_entry
 	int changed, size;
 
 	if (stat(ce->name, &st) < 0)
-		return NULL;
+		return ERR_PTR(-errno);
 
 	changed = cache_match_stat(ce, &st);
 	if (!changed)
@@ -183,10 +199,10 @@ static struct cache_entry *refresh_entry
 	 * to refresh the entry - it's not going to match
 	 */
 	if (changed & MODE_CHANGED)
-		return NULL;
+		return ERR_PTR(-EINVAL);
 
 	if (compare_data(ce, st.st_size))
-		return NULL;
+		return ERR_PTR(-EINVAL);
 
 	size = ce_size(ce);
 	updated = malloc(size);
@@ -212,8 +228,9 @@ static void refresh_cache(void)
 		}
 
 		new = refresh_entry(ce);
-		if (!new) {
-			printf("%s: needs update\n", ce->name);
+		if (IS_ERR(new)) {
+			if (!(not_new && PTR_ERR(new) == -ENOENT))
+				printf("%s: needs update\n", ce->name);
 			continue;
 		}
 		active_cache[i] = new;
@@ -328,6 +345,10 @@ int main(int argc, char **argv)
 				i += 3;
 				continue;
 			}
+			if (!strcmp(path, "--ignore-missing")) {
+				not_new = 1;
+				continue;
+			}
 			die("unknown option %s", path);
 		}
 		if (!verify_path(path)) {