{"thread":{"id":"1767","subject":"[PATCH 5/5] archimport - better handling of temp dirs","startedAt":"2005-09-11T09:26:05Z","lastAt":"2005-09-11T09:26:05Z","messageCount":2,"participants":["martin@catalyst.net.nz"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"8276","messageId":"1126430765146-git-send-email-martin@catalyst.net.nz","threadId":"1767","inReplyTo":"1126430765745-git-send-email-martin@catalyst.net.nz","subject":"[PATCH 5/5] archimport - better handling of temp dirs","fromName":"","fromEmail":"martin@catalyst.net.nz","sentAt":"2005-09-11T09:26:05Z","receivedAt":"2005-09-11T09:26:05Z","isPatch":true,"sender":{"key":"martin@laptop.org","avatar":null},"body":"Switched from backwards hard-coded tmp directory creation to using\nFile::Temp::tempdir() to create the directory inside $TMP_PATH or\nwhat the user has provided via the -t parameter.\n\nSigned-off-by: Martin Langhoff <martin@catalyst.net.nz>\n\n\n---\n\n git-archimport.perl |   10 +++++-----\n 1 files changed, 5 insertions(+), 5 deletions(-)\n\n67d3cf04c6a2353835d52a4d1ae85c7fb91bad36\ndiff --git a/git-archimport.perl b/git-archimport.perl\n--- a/git-archimport.perl\n+++ b/git-archimport.perl\n@@ -23,7 +23,6 @@ See man (1) git-archimport for more deta\n \n  - create tag objects instead of ref tags\n  - audit shell-escaping of filenames\n- - better handling of temp directories\n  - hide our private tags somewhere smarter\n  - find a way to make \"cat *patches | patch\" safe even when patchfiles are missing newlines  \n \n@@ -37,7 +36,7 @@ use strict;\n use warnings;\n use Getopt::Std;\n use File::Spec;\n-use File::Temp qw(tempfile);\n+use File::Temp qw(tempfile tempdir);\n use File::Path qw(mkpath);\n use File::Basename qw(basename dirname);\n use String::ShellQuote;\n@@ -72,9 +71,10 @@ usage if $opt_h;\n @ARGV >= 1 or usage();\n my @arch_roots = @ARGV;\n \n-my $tmp = $opt_t;\n-$tmp ||= '/tmp';\n-$tmp .= '/git-archimport/';\n+my ($tmpdir, $tmpdirname) = tempdir('git-archimport-XXXXXX', TMPDIR => 1, CLEANUP => 1);\n+my $tmp = $opt_t || 1;\n+$tmp = tempdir('git-archimport-XXXXXX', TMPDIR => 1, CLEANUP => 1);\n+$opt_v && print \"+ Using $tmp as temporary directory\\n\";\n \n my @psets  = ();                # the collection\n my %psets  = ();                # the collection, by name\n"},{"id":"8281","messageId":"1126430765745-git-send-email-martin@catalyst.net.nz","threadId":"1767","inReplyTo":"11264307653007-git-send-email-martin@catalyst.net.nz","subject":"[PATCH 4/5] archimport - use GIT_DIR instead of hardcoded \".git\"","fromName":"","fromEmail":"martin@catalyst.net.nz","sentAt":"2005-09-11T09:26:05Z","receivedAt":"2005-09-11T09:26:05Z","isPatch":true,"sender":{"key":"martin@laptop.org","avatar":null},"body":"Use GIT_DIR from the environment instead of a hardcoded '.git' string.\n\nSigned-off-by: Martin Langhoff <martin@catalyst.net.nz>\n\n\n---\n\n git-archimport.perl |   42 ++++++++++++++++++++++--------------------\n 1 files changed, 22 insertions(+), 20 deletions(-)\n\n600258cfefc38939f09f267090ba400d80ab14c3\ndiff --git a/git-archimport.perl b/git-archimport.perl\n--- a/git-archimport.perl\n+++ b/git-archimport.perl\n@@ -24,7 +24,6 @@ See man (1) git-archimport for more deta\n  - create tag objects instead of ref tags\n  - audit shell-escaping of filenames\n  - better handling of temp directories\n- - use GIT_DIR instead of hardcoded \".git\"\n  - hide our private tags somewhere smarter\n  - find a way to make \"cat *patches | patch\" safe even when patchfiles are missing newlines  \n \n@@ -52,6 +51,9 @@ use IPC::Open2;\n $SIG{'PIPE'}=\"IGNORE\";\n $ENV{'TZ'}=\"UTC\";\n \n+my $git_dir = $ENV{\"GIT_DIR\"} || \".git\";\n+$ENV{\"GIT_DIR\"} = $git_dir;\n+\n our($opt_h,$opt_v, $opt_T,\n     $opt_C,$opt_t);\n \n@@ -179,7 +181,7 @@ foreach my $root (@arch_roots) {\n ##      and put an initial import\n ##      or a full tag\n my $import = 0;\n-unless (-d '.git') { # initial import\n+unless (-d $git_dir) { # initial import\n     if ($psets[0]{type} eq 'i' || $psets[0]{type} eq 't') {\n         print \"Starting import from $psets[0]{id}\\n\";\n \t`git-init-db`;\n@@ -190,11 +192,11 @@ unless (-d '.git') { # initial import\n     }\n } else {    # progressing an import\n     # load the rptags\n-    opendir(DIR, \".git/archimport/tags\")\n+    opendir(DIR, \"$git_dir/archimport/tags\")\n \t|| die \"can't opendir: $!\";\n     while (my $file = readdir(DIR)) {\n \t# skip non-interesting-files\n-\tnext unless -f \".git/archimport/tags/$file\";\n+\tnext unless -f \"$git_dir/archimport/tags/$file\";\n \tnext if     $file =~ m/--base-0$/; # don't care for base-0\n \tmy $sha = ptag($file);\n \tchomp $sha;\n@@ -238,7 +240,7 @@ foreach my $ps (@psets) {\n     }\n \n     unless ($import) { # skip for import\n-        if ( -e \".git/refs/heads/$ps->{branch}\") {\n+        if ( -e \"$git_dir/refs/heads/$ps->{branch}\") {\n             # we know about this branch\n             `git checkout    $ps->{branch}`;\n         } else {\n@@ -291,7 +293,7 @@ foreach my $ps (@psets) {\n     # imports don't give us good info\n     # on added files. Shame on them\n     if ($ps->{type} eq 'i' || $ps->{type} eq 't') { \n-        `find . -type f -print0 | grep -zv '^./.git' | xargs -0 -l100 git-update-index --add`;\n+        `find . -type f -print0 | grep -zv '^./$git_dir' | xargs -0 -l100 git-update-index --add`;\n         `git-ls-files --deleted -z | xargs --no-run-if-empty -0 -l100 git-update-index --remove`;\n     }\n \n@@ -355,8 +357,8 @@ foreach my $ps (@psets) {\n     # Who's your daddy?\n     #\n     my @par;\n-    if ( -e \".git/refs/heads/$ps->{branch}\") {\n-        if (open HEAD, \"<.git/refs/heads/$ps->{branch}\") {\n+    if ( -e \"$git_dir/refs/heads/$ps->{branch}\") {\n+        if (open HEAD, \"<$git_dir/refs/heads/$ps->{branch}\") {\n             my $p = <HEAD>;\n             close HEAD;\n             chomp $p;\n@@ -403,11 +405,11 @@ foreach my $ps (@psets) {\n     #\n     # Update the branch\n     # \n-    open  HEAD, \">.git/refs/heads/$ps->{branch}\";\n+    open  HEAD, \">$git_dir/refs/heads/$ps->{branch}\";\n     print HEAD $commitid;\n     close HEAD;\n-    unlink ('.git/HEAD');\n-    symlink(\"refs/heads/$ps->{branch}\",\".git/HEAD\");\n+    unlink (\"$git_dir/HEAD\");\n+    symlink(\"refs/heads/$ps->{branch}\",\"$git_dir/HEAD\");\n \n     # tag accordingly\n     ptag($ps->{id}, $commitid); # private tag\n@@ -436,7 +438,7 @@ sub apply_import {\n \n     `tla get -s --no-pristine -A $ps->{repo} $ps->{id} $tmp/import`;\n     die \"Cannot get import: $!\" if $?;    \n-    `rsync -v --archive --delete --exclude '.git' --exclude '.arch-ids' --exclude '{arch}' $tmp/import/* ./`;\n+    `rsync -v --archive --delete --exclude '$git_dir' --exclude '.arch-ids' --exclude '{arch}' $tmp/import/* ./`;\n     die \"Cannot rsync import:$!\" if $?;\n     \n     `rm -fr $tmp/import`;\n@@ -482,7 +484,7 @@ sub apply_cset {\n     }\n \n     # bring in new files\n-    `rsync --archive --exclude '.git' --exclude '.arch-ids' --exclude '{arch}' $tmp/changeset/new-files-archive/* ./`;\n+    `rsync --archive --exclude '$git_dir' --exclude '.arch-ids' --exclude '{arch}' $tmp/changeset/new-files-archive/* ./`;\n \n     # deleted files are hinted from the commitlog processing\n \n@@ -577,7 +579,7 @@ sub tag {\n     $tag = shell_quote($tag);\n     \n     if ($commit) {\n-        open(C,\">.git/refs/tags/$tag\")\n+        open(C,\">$git_dir/refs/tags/$tag\")\n             or die \"Cannot create tag $tag: $!\\n\";\n         print C \"$commit\\n\"\n             or die \"Cannot write tag $tag: $!\\n\";\n@@ -585,7 +587,7 @@ sub tag {\n             or die \"Cannot write tag $tag: $!\\n\";\n         print \" * Created tag ' $tag' on '$commit'\\n\" if $opt_v;\n     } else {                    # read\n-        open(C,\"<.git/refs/tags/$tag\")\n+        open(C,\"<$git_dir/refs/tags/$tag\")\n             or die \"Cannot read tag $tag: $!\\n\";\n         $commit = <C>;\n         chomp $commit;\n@@ -603,12 +605,12 @@ sub ptag {\n     $tag =~ s|/|--|g; \n     $tag = shell_quote($tag);\n     \n-    unless (-d '.git/archimport/tags') {\n-        mkpath('.git/archimport/tags');\n+    unless (-d \"$git_dir/archimport/tags\") {\n+        mkpath(\"$git_dir/archimport/tags\");\n     }\n \n     if ($commit) {              # write\n-        open(C,\">.git/archimport/tags/$tag\")\n+        open(C,\">$git_dir/archimport/tags/$tag\")\n             or die \"Cannot create tag $tag: $!\\n\";\n         print C \"$commit\\n\"\n             or die \"Cannot write tag $tag: $!\\n\";\n@@ -618,10 +620,10 @@ sub ptag {\n \t    unless $tag =~ m/--base-0$/;\n     } else {                    # read\n         # if the tag isn't there, return 0\n-        unless ( -s \".git/archimport/tags/$tag\") {\n+        unless ( -s \"$git_dir/archimport/tags/$tag\") {\n             return 0;\n         }\n-        open(C,\"<.git/archimport/tags/$tag\")\n+        open(C,\"<$git_dir/archimport/tags/$tag\")\n             or die \"Cannot read tag $tag: $!\\n\";\n         $commit = <C>;\n         chomp $commit;\n"}]}