3

Given a Bitbucket workspace consisting of multiple repositories I'd like to search for a String occurrence in each of them. Here is how my bitbucket looks like:

Company / Projects / Departament

  repo1
  repo2
  ...
  repo 456

By String occurrence I mean a dependency declaration as a string

com.mycompany.oldlib

which is to be replaced with

com.mycompany.newlib

So basically I want to find what repos use the library and replace it with a new appropriate one.

Is there an API or any other way in Bitbucket to perform such search for a string occurrence in code? Checking out all of the repositories by hand and greping the code is not really a solution since there are hundreds of repositories.

St.Antario
  • 26,175
  • 41
  • 130
  • 318

2 Answers2

1

I'm not sure find-and-replace is feasible within any flavor of Bitbucket, given that they all rely heavily on Git to manage repository contents, and that API calls could interfere with normal commits. However, Bitbucket Cloud (bitbucket.org), Bitbucket Server, and Bitbucket Data Center all have a search function in the UI:

If you're using Bitbucket Cloud, then there's a search API as well:

(There doesn't appear to be a search API in the latest versions of Bitbucket Server or Bitbucket Data Center, but some earlier versions may have it.)

So while you probably can't do find-and-replace natively, you should be able to at least use code search in the UI and/or API to identify what needs to be updated.

Jim Redmond
  • 4,139
  • 1
  • 14
  • 18
0

I was not able to find any Bitbucket related APIs which allow such a mass replace operation to project files.

Although it's a really common use case still I am surprised that not so much is available on the internet about it.

Sadly we are left with doing it ourselves and assuming you are mentioning about grep command which points to the usage of Linux OS.

Here's the working and tested Linux command which iterates in directories and replaces the string using sed command,

find dirr/ -type f -name '*' -exec sed -i 's/com.mycompany.oldlib/com.mycompany.newlib/g' {} +

The only issue with this command is, it is really slow as you are doing the heavy lifting of finding and replacing over here, the execution time will depend on your System capabilities.

This above command is self-explanatory except may be the last "+" sign. More about it: Using semicolon (;) vs plus (+) with exec in find

Hope this helps, Thanks.

Suchandra T
  • 569
  • 4
  • 8
  • The only issue with this answer is, it is really slow and takes a lot of space as it requires the OP to clone hundreds of repositories locally. – phd Apr 01 '23 at 22:35
  • @phd So author is mentioning command like grep which means he does have local copies right. That's why in the answer i mentioned this is ONLY way over here even thought it was inefficient. Why are you commenting what i written in answer itself? – Suchandra T Apr 02 '23 at 00:46
  • 1
    The OP mentioned `grep` in this context: "Checking out all of the repositories by hand and `grep`ing the code **is not really a solution**" – phd Apr 02 '23 at 01:10