ソースを参照

Show live export progress by polling destination

cere 1 ヶ月 前
コミット
17eac8b37e
1 ファイル変更39 行追加37 行削除
  1. 39 37
      svn_interact/svn_git_init

+ 39 - 37
svn_interact/svn_git_init

@@ -5,46 +5,48 @@ DEST="${1:-trunk-git}"
 
 echo "Exporting SVN working copy to $DEST (versioned files only)..."
 rm -rf -- "$DEST"
-count=0
-start_ts="$(date +%s.%N)"
-if command -v script >/dev/null 2>&1; then
-  # script(1) forces a pseudo-tty; -f flushes each write for live progress
-  export_cmd=(script -qf -c "svn export . \"$DEST\"" /dev/null)
-elif command -v stdbuf >/dev/null 2>&1; then
-  export_cmd=(stdbuf -o0 -e0 svn export . "$DEST")
-else
-  export_cmd=(svn export . "$DEST")
-fi
 
-# Keep awk unbuffered too
-if command -v stdbuf >/dev/null 2>&1; then
-  awk_cmd=(stdbuf -o0 awk -v start="$start_ts")
-else
-  awk_cmd=(awk -W interactive -v start="$start_ts")
+# Optionally compute total items upfront for a rough percentage
+total_items=""
+if total=$(svn list -R . 2>/dev/null | wc -l || true); then
+  total_items="$total"
 fi
 
-"${export_cmd[@]}" | "${awk_cmd[@]}" '
-  function elapsed() {
-    cmd = "date +%s.%N"
-    cmd | getline now
-    close(cmd)
-    split(start, s, ".")
-    split(now, n, ".")
-    return (n[1]-s[1]) + (n[2]-s[2])/1e9
-  }
-  {
-    count++
-    if (count % 25 == 0) {
-      t = elapsed()
-      printf("[%.1fs] Exported %d items\n", t, count)
-      fflush()
-    }
-  }
-  END {
-    t = elapsed()
-    printf("[%.1fs] Exported %d items (done)\n", t, count)
-  }
-'
+start_ts="$(date +%s.%N)"
+
+# Run export in background so we can emit our own progress based on files created.
+svn export --quiet . "$DEST" &
+export_pid=$!
+
+last_reported=-1
+while kill -0 "$export_pid" 2>/dev/null; do
+  if [[ -d "$DEST" ]]; then
+    current=$(find "$DEST" -mindepth 1 -print | wc -l)
+  else
+    current=0
+  fi
+
+  # Only log when count changes to reduce noise.
+  if [[ "$current" -ne "$last_reported" ]]; then
+    now="$(date +%s.%N)"
+    elapsed=$(awk -v s="$start_ts" -v n="$now" 'BEGIN{split(s,sa,".");split(n,na,".");printf "%.1f", (na[1]-sa[1]) + (na[2]-sa[2])/1e9}')
+    pct=""
+    if [[ -n "$total_items" && "$total_items" -gt 0 ]]; then
+      pct=$(awk -v c="$current" -v t="$total_items" 'BEGIN{printf(" (%.1f%%)", (c*100)/t)}')
+    fi
+    printf "[%ss] Exported %d items%s\n" "$elapsed" "$current" "$pct"
+    last_reported="$current"
+  fi
+
+  sleep 1
+done
+
+wait "$export_pid"
+
+final_count=$(find "$DEST" -mindepth 1 -print | wc -l 2>/dev/null || echo 0)
+end_now="$(date +%s.%N)"
+end_elapsed=$(awk -v s="$start_ts" -v n="$end_now" 'BEGIN{split(s,sa,".");split(n,na,".");printf "%.1f", (na[1]-sa[1]) + (na[2]-sa[2])/1e9}')
+printf "[%ss] Exported %d items (done)\n" "$end_elapsed" "$final_count"
 
 cd "$DEST"
 git init