Repository: containers/buildah (language: go)

Issue:
Wrong repository name if the image is previously built with same name with one more level
### Issue Description

A image is already built with repository `foo/bar`, if image is built again with a different repository `bar`, the repository is still `foo/bar`.

### Steps to reproduce the issue

1. Create a simple `Dockerfile`

```
FROM busybox:1.36
ENTRYPOINT ["tail", "-f", "/dev/null"]
```

2. Run `podman build -t foo/bar .`
3. Run `podman build -t bar .`


### Describe the results you received

Run `podman images` and 1 image with repository `localhost/foo/bar` is shown.


### Describe the results you expected

Run `podman images` and 2 images with repository `localhost/foo/bar` and `localhost/bar` are shown.

### podman info output

```yaml
host:
  arch: amd64
  buildahVersion: 1.30.0
  cgroupControllers:
  - cpu
  - io
  - memory
  - pids
  cgroupManager: systemd
  cgroupVersion: v2
  conmon:
    package: conmon-2.1.7-2.fc38.x86_64
    path: /usr/bin/conmon
    version: 'conmon version 2.1.7, commit: '
  cpuUtilization:
    idlePercent: 76.96
    systemPercent: 15.54
    userPercent: 7.5
  cpus: 1
  databaseBackend: boltdb
  distribution:
    distribution: fedora
    variant: coreos
    version: "38"
  eventLogger: journald
  hostname: localhost.localdomain
  idMappings:
    gidmap:
    - container_id: 0
      host_id: 1000
      size: 1
    - container_id: 1
      host_id: 100000
      size: 1000000
    uidmap:
    - container_id: 0
      host_id: 501
      size: 1
    - container_id: 1
      host_id: 100000
      size: 1000000
  kernel: 6.3.12-200.fc38.x86_64
  linkmode: dynamic
  logDriver: journald
  memFree: 1286209536
  memTotal: 2048794624
  networkBackend: netavark
  networkBackendInfo:
    backend: ""
    dns: {}
  ociRuntime:
    name: crun
    package: crun-1.8.5-1.fc38.x86_64
    path: /usr/bin/crun
    version: |-
      crun version 1.8.5
      commit: b6f80f766c9a89eb7b1440c0a70ab287434b17ed
      rundir: /run/user/501/crun
      spec: 1.0.0
      +SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL
  os: linux
  pasta:
    executable: ""
    package: ""
    version: ""
  remoteSocket:
    exists: true
    path: /run/user/501/podman/podman.sock
  security:
    apparmorEnabled: false
    capabilities: CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT
    rootless: true
    seccompEnabled: true
    seccompProfilePath: /usr/share/containers/seccomp.json
    selinuxEnabled: true
  serviceIsRemote: true
  slirp4netns:
    executable: /usr/bin/slirp4netns
    package: slirp4netns-1.2.0-12.fc38.x86_64
    version: |-
      slirp4netns version 1.2.0
      commit: 656041d45cfca7a4176f6b7eed9e4fe6c11e8383
      libslirp: 4.7.0
      SLIRP_CONFIG_VERSION_MAX: 4
      libseccomp: 2.5.3
  swapFree: 0
  swapTotal: 0
  uptime: 0h 3m 57.00s
plugins:
  authorization: null
  log:
  - k8s-file
  - none
  - passthrough
  - journald
  network:
  - bridge
  - macvlan
  - ipvlan
  volume:
  - local
registries:
  search:
  - docker.io
store:
  configFile: /var/home/core/.config/containers/storage.conf
  containerStore:
    number: 0
    paused: 0
    running: 0
    stopped: 0
  graphDriverName: overlay
  graphOptions: {}
  graphRoot: /var/home/core/.local/share/containers/storage
  graphRootAllocated: 106769133568
  graphRootUsed: 2345537536
  graphStatus:
    Backing Filesystem: xfs
    Native Overlay Diff: "true"
    Supports d_type: "true"
    Using metacopy: "false"
  imageCopyTmpDir: /var/tmp
  imageStore:
    number: 2
  runRoot: /run/user/501/containers
  transientStore: false
  volumePath: /var/home/core/.local/share/containers/storage/volumes
version:
  APIVersion: 4.5.1
  Built: 1685123928
  BuiltTime: Sat May 27 02:58:48 2023
  GitCommit: ""
  GoVersion: go1.20.4
  Os: linux
  OsArch: linux/amd64
  Version: 4.5.1
```


### Podman in a container

No

### Privileged Or Rootless

Rootless

### Upstream Latest Release

Yes

### Additional environment details

I upgraded with brew and `podman --version` is showing 4.6.0 but `podman info` still shows `4.5.1`.

### Additional information

_No response_


Discussion / hints:
Thanks for reaching out, @franzwong !

@nalind @flouthoc PTAL
A friendly reminder that this issue had no activity for 30 days.
Transferring the issue to Buildah.  So what's happening is that Buildah seems to resolve the input name before and will use the resolved name.  Otherwise, it'll normalize the name.

Fix patch:
diff --git a/imagebuildah/executor.go b/imagebuildah/executor.go
index 9eef4fa8da9..2b9773048f1 100644
--- a/imagebuildah/executor.go
+++ b/imagebuildah/executor.go
@@ -376,15 +376,11 @@ func (b *Executor) resolveNameToImageRef(output string) (types.ImageReference, e
 	if imageRef, err := alltransports.ParseImageName(output); err == nil {
 		return imageRef, nil
 	}
-	runtime, err := libimage.RuntimeFromStore(b.store, &libimage.RuntimeOptions{SystemContext: b.systemContext})
+	resolved, err := libimage.NormalizeName(output)
 	if err != nil {
 		return nil, err
 	}
-	resolved, err := runtime.ResolveName(output)
-	if err != nil {
-		return nil, err
-	}
-	imageRef, err := storageTransport.Transport.ParseStoreReference(b.store, resolved)
+	imageRef, err := storageTransport.Transport.ParseStoreReference(b.store, resolved.String())
 	if err == nil {
 		return imageRef, nil
 	}


Test patch:
diff --git a/tests/bud.bats b/tests/bud.bats
index c331fc6fc62..5c1b5cba372 100644
--- a/tests/bud.bats
+++ b/tests/bud.bats
@@ -6359,3 +6359,12 @@ _EOF
     false
   fi
 }
+
+@test "build with name path changes" {
+  _prefetch busybox
+  run_buildah build --no-cache --quiet --pull=false $WITH_POLICY_JSON -t foo/bar $BUDFILES/commit/name-path-changes/
+  run_buildah build --no-cache --quiet --pull=false $WITH_POLICY_JSON -t bar $BUDFILES/commit/name-path-changes/
+  run_buildah images
+  expect_output --substring "localhost/foo/bar"
+  expect_output --substring "localhost/bar"
+}
diff --git a/tests/bud/commit/name-path-changes/Dockerfile b/tests/bud/commit/name-path-changes/Dockerfile
new file mode 100644
index 00000000000..d6fc44b2c8b
--- /dev/null
+++ b/tests/bud/commit/name-path-changes/Dockerfile
@@ -0,0 +1,2 @@
+FROM busybox
+RUN pwd