4

I am migrating all my JavaScript files to typescript, first i need to convert my existing JavaScript file extensions to *.ts

To test i have following command, but this fail: used examples

for f in **/*.js; do
  git mv "$f" "${f%.js}.ts"
done

I always seem to get:

fatal: not under version control, source=jscript/index.js, destination=jscript/index.ts

My final goal is to recursive go to my javascript folder and rename inplace from *.js to *.ts

Community
  • 1
  • 1
Jan Van Looveren
  • 908
  • 2
  • 11
  • 21

3 Answers3

2

Yeay!

The complete sollution in my case is a below, i hope somebody else can use this as well. just place in the root folder of your repo and adjust the folders you work with:

#!/usr/bin/env bash 
shopt -s globstar
for f in /public/jscript/*.min.js public/jscript/**/*.min.js; do
  git rm -r "$f"
done

# convert all the js files to ts files.
for f in public/jscript/**/*.js; do
  git mv "$f" "${f%.js}.ts"
done
Jan Van Looveren
  • 908
  • 2
  • 11
  • 21
  • Do you know why this works? How did you select those specific directories? As it stands, this isn't much use to someone else with the same problem. – jonrsharpe May 15 '16 at 08:31
1

Seems like you forgot to add them to Git. Run :

git add --all :/

After that you should be able to rename. Though I would recommend committing first:

git add --all :/ && git commit -m update

Note: If you haven't even initialized the repo, you will need to run git init before everything else.


EDIT:

As per your comment, it doesn't go into the sub-folders because you probably forgot to use globstar. Add the line

shopt -s globstar

before you use **.

Jahid
  • 21,542
  • 10
  • 90
  • 108
  • Owke, i have tested this and effectively something went wrong in side the repo. i have cloned my git repo again and then it was working. Now that its working do, it only converts the the js files in the root of the folder, the script does not doe this in all sub folders – Jan Van Looveren May 15 '16 at 07:32
  • @JanVanLooveren : You need to use globstar to make `**` work as expected. – Jahid May 15 '16 at 16:40
0

I always seem to get: fatal: not under version control, source=jscript/index.js, destination=jscript/index.ts

The error message tells you that the file jscript/index.js was not added to the Git repository yet. Add all the .js files to the repository, commit the changes then try your script again.

But you don't need to use git mv. Make sure your repository is in a clean state, rename the files in the working tree (your script is probably fine, just use the regular mv instead of git mv) then add all the .ts files to Git. Git will detect you renamed the files and the final outcome is the same as the one of git mv.

axiac
  • 68,258
  • 9
  • 99
  • 134