0

You know that sometimes in rare cases a developer decides to restructure his project packaging structure. When it comes to this, managing this task is quiet a cool smooth process if you only do it within Android Studio, and for example you can follow the steps in this solution which worked amazingly great for me : Package Renaming Android Studio

However if you do this while you are in a team and you are using bitbucket and git commands for instance, this turn out to be quiet a delicate matter.

In my case, I proceeded to package renaming using Android Studio in a first step. The Renaming was successful until my task has been reviewed and rejected because in bitbucket, when selecting Show diff of => all changes in this pull request, well, all the files in the project are not marked as MOVED or MODIFIED ,but actually marked as DELETED and ADDED.

So to avoid any unwanted consequences such as loosing file history, I proceeded to the renaming of the package via Git Command line which are detailed in the following steps :

  1. cd parent_folder (package_to_be_renamed)
  2. git mv package_to_be_renamed new_package_name

and if you want to add a sub package within you package

  1. cd parent_package (package_to_be_renamed)
  2. mkdir new_package_name
  3. git add new_package_name (note git won't add an empty folder in the repository)
  4. git mv child_package new_package_name
  5. pwd result => parent_package/new_package_name/child_package

And to fix the project error don't forget to rename your package file by replacing the old package by the new one. for example, in Android Studio : cmd+shift+r => replace parent_package.child_package with parent_package.new_package_name.child_package

Once pushing these changes bitbucket show again all the files as DELETED and ADDED, while when selecting the commit they are shown as MOVED..

So to make sure that everything is fine I created a branch from the master (master-test-renaming) and merged my repackaging_branch into this new branch. The result: project working FINE with the new Package name. And history files are still trackable in Android Studio Git, show history.

Now my question is : Why bitbucket displays the files as deleted and added (when selecting Show diff of => all changes in this pull request) ???

  • Git's logic for how it handles moving/renaming of files is fuzzy, and there is no guarantee that history will be preserved. If you absolutely want to maintain history, then don't move folders around like this. It might be possible to rewrite your entire history, but this also has major caveats. – Tim Biegeleisen Jun 07 '18 at 11:02
  • @TimBiegeleisen proceeding with git commands, I actually didn't lose any History, and yeah, it is a risky move. But sometimes, you are compelled to update your package naming, and it is confusing when dealing with such a task you find your files **deleted**. From a personal point of view, this **moving/renaming** should be a bit more clear when you are using bitbucket as an example. – Mohamed Aymen Haddad Jun 07 '18 at 15:26
  • I believe that Git uses fuzzy pattern matching to trace filenames across move/rename operations. If it can't, then it shows up as a deletion followed by an addition of another file. – Tim Biegeleisen Jun 07 '18 at 15:33

1 Answers1

0

Have you tried simply creating a new directory and then moving all the files? I believe Git recognizes "moved" files (thus, preserving the history).

TEK292
  • 261
  • 1
  • 13
  • it's the same thing with **git** **mv**, I am just moving my files to another directory, still i don't know why bitbucket shows the files as **deleted** – Mohamed Aymen Haddad Jun 07 '18 at 15:17